(创建于2017/12/24)
如果一个类中属性有另一个类或者有const类型,如何初始化
#define _CRT_SECURE_NO_WARNINGS
#include "iostream" //包含c++的头文件
using namespace std;
class A {
public:
A(int a) {
}
};
class B {
public:
int b;
A a;
A a2;
const int c;
B(int b):a(1),a2(2),c(3){
}
B(int a, int b, int c, int d,int e) :a(c), a2(d),c(e){
}
};
int main() {
B b(1,2,3,4,5);
system("pause");
return 0;
}
构造函数中调用构造函数
分析一下下边这个函数,为什么最终打印c得到的是一个很奇怪的数字呢?函数执行到 B b(1,2);的时候会执行B的两个参数的有参构造方法,将ab初始化,然后在此调用三个有参构造方法,传入三个值1,2,100,我们知道调用函数的构造方法会默认产生一个匿名对象,这个匿名对象是B的另一个对象,和b是不同的,由于这个对象没有被接收,导致执行完成之后就调用了他的析构函数销毁了这个对象,所以到此位置,b的c就没有被赋值过,所以打印的是这样的结果
#define _CRT_SECURE_NO_WARNINGS
#include "iostream" //包含c++的头文件
using namespace std;
class B {
public:
int b;
int c;
int d;
B(int a,int b){
this->b = a;
this->c = b;
B(a, b, 100);
}
B(int c, int d, int e) {
this->b = c;
this->c = d;
this->d = e;
}
~B(){
printf("析构函数执行:%d,%d,%d\n", b, c, d);
}
};
int main() {
B b(1,2);
printf("c:%d\n", b.d);
system("pause");
return 0;
}
打印结果
析构函数执行:1,2,100
c:-858993460
请按任意键继续. . .
//注意构造和析构的顺序:
//先执行类内部的对象的构造方法,再执行的类的构造方法,析构函数则是先执行的外层的析构函数,然后执行的对象属性的析构函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream> //这个头文件和命名空间有一个不存在 cout就无法使用
using namespace std;
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 t1;
public:
//属性对象通过构造方法初始化
Student(int id, char *n1, char *n2):t1(n1) {
this->id = id;
cout << "Student有参构造函数" << endl;
}
void myprint() {
cout << id << "," << t1.getName()<< endl;
}
~Student() {
cout << "Student析构函数" << endl;
}
};
void func() {
Student s1(10, "miss bo", "mrs liu");
s1.myprint();
}
C和C++开辟内存对比
#define _CRT_SECURE_NO_WARNINGS
#include "iostream" //包含c++的头文件
using namespace std;
class B {
public:
int b;
int c;
int d;
B(int a,int b){
this->b = a;
this->c = b;
B(a, b, 100);
}
B(int c, int d, int e) {
this->b = c;
this->c = d;
this->d = e;
}
~B(){
printf("析构函数执行:%d,%d,%d\n", b, c, d);
}
};
int main() {
///////////////////基本数据类型///////////////////////
//C
int *a = (int *)malloc(sizeof(int));
*a = 10;
printf("*a=%d\n", *a);
free(a);
//C++
int *b = new int();
*b = 20;
cout << "*b="<<*b<< endl;
delete b;
///////////////////数组类型///////////////////////
//C
int *c = (int *)malloc(sizeof(int) * 10);
*(c + 3) = 30;
printf("第三个数组元素值=%d\n", c[3]);
free(c);
//C++
int *d = new int[10];
*(d + 4) = 40;
cout << "第四个数组元素="<<d[4]<< endl;
delete [] d;//删除数据加[]
//////////////////对象类型/////////////////////
//C
B *e = (B*)malloc(sizeof(B));
e->c = 100;
printf("e->c =%d\n", e -> c);
free(e);
//C++
B *f = new B(200,300);
cout << "f->c ="<<f->c<< endl;
delete f;
system("pause");
return 0;
}
malloc和delete?new和free?
这两种运算符能否混搭使用呢,比如我用malloc开辟的内存,能否使用delete回收?我用new创建的对象,能否使用free释放?答案是可以的,但是有一点,delete回收内存相对于free,多了一步执行析构函数的过程,而new创建对象相对于malloc,多了一步执行构造方法的过程
//C++的new delete和C的malloc free的区别:
//new delete的方式创建和销毁会分别执行构造函数和析构函数
//malloc free则不会执行
void func2() {
//C++
//Teacher *t1 = new Teacher("renzhenming");
//cout << t1->getName() << endl;
//释放
//delete t1;
//C
Teacher* t2 = (Teacher*)malloc(sizeof(Teacher));
t2->setName("renzhenming2");
free(t2);
}
void main() {
func();
cout << "----------------------------------------------" << endl;
//C++和C创建和回收数组
//c
int *p = (int*)malloc(sizeof(int) * 10);
p[0] = 9;
free(p);
//c++
int *p2 = new int[10];
p[0] = 2;
//释放数组
//注意释放数组需要加[]
delete[]p2;
system("pause");
}