构造函数、析构函数、拷贝构造函数,常函数
//构造函数、析构函数、拷贝构造函数
/*
class Teacher{
private:
char *name;
int age;
public:
//无参构造函数(写了,就会覆盖默认的无参构造函数)
Teacher(){
cout << "无参构造函数" << endl;
}
//有参构造函数会覆盖默认的构造函数
Teacher(char *name, int age){
this->name = name;
this->age = age;
cout << "有参构造函数" << endl;
}
};
void main(){
//Teacher t1;
Teacher t2("yuehan",20);
//另外一种调用方式
Teacher t3 = Teacher("jack",21);
system("pause");
}
*/
/*
//析构函数
class Teacher{
private:
char *name;
int age;
public:
//无参构造函数赋默认值
Teacher(){
this->name = (char*)malloc(100);
strcpy(name,"jack walson");
age = 20;
cout << "无参构造函数" << endl;
}
//析构函数
//当对象要被系统释放时,析构函数被调用
//作用:善后处理
~Teacher(){
cout << "析构" << endl;
//释放内存
free(this->name);
}
};
void func(){
Teacher t1;
}
void main(){
func();
system("pause");
}
*/
//拷贝构造函数
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
this->name = name;
this->age = age;
cout << "有参构造函数" << endl;
}
//拷贝构造函数(值拷贝)
//默认拷贝构造函数,就是值拷贝
Teacher(const Teacher &obj){
this->name = obj.name;
this->age = obj.age;
cout << "拷贝构造函数" << endl;
}
void myprint(){
cout << name << "," << age << endl;
}
};
Teacher func1(Teacher t){
t.myprint();
return t;
}
void main(){
Teacher t1("rose",20);
//拷贝构造函数被调用的场景
//1.声明时赋值
//Teacher t2 = t1;
//t2.myprint();
//2.作为参数传入,实参给形参赋值
func1(t1);
//3.作为函数返回值返回,给变量初始化赋值
//Teacher t3 = func1(t1);
//这里不会被调用
//Teacher t1 ;
//Teacher t2;
//t1 = t2;
system("pause");
}
*/
//浅拷贝(值拷贝)问题
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
this->name = (char*)malloc(100);
strcpy(this->name,name);
this->age = age;
cout << "有参构造函数" << endl;
}
~Teacher(){
cout << "析构" << endl;
//释放内存
free(this->name);
}
void myprint(){
cout << name << "," << age << endl;
}
};
void func(){
Teacher t1("rose", 20);
Teacher t2 = t1;
t2.myprint();
}
void main(){
func();
system("pause");
}
*/
//深拷贝
/*
class Teacher{
private:
char *name;
int age;
public:
Teacher(char *name, int age){
int len = strlen(name);
this->name = (char*)malloc(len+1);
strcpy(this->name, name);
this->age = age;
cout << "有参构造函数" << endl;
}
~Teacher(){
cout << "析构" << endl;
//释放内存
free(this->name);
}
//深拷贝
Teacher(const Teacher &obj){
//复制name属性
int len = strlen(obj.name);
this->name = (char*)malloc(len+1);
strcpy(this->name,obj.name);
this->age = obj.age;
}
void myprint(){
cout << name << "," << age << endl;
}
};
void func(){
Teacher t1("rose", 20);
Teacher t2 = t1;
t2.myprint();
}
void main(){
func();
system("pause");
}
*/
//构造函数的属性初始化列表
/*
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("miss cang");
Teacher t1;
Teacher t2;
public:
Student(int id,char *t1_n, char* t2_n) : t1(t1_n), t2(t2_n){
this->id = id;
cout << "Student有参构造函数" << endl;
}
void myprint(){
cout << id << "," << t1.getName() <<"," << t2.getName() << endl;
}
~Student(){
cout << "Student析构函数" << endl;
}
};
void func(){
Student s1(10, "miss bo", "mrs liu");
//Student s2(20, "miss cang", "jason");
s1.myprint();
//s2.myprint();
}
void main(){
func();
system("pause");
}
*/
//C++ 通过new(delete)动态内存分配
//C malloc(free)
/*
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("jack");
cout << t1->getName() << endl;
//释放
delete t1;
//C
//Teacher *t2 = (Teacher*)malloc(sizeof(Teacher));
//t2->setName("jack");
//free(t2);
}
void main(){
func();
//数组类型
//C
//int *p1 = (int*)malloc(sizeof(int) * 10);
//p1[0] = 9;
//free(p1);
//C++
int *p2 = new int[10];
p2[0] = 2;
//释放数组 []
delete[] p2;
system("pause");
}
*/
//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 t1("yuehang");
t1.count();
system("pause");
}
*/
//类的大小
/*
class A{
public:
int i;
int j;
int k;
static int m;
};
class B{
public:
int i;
int j;
int k;
void myprintf(){
cout << "打印" << endl;
}
};
void main(){
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
//C/C++ 内存分区:栈、堆、全局(静态、全局)、常量区(字符串)、程序代码区
//普通属性与结构体相同的内存布局
//JVM Stack(基本数据类型、对象引用)
//Native Method Stack(本地方法栈)
//方法区
system("pause");
}
*/
//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 myprint() const{
printf("%#x\n",this);
//改变属性的值
//this->name = "yuehang";
//改变this指针的值
//this = (Teacher*)0x00009;
cout << this->name << "," << this->age << endl;
}
void myprint2(){
cout << this->name << "," << this->age << endl;
}
};
void main(){
Teacher t1("jack",20);
const Teacher t2("rose", 18);
//t2.myprint2(); 常量对象只能调用常量函数,不能调用非常量函数
//常函数,当前对象不能被修改,防止数据成员被非法访问
printf("%#x\n", &t1);
t1.myprint();
printf("%#x\n", &t2);
t2.myprint();
system("pause");
}
*/
浅拷贝是值拷贝,因为对象的值为地址,所以拷贝的也就是地址,深拷贝是重新开辟空间,拷贝的是内容
浅拷贝图:
深拷贝:
还没有评论,来说两句吧...