4.1封装
4.1.1封装的意义
4.1.2struct和class的区别
4.1.3成员属性设置为私有
class student
{
public:
//设置姓名
void setname(string name);
//获取姓名
string getname();
int getid();
void setidl(string name);
private:
//属性 姓名学号
string name;//可读可写
int id = 18;//只读
string idl;//只写
};
void student::setname(string n)
{
name = n;
}
string student::getname()
{
return name;
}
int student::getid()
{
return id;
}
void student::setidl(string n)
{
idl = n;
}
int main()
{
student p;
p.setname("张三");
p.setidl("蔡徐坤");
cout << "姓名:" << p.getname()<<endl<< "id="<<p.getid()<< endl;
return 0;
}
4.1.4练习
class cube
{
public:
int l;
int h;
int w;
int s()
{
return h * l * 2 + h * w * 2 + w * l * 2;
}
int v()
{
return l * h * w;
}
//利用成员函数来判断是否相等
//只需要传一个参数
//因为:成员调用这个成员函数,成员自身信息都可直接获取,而另一个对象的信息需要通过引用用获取
bool issamebyclass(cube&c)
{
if (l == c.l && w == c.w && h == c.h)
{
return true;
}
return false;
}
};
bool issame(cube& c1, cube& c2)//使用全局函数来判断是否相等
{
if (c1.l == c2.l && c1.w == c2.w && c1.h == c2.h)
{
return true;
}
return false;
}
int main()
{
cube p1;
p1.h = 1;
p1.l = 1;
p1.w = 1;
cube p2;
p2.h = 2;
p2.l = 2;
p2.w = 2;
cout << "p1的面积"<< p1.s()<<"p1的体积"<<p1.v() <<endl;
cout << "p2的面积"<< p2.s() <<"p2的体积"<< p2.v() <<endl;
bool ret= issame( p1, p2);
if (ret)
{
cout << "p1和p2是相等的" << endl;
}
else
{
cout << "p1和p2是不相等的" << endl;
}
bool rete = p1.issamebyclass(p2);
if (rete)
{
cout << "p1和p2是相等的" << endl;
}
else
{
cout << "p1和p2是不相等的" << endl;
}
return 0;
}
class point
{
public:
void setx(int a);
void sety(int b);
int getx();
int gety();
private:
int x;
int y;
};
void point::setx(int a)
{
x = a;
}
void point::sety(int b)
{
y = b;
}
int point::getx()
{
return x;
}
int point::gety()
{
return y;
}
class circle
{
public:
void setr(int m);
int getr();
void setcenter(point c);
point getcenter();
private:
int r;
point center;//在类中可以让另一个类作为本来的成员
};
void circle::setr(int m)
{
r = m;
}
int circle::getr()
{
return r;
}
void circle::setcenter(point c)
{
center = c;
}
point circle::getcenter()
{
return center;
}
void isincircle(circle& c,point & p)
{
//计算两点间的距离
int distence =
((p.getx() - c.getcenter().getx()) * (p.getx() - c.getcenter().getx()))+
((p.gety() - c.getcenter().gety()) * (p.gety() - c.getcenter().gety()));
//计算半径的平方
int rdistence = c.getr() * c.getr();
if (distence == rdistence)
{
cout << "点在圆上" << endl;
}
else if (distence < rdistence)
{
cout << "点在圆内" << endl;
}
else
{
cout << "点在圆外" << endl;
}
}
int main()
{
circle c;
point center;
point p;
c.setr(10);
//center 必须先初始化出来后才能在圆中使用
center.setx(0);
center.sety(0);
//也就是这一步中的center必须在前面初始化完成它的属性
c.setcenter(center);
p.setx(5);
p.sety(5);
isincircle(c, p);
return 0;
}
4.2对象的初始化和清理![](https://i-blog.csdnimg.cn/blog_migrate/9dc18722dee9c301493e47649433f675.png)
4.2.1构造函数和析构函数
![](https://i-blog.csdnimg.cn/blog_migrate/1874ba347e8982215b3d926ce844253b.png)
class person
{
public:
//1.构造函数
person()
{
cout << "person构造函数的调用" << endl;
}
//2.析构函数
~person()
{
cout << "person析构函数的调用" << endl;
}
};
//构造和析构函数都是必须有的实现,如果我们不提供,编译器会提供一个空实现的构造和析构
void test01()
{
person p;//在栈上的数据,test01执行完毕后,释放这个对象
}
int main()
{
test01();
return 0;
}
4.2.2构造函数的分类及调用
//1.构造函数的分类及调用
//分类
//按参数分类:有参构造 无参构造(默认构造函数)
//按照类型分类:普通构造函数 拷贝构造函数
class person
{
public:
//1.构造函数
person()
{
cout << "person无参构造函数的调用" << endl;
}
person(int a)
{
age = a;
cout << "person有参构造函数的调用" << endl;
}
//拷贝构造函数
person(const person& p)
{
//将传入的这个人身上的所有属性,拷贝到我身上
age = p.age;
cout << "person拷贝构造函数的调用" << endl;
}
//2.析构函数
~person()
{
cout << "person析构函数的调用" << endl;
}
int age;
};
//调用
void test01()
{
//1.括号法
person p;//默认构造函数
person p2(10);//有参构造函数
person p3(p2);//拷贝构造函数
//注意事项:
//1.调用默认构造函数的时候不要加()
//2.不要利用拷贝构造函数 初始化匿名对象
cout << "p2的年龄为:" << p2.age << endl;
cout << "p3的年龄为:" << p3.age << endl;
//下面这个函数编译器会默认为函数声明,不会认为是在创建对象
//person p1();
//2.显示法
person a1;
person a2 = person(10);
person a3 = person(a2);
person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象
//不要利用拷贝构造函数 初始化匿名对象 编译器会认为person(p3)======person p3;
//person(p2);
//3.隐式转换法
person p4 = 10;//相当于 写了person pe4=person(10);
person p5 = p4;
}
int main()
{
test01();
return 0;
}
4.2.3拷贝构造函数的调用时机
//拷贝构造函数调用时机
//1.使用一个已经创建完毕的对象来初始化一个新对象
//2.值传递的方式给函数参数传值
//3.值方式返回局部对象
class person
{
public:
//1.构造函数
person()
{
cout << "person无参构造函数的调用" << endl;
}
person(int a)
{
age = a;
cout << "person有参构造函数的调用" << endl;
}
//拷贝构造函数
person(const person& p)
{
//将传入的这个人身上的所有属性,拷贝到我身上
cout << "person拷贝构造函数的调用" << endl;
age = p.age;
}
//2.析构函数
~person()
{
cout << "person析构函数的调用" << endl;
}
int age;
};
//拷贝构造函数调用时机
//1.使用一个已经创建完毕的对象来初始化一个新对象
void test01()
{
person p1(20);
person p2(p1);
cout << "p2的年龄" << p2.age << endl;
}
//2.值传递的方式给函数参数传值
void dowork(person p)
{
}
void test02()
{
person p;
dowork(p);
}
//3.值方式返回局部对象
person dowork02()
{
person p1;
cout << (int*)&p1 << endl;
return person (p1);
}
void test03()
{
person p = dowork02();
cout << (int*)&p << endl;
}
int main()
{
//test01();
//test02();
test03();
return 0;
}
4.2.4构造函数的调用规则
4.2.5深拷贝与浅拷贝
//深拷贝与浅拷贝
class person
{
public:
//1.构造函数
person()
{
cout << "person无参构造函数的调用" << endl;
}
person(int a,int h)
{
age = a;
height=new int(h);
cout << "person有参构造函数的调用" << endl;
}
//2.析构函数
~person()
{
//析构函数,将堆区开辟数据做释放操作
if (height != NULL)
{
delete height;
height = NULL;
}
cout << "person析构函数的调用" << endl;
}
//自己实现拷贝构造函数 解决浅拷贝带来的问题
person(const person& p)
{
cout << "person拷贝构造函数的调用" << endl;
age = p.age;
//深拷贝操作
height=new int(*p.height);
}
int age;
int* height;
};
void test01()
{
person p1(18,160);
cout << "p1的年龄为多大:" << p1.age << "身高为:"<<*p1.height << endl;
person p2(p1);
cout << "p1的年龄为多大:" << p1.age << "身高为:" <<*p2.height << endl;
}
int main()
{
test01();
return 0;
}
4.2.6初始化列表
class person
{
public:
int m_a;
int m_b;
int m_c;
//传统初始化操作
/*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)
{}
};
void test01()
{
//person p(10, 20, 30);
person p(30,20,10);
cout << "m_a=" << p.m_a << endl;
cout << "m_b=" << p.m_b << endl;
cout << "m_c=" << p.m_c << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.2.7类对象作为类成员
class phone
{
public:
//手机品牌名称
string m_pname;
phone(string pname)
{
m_pname = pname;
}
};
class person
{
public:
//phone m_phone=pname; 隐式转换法
person(string name, string pname) :m_name(name), m_phone(pname)//phone m_phone=pname
{
}
//姓名
string m_name;
//手机
phone m_phone;
};
void test01()
{
person p("张三", "苹果MAX");
cout << p.m_name << "拿着:" << p.m_phone.m_pname << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.2.8静态成员
静态成员变量
class Person
{
public:
//1.所有对象都共同享用同一份数据
//2.编译阶段就分配内存
//3.类内声明,类外初始化操作
static int m_A;
//静态成员变量也是有访问权限的
private:
static int m_B;
};
int Person::m_A=100;
void test01()
{
Person p;
cout << p.m_A << endl;
Person p2;
p2.m_A = 200;
cout << p.m_A << endl;
}
void test02()
{
//静态成员变量 不属于某一个对象上,所有对象都共享同一份数据
//因此静态成员变量有两种访问方式
//1.通过对象进行访问
Person p;
cout << p.m_A << endl;
//2.通过类名进行访问
cout << Person::m_A << endl;
//cout << Person::m_b << endl;//类外访问不到私有静态成员变量
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
静态成员函数
class Person
{
public:
//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量
static void func()
{
//m_B = 200;//静态成员函数不可以访问 非静态成员变量,无法区分到底是哪个对象的m_B的属性
m_A = 100;//静态成员函数可以访问 静态成员变量
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;
void test02()
{
//静态成员函数 两种访问方式
//1.通过对象进行访问
Person p;
p.func();
//2.通过类名进行访问
Person::func();
//Person::func2();//类外访问不到私有静态成员函数
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
4.3C++对象模型和this指针
4.3.1成员变量和成员函数分开存储
//成员变量 和成员函数是分开存储的
class Person
{
int m_A;//非静态成员变量 属于类的对象上的数据
static int m_B;//静态成员变量 不属于类的对象上
void func(){}//非静态成员函数 不属于类的对象上
static void func2(){}//静态成员函数 不属于类的对象上
};
int Person::m_B;
void test01()
{
Person p;
//空对象占用的内存空间为: 1
//C++编译器会给每个空对象也分配字节空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "size of p =" << sizeof(p) << endl;
}
void test02()
{
Person p1;
//
cout << "size of p =" << sizeof(p1) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
4.3.2 this指针概念
class Person
{
public:
Person(int age)
{
//this 指针指向的是 被调用的成员函数所属的对象
this->age = age;
}
Person& PersonAddAge(Person& p)//返回它的本体需要使用引用的方式作为返回
{
this->age += p.age;
//this 是指向p2的指针,而*p2就是这个对象本体
return *this;
}
int age;
};
//1.解决名称冲突
void test01()
{
Person p1(18);
cout << "p1的年龄为:" << p1.age << endl;
}
//2.返回对象本身用*this
void test02()
{
Person p1(10);
Person p2(10);
//链式编程思想
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为:" << p2.age << endl;
}
int main()
{
//test01();
test02();
return 0;
}
4.3.3空指针访问成员函数
class Person
{
public:
void showClassName()
{
cout << "this Person class" << endl;
}
void showClassAge()
{
//报错原因是因为传入的指针为NULL
if (this==NULL)
{
return;
}
cout << "age=" <<m_age<< endl;
}
int m_age;
};
void test01()
{
Person* p = NULL;
p->showClassName();
p->showClassAge();
}
int main()
{
test01();
//test02();
return 0;
}
4.3.4const修饰成员函数
//常函数
class Person
{
public:
//this指针的本质 是指针常量 指针的指向是不可以修改的
//const Person * const this;
//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
void ShowPerson()const
{
//this->m_A = 100;
//this->NULL;//this指针不可以修改指针的指向的
}
void func()
{}
int m_A;
mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值,加关键字mutable
};
void test01()
{
Person p;
p.ShowPerson();
}
//常对象
void test02()
{
const Person p;//在对象前加const,变为常对象
//p.m_A=100; 不可修改
p.m_B = 100; //特殊值,在常对象下也可以修改
//常对象只能调用常函数
p.ShowPerson();
//p.func();//常对象 不可以调用普通成员函数,因为普通成员函数可以修改属性
}
int main()
{
test01();
//test02();
return 0;
}
4.4 友元
4.4.1全局函数做友元
//建筑物类
class Building
{
//goodGay全局函数是Building的好朋友,可以访问Building中私有成员
friend void goodGay(Building& building);
public:
string m_SettingRoom;
Building()
{
m_SettingRoom = "客厅";
m_BedRoom = "卧室";
}
private:
string m_BedRoom;
};
//全局函数
void goodGay(Building& building)
{
cout << "好基友的全局函数正在访问:"<<building.m_SettingRoom << endl;
cout << "好基友的全局函数正在访问:" << building.m_BedRoom << endl;
}
void test01()
{
Building building;
goodGay(building);
}
int main()
{
test01();
system("pause");
return 0;
}
4.4.2类做友元(代码有点问题)
class Building;
class GoodGay
{
public:
Building* building;
public:
GoodGay()
{
//创建建筑物对象
building = new Building;
}
void visit()
{
cout << "好基友类正在访问:" << building->m_SettingRoom << endl;
cout << "好基友类正在访问:" << building->m_BedRoom << endl;
}
};
class Building
{
//GoodGay类是本来的好朋友,可以访问本类中私有成员
friend class GoodGay;
public:
string m_SettingRoom;
Building()
{
m_SettingRoom = "客厅";
m_BedRoom = "卧室";
}
private:
string m_BedRoom;
};
void test01()
{
GoodGay gg;
gg.visit();
}
int main()
{
test01();
system("pause");
return 0;
}
4.4.3成员函数做友元
class Building;
class GoodGay
{
public:
GoodGay();
void visit();//让visit函数可以访问Building中私有成员
void visit2();//让visit2函数不可以访问Building中私有成员
Building* building;
};
class Building
{
public:
//告诉编译器 GoodGay中的visit()函数作为本类的好朋友,可以访问私有成员
friend void GoodGay::visit2();
Building();
string m_SettingRoom;//客厅
private:
string m_BedRoom;//客厅
};
//类外实现成员函数
Building::Building()
{
m_SettingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()//让visit函数可以访问Building中私有成员
{
cout << "visit函数正在访问" << building->m_SettingRoom << endl;
}
void GoodGay::visit2()
{
cout << "visit函数正在访问" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
gg.visit2();
}
int main()
{
test01();
system("pause");
return 0;
}
4.5运算符重载
4.5.1加号运算符重载
![](https://i-blog.csdnimg.cn/blog_migrate/8d61106ae976c5e22e89803053fd143b.png)
4.5.2左移运算符重载![](https://i-blog.csdnimg.cn/blog_migrate/5ad8eea8d695652ce51a4d1172383ce2.png)
//左移运算符重载
class Person
{
friend ostream& operator<<(ostream& out, Person& p);
public:
Person(int a, int b)
{
m_A=a;
m_B=b;
}
private:
//利用成员函数重载 左移运算符 简化版 p << cout
//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
//只能利用全局函数重载左移运算符
/*void operator<< ( cout )
{
}*/
int m_A;
int m_B;
};
//利用全局函数重载左移运算符
ostream& operator<<(ostream& out,Person& p)//本质 operator<<(cout,p) 简化为cout<<p
{
out << "m_A=" << p.m_A << "m_B=" << p.m_B ;
return cout;
}
void test01()
{
Person p(10,10);
cout << p<<endl;
/*cout << "p3.m_A= " << p1.m_A << endl;
cout << "p3.m_B= " << p1.m_B << endl;
cout << "p4.m_A= " << p1.m_A << endl;
cout << "p4.m_B= " << p1.m_B << endl;*/
}
int main()
{
test01();
return 0;
}
4.5.3递增运算符重载
//递增运算符重载
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num=0;
}
//重载前置++运算符 返回引用是为了一直对一个是数据进行递增操作
MyInteger& operator++()
{
//先进行++运算
m_Num++;
//再将自身做返回
return *this;
}
//重载后置++运算符
//int 代表占位参数,可以用于区分前置和后置递增
MyInteger operator++ (int)
{
//先 记录当时结果
MyInteger temp = *this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
test02();
return 0;
}
4.5.4赋值运算符重载
class Person
{
public:
Person(int age)
{
m_Age=new int(age);
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//重载赋值运算符
Person& operator=(Person& p)
{
//编译器提供的浅拷贝
//应该先判断是否有属性在堆区,如果有先释放感觉,然后在深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3=p2 = p1;//赋值操作
cout <<"p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main()
{
test01();
return 0;
}
4.5.5关系运算符重载
class Person
{
public:
Person(int age,string name)
{
m_Age = age;
m_Name=name;
}
//重载关系运算符==
bool operator!=(Person& p)
{
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
return true;
}
else
return false;
}
bool operator==(Person& p)
{
if (this->m_Age != p.m_Age || this->m_Name != p.m_Name)
{
return true;
}
else
return false;
}
int m_Age;
string m_Name;
};
void test01()
{
Person p1(18,"Tom");
Person p2(18,"Gerry");
if (p1==p2)
{
cout << "p1和p2是相等的!" << endl;
}
else
cout << "p1和p2是不相等的!" << endl;
if (p1 != p2)
{
cout << "p1和p2是不相等的!" << endl;
}
else
cout << "p1和p2是相等的!" << endl;
}
int main()
{
test01();
return 0;
}
4.5.6函数调用运算符重载
4.6 继承
4.6.1继承的基本语法
4.6.2继承方式
4.6.3继承中的对象模型
4.6.4 继承中构造和析构的顺序
4.6.5同名成员处理
4.6.6同名静态成员处理
4.6.7继承语法
4.6.8菱形继承问题及解决办法
4.7多态
4.7.1多态的基本语法
4.7.2 多态案例——计算机类
4.7.3纯虚函数和抽象类![](https://i-blog.csdnimg.cn/blog_migrate/f7211299485ea079d3562d56787931b6.png)
4.7.4多态案例二制作饮品
4.7.5虚析构和纯虚析构
4.7.8多态案例三——电脑组装