Android NDK基础19:C++_构造函数属性初始化_new(delete)_静态成员_this指针

构造函数属性初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//构造函数的属性初始化列表
class Teacher {
private:
char* name;
public:
Teacher(char* name) {
this->name = name;
cout << "Teacher有参构造函数" << endl;
}

~Teacher() {
cout << "Teacher析构函数" << endl;
}

char* getName() {
return this->name;
}
};

class Student {
private:
int id;
//属性对象
//Teacher t = Teacher("Joe");
Teacher t1;
Teacher t2;
public:
Student(int id, char *t1_name, char* t2_name) : t1(t1_name), t2(t2_name) {
this->id = id;
cout << "Student有参构造函数" << endl;
}

void print() {
cout << id << "," << t1.getName() <<"," << t2.getName() << endl;
}

~Student() {
cout << "Student析构函数" << endl;
}
};

void func() {
//Teacher构造 -> Student构造 -> Student析构 -> Teacher析构
Student s1(10, "Miss Zhao", "Mr Qian");
//Student s2(20, "Mr Sun", "Miss Li");
s1.print();
//s2.print();
}

void main() {
func();

system("pause");
}

new(delete)动态内存分配

  • C++ 通过new(delete)动态内存分配
  • C 通过malloc(free)动态内存分配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Teacher {
private:
char* name;
public:
Teacher(char* name) {
this->name = name;
cout << "Teacher有参构造函数" << endl;
}

~Teacher() {
cout << "Teacher析构函数" << endl;
}

void setName(char* name) {
this->name = name;
}

char* getName() {
return this->name;
}
};

void func() {
//C++
//会调用构造和析构函数
Teacher *t1 = new Teacher("Joe");
cout << t1->getName() << endl;
//释放
delete t1;

//C
//不会调用构造和析构函数
//Teacher *t2 = (Teacher*) malloc(sizeof(Teacher));
//t2->setName("Joe");
//free(t2);
}

void main() {
func();
//数组类型
//C
//int *p1 = (int*) malloc(sizeof(int) * 10);
//p1[0] = 1;
//free(p1);

//C++
int *p2 = new int[10];
p2[0] = 2;
//释放数组 []
delete[] p2;

system("pause");
}

static 静态属性和方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//static 静态属性和方法
class Teacher {
public:
char* name;
//计数器
static int total;
public:
Teacher(char* name) {
this->name = name;
cout << "Teacher有参构造函数" << endl;
}

~Teacher() {
cout << "Teacher析构函数" << endl;
}

void setName(char* name) {
this->name = name;
}

char* getName() {
return this->name;
}

//计数,静态函数
static void count() {
total++;
cout << total << endl;
}
};

//静态属性初始化赋值
int Teacher::total = 9;

void main() {
Teacher::total++;
cout << Teacher::total << endl;
//直接通过类名访问
Teacher::count();

//也可以通过对象名访问
Teacher t("Joe");
t.count();

system("pause");
}

this指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//this,当前对象的指针
//函数是共享的,必须要有能够标识当前对象是谁的办法
class Teacher {
private:
char* name;
int age;
public:
Teacher(char* name,int age) {
this->name = name;
this->age = age;
cout << "Teacher有参构造函数" << endl;
}

~Teacher() {
cout << "Teacher析构函数" << endl;
}

//常函数,修饰的是this
//既不能改变指针的值,又不能改变指针指向的内容
//const Teacher* const this
void print() const {
printf("%#x\n", this);
//不能改变属性的值
//this->name = "Mr Ye";
//不能改变this指针的值
//this = (Teacher*)0x000008;
cout << this->name << "," << this->age << endl;
}

void normal_print() {
cout << this->name << "," << this->age << endl;
}
};

void main() {
Teacher t1("Joe", 20);
const Teacher t2("Ann", 18);
//t2.normal_print(); 常量对象只能调用常量函数,不能调用非常量函数
//常函数,当前对象不能被修改,防止数据成员被非法访问
printf("%#x\n", &t1);
t1.print();

printf("%#x\n", &t2);
t2.print();

system("pause");
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :