代码保存
//1、构造函数的分类及调用
//分类
// 按照参数分类 无参构造(默认构造)和有参构造
// 按照类型分类 普通构造函数和拷贝构造函数
/*
class person
{
public:
//构造函数
person()
{
cout << "person默认构造函数的调用" << endl;
}
person(int a)
{
age = a;
cout << "person有参构造函数的调用" << endl;
}
//拷贝构造函数
person(const person &p)
{
//将传入的人身上的所有属性,拷贝到我身上
age = p.age;
cout << "person拷贝构造函数的调用" << endl;
}
//析构函数
~person()
{
cout << "person析构函数的调用" << endl;
}
int age;
};
void test01()
{
//1、括号法
//person p;//默认构造函数的调用
//person p2(10);//有参构造函数
//person p3(p2);//拷贝构造函数的调用
//cout << "p2的年龄:" << p2.age << endl;
//cout << "p3的年龄:" << p3.age << endl;
//注意事项1
//默认构造函数的时候,不要加()
//因为下面这行代码,编译器会认为这是一个函数声明,不会认为在创建对象
//person p1();
//2、显示法
person p1;
person p2 = person(10);//有参构造
person p3 = person(p2);//拷贝构造
//person(10)匿名对象
//注意事项2
//不要利用拷贝构造函数,初始化匿名对象
//person(p3);
//3、隐式转换法
person p4 = 10;//person p4=person(10)
person p5 = p4;//person p5=person(p4)
}
int main()
{
test01();
return 0;
}
*/
//拷贝构造函数的调用时机
//1、使用一个已经创建完毕的对象来初始化下一个对象
//2、值传递的方式给函数的参数传值
//3、值方式返回局部对象
/*
class person
{
public:
person()
{
cout << "person构造函数的调用" << endl;
}
person(int age)
{
m_age = age;
cout << "person有参构造函数的调用" << endl;
}
person(const person& p)
{
m_age = p.m_age;
cout << "person拷贝构造函数的调用" << endl;
}
~person()
{
cout << "person析构函数的调用" << endl;
}
int m_age;
};
//1、使用一个已经创建完毕的对象来初始化下一个对象
void test1()
{
person p1(20);
person p2(p1);
cout << "p2的年龄:" << p2.m_age << endl;
}
//2、值传递的方式给函数的参数传值
void dowork(person p)
{}
void test2()
{
person p;
dowork(p);
}
//3、值方式返回局部对象
person dowork2()
{
person p1;
return p1;
}
void test3()
{
person p = dowork2();
}
int main()
{
//test1();
//test2();
test3();
return 0;
}
*/
//构造函数的调用规则
//1、创建一个类,c++编译器会为每个类都添加至少3个函数
// 默认构造函数(空实现)
// 析构函数(空实现)
// 拷贝构造(值拷贝)
//2、当我们写了有参构造函数,编译器不会自动提供默认构造
// 当我们写了拷贝构造函数,编译器不会提供其他构造函数
/*
class person
{
public:
person()
{
cout << "person构造函数的调用" << endl;
}
person(int age)
{
m_age = age;
cout << "person有参构造函数的调用" << endl;
}
//系统自动实现拷贝构造函数 自动实现拷贝操作
//person(const person& p)
//{
// m_age = p.m_age;
// cout << "person拷贝构造函数的调用" << endl;
//}
~person()
{
cout << "person析构函数的调用" << endl;
}
int m_age;
};
void test1()
{
person p;
p.m_age = 18;
person p2(p);
cout << "p2的年龄为:" << p2.m_age << endl;
}
void test2()
{
//person p;//当写了有参构造函数,编译器不会自动实现无参构造
}
int main()
{
//test1();
test2();
}
*/
//深拷贝和浅拷贝
//使用编译器提供的拷贝构造函数,会做浅拷贝操作
//浅拷贝带来的问题是堆区的内存重复操作
//浅拷贝带来的问题,要利用深拷贝来解决
/*
class person
{
public:
person()
{
cout << "person构造函数的调用" << endl;
}
person(int age,int height)
{
m_age = age;
m_height = new int(height);
cout << "person有参构造函数的调用" << endl;
}
//自己实现拷贝构造函数,解决浅拷贝的问题
person(const person& p)
{
m_age = p.m_age;
//m_height=p.m_height;编译器默认实现的就是这行代码
//深拷贝操作
m_height = new int(*p.m_height);
cout << "person拷贝构造函数的调用" << endl;
}
//析构函数作用是释放堆区内存
~person()
{
//释放内存
if (m_height != NULL)
{
delete m_height;
m_height = NULL;
}
cout << "person析构函数的调用" << endl;
}
int m_age;
int* m_height;
};
void test1()
{
//使用编译器提供的拷贝构造函数,会做浅拷贝操作
//浅拷贝带来的问题是堆区的内存重复操作
//浅拷贝带来的问题,要利用深拷贝来解决
person p1(18,160);
cout << "p1的年龄:" << p1.m_age << endl;
cout << "p1的身高:" << *p1.m_height << endl;
person p2(p1);
cout << "p2的年龄:" << p2.m_age << endl;
cout << "p2的身高:" << *p2.m_height << endl;
}
int main()
{
test1();
return 0;
}
*/
//初始化列表
/*
class person
{
public:
//传统初始化操作
//person(int a,int b,int c)
//{
// m_a = a;
// m_b = b;
// m_c = c;
//}
//初始化列表初始化属性
person(int a,int b,int c) :m_a(a), m_b(b), m_c(c){}
int m_a;
int m_b;
int m_c;
};
void test1()
{
//person p(10, 20, 30);
person p(30,20,10);
cout << "m_a: " << p.m_a << endl
<< "m_b: " << p.m_b << endl
<< "m_c: " << p.m_c << endl;
}
int main()
{
test1();
return 0;
}
*/
//在类中使用另一个类
/*
class phone
{
public:
phone(string pname)
{
cout << "phone" << endl;
m_pname = pname;
}
~phone()
{
cout << "phone析构函数" << endl;
}
string m_pname;
};
class person
{
public:
person(string name, string pname) :m_name(name), m_phone(pname)
{
cout << "person" << endl;
}
~person()
{
cout << "person析构函数" << endl;
}
//姓名
string m_name;
//手机
phone m_phone;
};
void test1()
{
person p("zhangsan", "pingguomax");
cout << "name: " << p.m_name << endl
<< "phone: " << p.m_phone.m_pname << endl;
}
int main()
{
test1();
return 0;
}
*/
//静态成员变量
//1、所有对象都共享同一份数据
//2、编译阶段就分配内存
//3、类内声明,类外初始化操作
/*
class person
{
public:
//1、所有对象都共享同一份数据
//2、编译阶段就分配内存
//3、类内声明,类外初始化操作
static int m_a;
//静态成员变量也是有访问权限的
private:
static int m_b;
};
int person::m_a = 100;
int person::m_b = 200;
void test1()
{
person p;
cout << p.m_a << endl;
person p2;
p2.m_a = 200;
cout << p.m_a << endl;
}
void test2()
{
//静态成员变量,不属于某个对象上,所有对象共享同一份数据
//因此静态成员变量有两种访问方式
//1、通过对象访问
person p;
cout << p.m_a << endl;
//2、通过类名的方式访问
cout << person::m_a << endl;
//cout << person::m_b << endl;权限不够,无法访问
}
int main()
{
//test1();
test2();
return 0;
}
*/
//静态成员函数
//1、所有对象共享同一个函数
//1、静态成员函数只能访问静态成员变量
/*
class person
{
public:
static void func()
{
m_a = 100;//静态成员变量只能访问静态成员变量
//m_b = 200;//静态成员变量不能访问非静态成员变量
cout << "static void func调用" << endl;
}
static int m_a;
int m_b;
静态成员函数也是又访问权限的
private:
static void func2()
{
cout << "static void func2调用" << endl;
}
};
int person::m_a = 100;
void test1()
{
//1、通过对象访问
person p;
p.func();
//2、通过类名访问
person::func();
//person::fun2();类外访问不到私有静态成员函数
}
int main()
{
test1();
return 0;
}
*/
//成员变量和成员函数是分开存储的
/*
class person
{
int m_a;//1
static int m_b;//0
void func(){}//0
static void func2(){}//0
};
int person::m_b = 100;
void test1()
{
//空对象占用内存空间为:1
//c++编译器会为每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
person p;
cout << "sizeof(p) = " << sizeof(p) << endl;
}
void test2()
{
person p;
cout << "sizeof(p) = " << sizeof(p) << endl;
}
int main()
{
//test1();
test2();
return 0;
}
*/
//this指针
//1、解决名称冲突
//2、返回对象本身
/*
class person
{
public:
person(int age)
{
//this 指针指向的是被调用的成员函数所属的对象
this->age = age;
}
person& personaddage(person& p)
{
this->age += p.age;
return *this;
}
int age;
};
//1、解决名称冲突
void test1()
{
person p1(18);
cout << "p1_age: " << p1.age << endl;
}
//2、返回对象本身
void test2()
{
person p1(10);
person p2(10);
p2.personaddage(p1).personaddage(p1);
cout << "p2_age: " << p2.age << endl;
}
int main()
{
//test1();
test2();
return 0;
}
*/
//空指针访问成员
/*
class person
{
public:
void showclsaaname()
{
cout << "this is person class" << endl;
}
void showpersonage()
{
if (this == NULL)
return;
//报错原因是因为传入的指针是为NULL
cout << "age= " << m_age << endl;
}
int m_age;
};
void test1()
{
person* p = NULL;
p->showclsaaname();
p->showpersonage();
}
int main()
{
test1();
return 0;
}
*/
//常函数和常对象
/*
class person
{
public:
//this指针的本质是指针常量,指针的指向是不能修改的
void showperson() const//常函数
{
//m_a = 100;
m_b = 100;
//this = NULL;//this指针的指向不能修改指向
}
void func()
{
}
int m_a;
mutable int m_b;//特殊变量,在常函数中也可以修改这个值
};
void test2()
{
const person p;//在对象前加const ,变成常对象
//p.m_a = 100;
p.m_b = 100;//特殊变量,在常对象中也可以修改这个值
//常对象只能调用常函数
//p.func();
p.showperson();
}
int main()
{
test2();
return 0;
}*/
//友元
//全局函数做友元
/*
class building //建筑物类
{
//goodgay全局函数是builging的友元
friend void goodgay(building* building);
public:
building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
public:
string m_sittingroom;//客厅
private:
string m_bedroom;//卧室
};
//全局函数
void goodgay(building* building)
{
cout << "好基友的全局函数 正在访问:" << building->m_sittingroom << endl;
cout << "好基友的全局函数 正在访问:" << building->m_bedroom << endl;
}
void test1()
{
building building;
goodgay(&building);
}
int main()
{
test1();
return 0;
}
*/
//类做友元
/*
class Building;
class Goodgay
{
public:
Goodgay();
void visit();// 参观函数,访问building中的属性
Building* building;
};
class Building //建筑物类
{
//Goodgay是Builging的友元,可以访问本类中的私有成员
friend class Goodgay;
//goodgay全局函数是Builging的友元
//friend void goodgay(Building* building);
public:
Building();
public:
string m_sittingroom;//客厅
private:
string m_bedroom;//卧室
};
//类外写成员函数
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
Goodgay::Goodgay()
{
//创建建筑物对象
building = new Building;
}
void Goodgay::visit()
{
cout << "好基友类正在访问:" << building->m_sittingroom << endl;
cout << "好基友类正在访问:" << building->m_bedroom << endl;
}
void test1()
{
Goodgay gg;
gg.visit();
}
int main()
{
test1();
return 0;
}
*/
//3、成员函数做友元
/*
class Building;
class Goodgay
{
public:
Goodgay();
void visit();// 可以访问Building中的私有成员
void visit2();//不可以访问Building中的私有成员
Building* building;
};
class Building //建筑物类
{
//告诉编译器 Goodgay 类下的visit成员函数作为本类的好朋友,可以访问私有成员
friend void Goodgay::visit();
public:
Building();
public:
string m_sittingroom;//客厅
private:
string m_bedroom;//卧室
};
//类外写成员函数
Building::Building()
{
m_sittingroom = "客厅";
m_bedroom = "卧室";
}
Goodgay::Goodgay()
{
//创建建筑物对象
building = new Building;
}
void Goodgay::visit()
{
cout << "visit正在访问:" << building->m_sittingroom << endl;
cout << "visit正在访问:" << building->m_bedroom << endl;
}
void Goodgay::visit2()
{
cout << "visit2正在访问:" << building->m_sittingroom << endl;
//cout << "visit2正在访问:" << building->m_bedroom << endl;
}
void test1()
{
Goodgay gg;
gg.visit();
gg.visit2();
}
int main()
{
test1();
return 0;
}
*/
//运算符重载
//对于内置的数据类型,编译器知道如何运算
//对于自定义的数据类型,要通过自己写成员函数,实现两个对象进行相加
//1、成员函数重载 + 号
//2、全局函数重载 + 号
/*
class person
{
public:
//1、成员函数重载 + 号
//person operator+(person& p)
//{
// person temp;
// temp.m_a = this->m_a + p.m_a;
// temp.m_b = this->m_b + p.m_b;
// return temp;
//}
int m_a;
int m_b;
};
person operator+(person& p1, person& p2)
{
person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
}
person operator+(person& p1, int num)
{
person temp;
temp.m_a = p1.m_a + num;
temp.m_b = p1.m_b + num;
return temp;
}
void test1()
{
person p1;
p1.m_a = 10;
p1.m_b = 10;
person p2;
p2.m_a = 10;
p2.m_b = 10;
person p3 = p1 + p2;
//person p3 = p1.operator+(p2);//成员函数的本质调用
//person p3 = operator+(p1, p2);//全局函数的本质调用
cout << "p3.m_a= " << p3.m_a << endl;
cout << "p3.m_b= " << p3.m_b << endl;
//运算符重载也可以发生函数重载
person p4 = p1 + 100;//person +int
cout << "p4.m_a= " << p4.m_a << endl;
cout << "p4.m_b= " << p4.m_b << endl;
}
int main()
{
test1();
return 0;
}
*/
//左移运算符重载
//只能利用全局函数重载
/*
class person
{
friend ostream& operator<<(ostream& cout, person& p);//本质operator<<(cout,p) 简化cout<<p
public:
person(int a, int b)
{
m_a = a;
m_b = b;
}
private:
int m_a;
int m_b;
};
ostream& operator<<(ostream &cout, person &p)//本质operator<<(cout,p) 简化cout<<p
{
cout << "m_a= " << p.m_a << " m_b= " << p.m_b;
return cout;
}
void test1()
{
person p1(10,10);
//p1.m_a = 10;
//p1.m_b = 10;
cout << p1 << endl;
}
int main()
{
test1();
return 0;
}
*/
//递增运算符重载
/*
class myinteger
{
friend ostream& operator<<(ostream& cout, myinteger& myint);
public:
myinteger()
{
m_num = 0;
}
//重载前置++运算符
myinteger& operator++()//返回引用是为了一直对一个数操作
{
m_num++;
return *this;
}
//重载后置++运算符
myinteger operator++(int)//int 代表占位参数,可以用于区分前置递增和后置递增
{
myinteger temp=*this;
m_num++;
return temp;
}
private:
int m_num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, myinteger& myint)
{
cout << myint.m_num;
return cout;
}
//myinteger& operator++
void test1()
{
myinteger myint;
cout << ++myint << endl;
}
void test2()
{
myinteger myint;
myint++;
cout << myint << endl;
}
int main()
{
//test1();
test2();
return 0;
}
*/