类和对象——封装
C++认为:万事万物皆为对象,对象上有其属性和行为
对象:人;属性:姓名,年龄...;行为:走,跑...
对象:车;属性:车灯,方向盘...;行为:载人,放歌...
具有相同性质的对象,可以抽象为类,如:人属于人类,车属于车类....
封装
意义:
● ①把属性和行为作为一个整体,表现生活中的事务。。
● ②把属性和行为加以权限控制。。
PS:类中的属性和行为统称为成员(属性=成员属性/成员变量)、(行为=成员函数/成员方法)。。
①语法:
例子:设计一个圆类,求圆的周长。
1 #include <iostream> 2 3 using namespace std; 4 5 const double PI = 3.14; 6 7 //设计一个圆类,求圆的周长 8 //周长公式:2*PI*半径(圆的一个属性) 9 10 class Yuan{ 11 12 //访问权限 13 public: //公共访问权限 14 15 //属性 16 int m_r; 17 18 //行为 19 //获取圆的周长 20 double calculateZC(){ 21 return 2 * PI * m_r; 22 } 23 }; 24 25 int main(){ 26 27 //通过圆类,创建具体的圆(对象) 28 //实例化:通过一个类,创建一个对象的过程 29 Yuan c1; //c1为对象名 30 //给圆对象的属性进行赋值 31 c1.m_r = 10; 32 33 //2 * PI * 10 = 62.8 34 cout << "圆的周长为:" << c1.calculateZC() << endl; 35 36 system("pause"); 37 return 0; 38 }
练手:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Student{ 7 public: 8 string name; 9 string number; 10 void print(){ 11 cout << "姓名:" << name << endl; 12 cout << "学号:" << number << endl; 13 } 14 }; 15 16 int main(){ 17 Student S1; 18 S1.name = "你好"; 19 S1.number = "5281000"; 20 S1.print(); 21 system("pause"); 22 return 0; 23 }
=======================
当然还可以通过类的行为给类的属性赋值:
如下:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Student{ 7 public: 8 string m_name; 9 string number; 10 void print(){ 11 cout << "姓名:" << m_name << endl; 12 } 13 void setName(string name){ 14 m_name = name; 15 } 16 }; 17 18 int main(){ 19 Student S1; 20 S1.setName("你好"); 21 S1.print(); 22 system("pause"); 23 return 0; 24 }
②封装权限:
类在设计的时候,可以把属性和行为放在不同的权限下,加以控制。。。
权限分三类:㊀public公共权限、㊁protected保护权限、㊂private私有权限
㊀公共权限:类内、类外都可以访问
㊁保护权限:类内可以访问、类外不可以访问、子类能访问父类
㊂私有权限:类内可以访问、类外不可以访问、子类不能访问父类
例子:
1 #include <iostream> 2 3 using namespace std; 4 5 //三类权限:public、protected、private 6 7 class Person{ 8 public: 9 string m_Name; 10 protected: 11 string m_Car; 12 private: 13 int m_Password; 14 15 public: 16 void func(){ 17 m_Name = "我"; 18 m_Car = "奔驰"; 19 m_Password = 551133; 20 } 21 }; 22 23 int main(){ 24 Person p1; 25 p1.m_Name = "欸嘿"; 26 //p1.m_Car = "66"; 27 //p1.m_Password =565333; 28 system("pause"); 29 return 0; 30 }
关于p1.m_car和p1.m_Password在类外是不可访问的
void func()函数由于是public权限,因此也可以在int main()中访问,但是若改为protected或者private权限,那么void func()在int main()中也变得不可访问。
struct和class的区别
唯一区别:
● struct默认权限为公共
● class默认权限为私有
例子:
成员私有化
优点1:把所有成员属性设置为私有,可以自己控制读取权限。
优点2:对于写权限,可以检测数据的有效性。。
相当于把属性私有化,提供公共的行为来对私有化的属性进行操作(读写,不可读不可写之类的)。。
同时也可以写判断来过滤无效数据。。
例子:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Person{ 7 private: 8 string m_Name; //可读可写 9 int m_Age; //只读 10 string m_Phone; //只写 11 public: 12 void setName(string name){ //写 13 m_Name = name; 14 } 15 16 string getName(){ //读 17 return m_Name; 18 } 19 20 int getAge(){ //读 21 return m_Age; 22 } 23 24 void setPhone(string phone){ 25 m_Phone = phone; 26 } 27 }; 28 29 int main(){ 30 Person p; 31 p.setName("欸嘿"); 32 cout << "姓名:" << p.getName() << endl; 33 cout << "年龄:" << p.getAge() << endl; 34 p.setPhone("45555"); 35 system("pause"); 36 return 0; 37 }
练手1:设计一个立方体类,求出立方体的面积和体积,利用全局函数和成员函数判断两个立方体是否相等。
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Cube{ 7 private: 8 double m_L; 9 double m_W; 10 double m_H; 11 public: 12 void setLWH(double L, double W, double H){ 13 if(L<=0 && W<=0 && H<=0){ 14 return; 15 } 16 m_L = L; 17 m_W = W; 18 m_H = H; 19 } 20 21 double *getLWH1(){ 22 static double arr1[] = {m_L ,m_W ,m_H}; 23 return arr1; 24 } 25 26 double *getLWH2(){ 27 static double arr2[] = {m_L ,m_W ,m_H}; 28 return arr2; 29 } 30 31 double mianji(){ 32 return (m_H*m_L + m_H*m_W + m_L*m_W)*2; 33 } 34 35 double tiji(){ 36 return m_H*m_L*m_W; 37 } 38 39 bool isSameByclass(Cube &c){ //利用成员函数判断两个立方体是否相同 40 //此时只需要传入一个需要比较的对象即可进行比较 41 if(m_L == c.m_L && m_W == c.m_W && m_H == c.m_H){ 42 return true; 43 } 44 return false; 45 } 46 }; 47 48 //全局函数判断俩个立方体是否相等 49 bool isSame(Cube &f, Cube &s){ 50 double *arr1 = f.getLWH1(); 51 double *arr2 = s.getLWH2(); 52 53 for(int i = 0 ; i<3 ; i++){ 54 if(arr1[i] != arr2[i]){ 55 return false; 56 } 57 } 58 return true; 59 } 60 61 int main(){ 62 Cube c1; 63 c1.setLWH(10.0 ,10.0 ,10.0); 64 cout << "c1的面积为:" << c1.mianji() <<endl; 65 cout << "c1的体积为:" << c1.tiji() <<endl; 66 Cube c2; 67 c2.setLWH(1.0,10.0,10.0); 68 cout << "c2的面积为:" << c2.mianji() <<endl; 69 cout << "c2的体积为:" << c2.tiji() <<endl; 70 71 // bool same = isSame(c1,c2); 72 // if(same){ 73 // cout << "c1和c2是同一个立方体" << endl; 74 // } 75 // else{ 76 // cout << "c1和c2不是同一个立方体" << endl; 77 // } 78 79 bool ret = c1.isSameByclass(c2); 80 if(ret){ 81 cout << "c1和c2是同一个立方体" << endl; 82 } 83 else{ 84 cout << "c1和c2不是同一个立方体" << endl; 85 } 86 87 system("pause"); 88 return 0; 89 }
提供了俩类方法
其中全局函数判断采用了数组返回(return只能返回一个东西QAQ),由于学艺不精,只能采用不同函数附带不同的static的数组来保存,再进行比较..
——————————————
练手2:设计一个圆类和一个点类,计算圆和点的关系(园内,圆上,园外,圆心):

1 #include <iostream> 2 #include <math.h> 3 4 using namespace std; 5 6 class Point{ 7 private: 8 int m_x; 9 int m_y; 10 11 public: 12 void setX(int x){ 13 m_x = x; 14 } 15 void setY(int y){ 16 m_y = y; 17 } 18 19 int getX(){ 20 return m_x; 21 } 22 int getY(){ 23 return m_y; 24 } 25 }; 26 27 class Cirecle{ 28 private: 29 int m_R; 30 Point m_Center; 31 32 public: 33 void setR(int r){ 34 m_R = r; 35 } 36 void setCenter(Point center){ 37 m_Center = center; 38 } 39 40 int getR(){ 41 return m_R; 42 } 43 Point getCenter(){ 44 return m_Center; 45 } 46 }; 47 48 void isInCircle(Cirecle &c, Point &p){ //pow是平方函数,保存在头文件math.h中 49 int distance = pow((c.getCenter().getX() - p.getX()), 2) + pow((c.getCenter().getY() - p.getY()), 2); 50 int rDistance = pow(c.getR(),2); 51 52 if(distance == rDistance){ 53 cout << "点在圆上" << endl; 54 } 55 else if(distance > rDistance){ 56 cout << "点在圆外" << endl; 57 } 58 else{ 59 cout << "点在圆内" << endl; 60 } 61 62 } 63 64 int main(){ 65 Cirecle c; 66 c.setR(10); 67 68 Point center; 69 center.setX(10); 70 center.setY(0); 71 c.setCenter(center); 72 73 Point p; 74 p.setX(10); 75 p.setY(10); 76 77 isInCircle(c,p); 78 79 system("pause"); 80 return 0; 81 }
核心:
①在一个类中,让另一个类作为本类中的成员存在。。
PS:可以把类中的属性和行为分开(头文件引入方式),减少单个文件的臃肿程度。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具