Android NDK基础17:C++_命名空间_类_引用

C与C++的关系

  • 1.C++可以与C代码进行混编
  • 2.C++面向对象编程语言、C面向过程的编程
  • 3.C++对C的一个增强

命名空间

#include <stdlib.h>
#include <iostream>

//标准命名空间(包含很多标准的定义)
using namespace std;
//命名空间类似于Java中包(归类)

//自定义命名空间
namespace NSP_A {
    int a = 9;

    struct Teacher t;
    {
        char name[20];
        int age;
    };

    struct Student {
        char name[20];
        int age;
    };
}

namespace NSP_B {
    int a = 12;

    //命名空间嵌套
    namespace NSP_C {
        int c = 90;        
    }
}

void main() {
    printf("Hello World\n");
    //运算符重载
    //std::cout << "this is c plus plus" << std::endl;
    cout << "this is c plus plus" << endl;
    //使用命名空间
    //::访问修饰符
    cout << NSP_A::a << endl;
    cout << NSP_B::a << endl;

    cout << NSP_B::NSP_C::c << endl;

    struct NSP_A::Teacher t;
    t.age = 25;

    //使用命名空间中的结构体
    using NSP_A::Student;
    Student s;
    s.age = 10;

    system("pause");
}

类和结构体

//类
#define PI 3.14

//圆
class MyCircle {
//属性(共用权限访问修饰符)
private:
    double r;
    double s;

public:
    void setR(double r) {
        this->r = r;
    }

    //获取面积
    double getS() {
        return PI * r * r;
    }
};

void main() {
    MyCircle c;
    c.setR(4);

    cout << "圆的面积:" << c.getS() << endl;

    system("pause");
}
//结构体
struct MyTeacher {
public:
    char name[20];
    int age;
public:
    void say() {
        cout << this->age << "岁" << endl;
    }
};

void main() {
    MyTeacher t;  //struct关键字可以省略
    t.age = 10;
    t.say();
    system("pause");
}

布尔类型

C语言没有布尔类型,在C++中才有

//布尔类型
void main() {
    //bool isSingle = true;
    bool isSingle = 17;
    //false -17

    if (isSingle) {
        cout << "单身" << endl;
        cout << sizeof(bool) << endl;
    } else {
        cout << "有对象" << endl;
    }

    int a = 10, b = 20;
    ((a > b) ? a : b) = 30;  //三元运算符可以成为左值,这在C/Java中是不可行的
    cout << b << endl;

    system("pause");
}

引用

引用:内存空间地址的别名

void main() {
    //变量名-门牌号(内存空间0x00001的别名,可不可以有多个名字?)
    int a = 10;
    //b就这个内存空间另外一个别名
    //& C++中的引用
    int &b = a;    
    cout << b << endl;

    system("pause");
}
//指针值交换
void swap_1(int *a, int *b) {
    int c = 0;
    c = *a;
    *a = *b;
    *b = c;
}

//引用值交换
void swap_2(int &a, int &b) {
    int c = 0;
    c = a;
    a = b;
    b = c;
}

void main() {
    int x = 10;
    int y = 20;

    printf("%d,%d\n", x, y);
    //swap_1(&x, &y);
    //a成了x的别名,b成了y的别名
    swap_2(x, y);
    printf("%d,%d\n", x, y);

    system("pause");
}

引用的主要功能:作为函数的参数或返回值

//1.单纯给变量取别名没有任何意义,作为函数参数传递,能保证参数传递过程中不产生副本
//2.引用可以直接操作变量,指针要通过取值(*p),间接操作变量,指针的可读性差

struct Teacher {
    char* name;
    int age;
};

void print1(Teacher &t) {
    cout << t.name << "," << t.age << endl;
    t.age = 21;
}

void print2(Teacher *t) {
    cout << t->name << "," << t->age << endl;
    //(*t).name 
}

//引用的主要功能:作为函数的参数或返回值
void main() {
    Teacher t;
    t.name = "Joe";
    t.age = 20;

    print1(t);
    print2(&t);

    system("pause");
}

指针的引用,代替二级指针

//指针的引用,代替二级指针
struct Teacher {
    char* name;
    int age;
};

void getTeacher(Teacher **p) {
    Teacher *tmp = (Teacher*) malloc(sizeof(Teacher));
    tmp->age = 20;
    *p = tmp;
}

//Teacher* &p = (Teacher **p)
void getTeacher(Teacher* &p) {
    p = (Teacher*) malloc(sizeof(Teacher));
    p->age = 20;
}

void main() {
    Teacher *t = NULL;
    getTeacher(&t);

    system("pause");
}

指针常量与常量指针

void main() {
    //指针常量,指针的常量,不改变地址的指针,但是可以修改它指向的内容
    int a = 2, b = 3;
    int const *p1 = &a;
    //p1 = &b;  //NO
    *p1 = 4;

    //常量指针,指向常量的指针,内容不能修改
    const int *p2 = &a;
    p2 = &b;
    //*p2 = 9;  //NO
}

常引用

/*
//常引用类似于java中final
void printf(const int &a) {
    cout << a << endl;    
}

void main() {    
    //const int a;
    //引用必须要有值,不能为空
    //int &a = NULL;

    //常引用
    int a = 10, b = 9;
    const int &c = a;

    //字面量
    const int &d = 70;

    //c = b;
    printf(c);

    system("pause");
}
struct Teacher {
    char name[20];
    int age;
};

void print1(Teacher *t) {
    if (t != NULL) {
        cout << t->name << "," << t->age << endl;
    }
}

void print2(Teacher &t) {
    cout << t.name << "," << t.age << endl;
    t.age = 21;
}

void main() {
    Teacher t;

    Teacher *p = NULL;
    //报错,防止不报错,进行非空判断
    print1(p);

    //引用不能为空,没法传进去
    Teacher &t = NULL;
    print2(t);

    system("pause");
}

引用的大小

struct Teacher {
    char name[20];
    int age;
};

void main() {
    Teacher t;

    Teacher &t1 = t;
    Teacher *p = &t;

    cout << sizeof(t1) << endl;  //24
    cout << sizeof(p) << endl;  //4
    system("pause");
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/android-ndk-basic-cpp-namespaces-class-quote/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android NDK基础17:C++_命名空间_类_引用
C与C++的关系 1.C++可以与C代码进行混编 2.C++面向对象编程语言、C面向过程的编程 3.C++对C的一个增强 命名空间 #include <stdlib.h> #include <i……
<<上一篇
下一篇>>
文章目录
关闭
目 录