随笔 - 110,  文章 - 0,  评论 - 0,  阅读 - 18445

C++面向对象的三大特性为:==封装、继承、多态==

C++认为==万事万物都皆为对象==,对象上有其属性和行为

例如:

​ 人可以作为对象,属性有姓名、年龄、身高、体重...,行为有走、跑、跳、吃饭、唱歌...

​ 车也可以作为对象,属性有轮胎、方向盘、车灯...,行为有载人、放音乐、放空调...

​ 具有相同性质的==对象==,我们可以抽象称为==类==,人属于人类,车属于车类

 

4.1.1 封装的意义

封装是C++面向对象三大特性之一

封装的意义:

  • 将属性和行为作为一个整体,表现生活中的事物
  • 将属性和行为加以权限控制

封装意义一:

​ 在设计类的时候,属性和行为写在一起,表现事物

语法: class 类名{ 访问权限: 属性 / 行为 };

复制代码
 1 #include <iostream>
 2 using namespace std;
 3 const double pi = 3.14;
 4 class yuan
 5 {
 6     //访问权限
 7     //公共权限
 8 public:
 9     //属性
10     //半径
11     int r;
12     //行为
13     //获取圆的周长
14     double calculate()
15     {
16         return 2 * pi * r;
17     }
18 };
19 int main()
20 {
21     yuan yuan1;//通过圆类,创建圆的对象,实例化 (通过一个类 创建一个对象)
22 cout << "请输入圆的半径" << endl; 
23 cin >> yuan1.r ;
24 cout << "半径为" << yuan1.r << "的圆面积为 :" <<yuan1.calculate();
25 return 0;
26 }
复制代码

 学生类

复制代码
 1 // **示例2:**设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
 2 #include <iostream>
 3 #include <string.h>
 4 using namespace std;
 5 class student
 6 {
 7 // 类中的属性与行为 我们统一称为 成员
 8 // 属性 成员属性 成员变量
 9 // 行为 成员函数 成员方法
10 //属性
11 public:
12     string NAME;
13     int ID;
14 //行为
15     void showStudent(string name,int id)
16     {
17         cout << "学生姓名为:" << name << endl
18         << "学生学号为:" << id << endl;
19     }
20 };
21 int main()
22 {
23     class student stu;
24     cout << "please enter the student name :" << endl;
25     cin >> stu.NAME;
26     string name = stu.NAME;
27     cout << "please enter the student ID :" << endl;
28     cin >> stu.ID;
29     int id = stu.ID;
30     stu.showStudent(name,id);
31 }
复制代码

 

 

封装意义与三种权限:

类在设计时,可以把属性和行为放在不同的权限下,加以控制

访问权限有三种:

  1. public 公共权限
  2. protected 保护权限
  3. private 私有权限
复制代码
 1 //三种权限
 2 //公共权限  public     类内可以访问  类外可以访问
 3 //保护权限  protected  类内可以访问  类外不可以访问  子类可以访问父类
 4 //私有权限  private    类内可以访问  类外不可以访问  子类不能访问父类
 5 
 6 class Person
 7 {
 8     //姓名  公共权限
 9 public:
10     string m_Name;
11 
12     //汽车  保护权限
13 protected:
14     string m_Car;
15 
16     //银行卡密码  私有权限
17 private:
18     int m_Password;
19 
20 public:
21     void func()
22     {
23         m_Name = "张三";
24         m_Car = "拖拉机";
25         m_Password = 123456;
26     }
27 };
28 
29 int main() {
30 
31     Person p;
32     p.m_Name = "李四";
33     //p.m_Car = "奔驰";  //保护权限类外访问不到
34     //p.m_Password = 123; //私有权限类外访问不到
35 
36     system("pause");
37 
38     return 0;
39 }
复制代码

 

 

struct和class区别

在C++中 struct和class唯一的区别就在于 默认的访问权限不同

区别:

  • struct 默认权限为公共
  • class 默认权限为私有
复制代码
 1 class C1
 2 {
 3     int  m_A; //默认是私有权限
 4 };
 5 
 6 struct C2
 7 {
 8     int m_A;  //默认是公共权限
 9 };
10 
11 int main() {
12 
13     C1 c1;
14     c1.m_A = 10; //错误,访问权限是私有
15 
16     C2 c2;
17     c2.m_A = 10; //正确,访问权限是公共
18 
19     system("pause");
20 
21     return 0;
22 }
复制代码

成员属性设置为私有

**优点1:**将所有成员属性设置为私有,可以自己控制读写权限

**优点2:**对于写权限,我们可以检测数据的有效性

复制代码
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class student
 5 {
 6 private:
 7     string name;
 8     int age;
 9     string lover;
10 public:
11     void sname(string tname)//可写
12     {
13         name = tname;
14     }
15     string getname()//可读(被使用者读到)
16     {
17         return name;
18     }
19     int ag(int a)//可写又可读(被使用者读到)
20     {
21         age = a;
22         if (age < 0 || age > 150)
23         {
24             cout << "请输入0~150之间的数值" << endl;
25             return 0;
26         }
27         return age;
28     }
29     void slover(string slove)//只写(只可以写入,不可以被外界读到)
30     {
31         lover = slove;
32     }
33 };
34 int main(void)
35 {
36     student student1;
37     student1.sname("二狗");
38     cout << student1.getname() << endl;
39     cout << student1.ag(18) << endl;
40 }
复制代码

 

练习案例1:设计立方体类

设计立方体类(Cube)

求出立方体的面积和体积

分别用全局函数和成员函数判断两个立方体是否相等。

复制代码
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class Cube
 5 {
 6 private :
 7     int m_l;
 8     int m_w;
 9     int m_h;
10 public :
11     void volumn(int l, int w, int h)
12     {
13         m_l = l ;
14         m_w = w;
15         m_h = h;
16     }
17     int ivolumn()
18     {
19         return  m_l * m_w * m_h;
20     }
21     void area(int l, int w, int h)
22     {
23         m_l = l;
24         m_w = w;
25         m_h = h;
26     }
27     int iarea()
28     {
29         return  2 * (m_l * m_w + m_l * m_h + m_w * m_h);
30     }
31     string compare1(Cube& c)//成员函数中的判断
32     {
33         if ((m_l * m_w * m_h) == c.ivolumn() && (2 * (m_l * m_w + m_l * m_h + m_w * m_h) )== c.iarea())
34         {
35             return "两个多边形完全相同";
36         }
37         return "两个多边形不完全相同";
38     }
39 };
40 // 全局函数中的判断
41 string compare(Cube &cube1, Cube &cube2)
42 {
43     if (cube1.ivolumn() == cube2.ivolumn() && cube1.iarea() == cube2.iarea())
44     {
45         return "两个多边形完全相同";
46     }
47     return "两个多边形不完全相同";
48 }
49 int main()
50 {
51    Cube cube1;
52    cube1.volumn(4, 3, 5);
53    cube1.area(4, 3, 5);
54    cout << "cube1's volumn :" << cube1.ivolumn() << endl;
55    cout << "cube1's area :"<<cube1.iarea() << endl;
56    Cube cube2;
57    cube2.volumn(4, 3, 5);
58    cube2.area(4, 3, 5);
59    cout << "cube2's volumn :" << cube2.ivolumn() << endl;
60    cout << "cube2's area :" << cube2.iarea() << endl;
61    cout << "全局函数中的判断:" << compare(cube1, cube2) << endl;
62    cout << "成员函数中的判断:" << cube1.compare1(cube2) << endl;
63    return 0;
64 }
复制代码

 

练习案例2:点和圆的关系

设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。

 

 函数主体

复制代码
 1 #include <iostream>
 2 #include "point.h";
 3 #include "circle.h"
 4 #include "isincircle.h"
 5 #include "ssincircle.h"
 6 using namespace std;
 7 // 点类
 8 //class point
 9 //{
10 //private:
11 //    int m_x;
12 //    int m_y;
13 //public:
14 //    void setx(int x)
15 //    {
16 //        m_x = x;
17 //    }
18 //    int getx()
19 //    {
20 //        return m_x;
21 //    }
22 //    void sety(int y)
23 //    {
24 //        m_y = y;
25 //    }
26 //    int gety()
27 //    {
28 //        return m_y;
29 //    }
30 //};
31 //圆类
32 //class Circle
33 //{
34 //private:
35 //    int m_R;
36 //    point m_Center;
37 //public:
38 //    void setR(int r)
39 //    {
40 //        m_R = r;
41 //    }
42 //    int getR()
43 //    {
44 //        return m_R;
45 //    }
46 //    void setcenter(point center)
47 //    {
48 //        m_Center = center;
49 //    }
50 //    // 让另一个类作为本类中的成员
51 //    point getcenter()
52 //    {
53 //        return m_Center;
54 //    }
55 //};
56 //获取和判断圆心到点距离函数
57 //string isincircle(Circle& c, point& p)
58 //{
59 //    if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2 )< c.getR())
60 //    {
61 //        return "点在圆内";
62 //    }
63 //    if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) == c.getR())
64 //    {
65 //        return "点在圆上";
66 //    }
67 //    return "点在圆外";
68 //}
69 //int ssincircle(Circle& c, point& p)
70 //{
71 //    return((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2);
72 //}
73 int main()
74 {
75     Circle c;
76     point p;
77     c.getcenter().setx(5);
78     c.getcenter().sety(5);
79     c.setR(3);
80     p.setx(2);
81     p.sety(2);
82     cout << "圆的半径为 :" << c.getR() << endl;
83     cout << "圆心到点的距离为 :" << ssincircle(c, p) <<  endl;
84     cout << "所以点和圆的关系为 :" << isincircle(c, p);
85 }
复制代码

点类分文件编写

point.h

复制代码
 1 #pragma once //防止头文件重复包含
 2 #include <iostream>
 3 using namespace std;
 4 class point
 5 {
 6 private:
 7     int m_x;
 8     int m_y;
 9 public:
10     void setx(int x);
11 
12     int getx();
13 
14     void sety(int y);
15 
16     int gety();
17 }; 
复制代码

point.cpp

复制代码
 1 #include "point.h"
 2 void point::setx(int x)
 3 {
 4     m_x = x;
 5 }
 6 int point::getx()
 7 {
 8     return m_x;
 9 }
10 void point::sety(int y)
11 {
12     m_y = y;
13 }
14 int point::gety()
15 {
16     return m_y;
17 }
复制代码

圆类分文件编写 

circle.h

复制代码
 1 #pragma once
 2 #include <iostream>
 3 #include  "point.h"
 4 using namespace std;
 5 class Circle
 6 {
 7 private:
 8     int m_R;
 9     point m_Center;
10 public:
11     void setR(int r);
12     
13     int getR();
14     
15     void setcenter(point center);
16     
17     // 让另一个类作为本类中的成员
18     point getcenter();
19     
20 };
复制代码

circle.cpp

复制代码
 1 #include "circle.h"
 2 void Circle :: setR(int r)
 3 {
 4     m_R = r;
 5 }
 6 int Circle :: getR()
 7 {
 8     return m_R;
 9 }
10 void Circle :: setcenter(point center)
11 {
12     m_Center = center;
13 }
14 // 让另一个类作为本类中的成员
15 point Circle :: getcenter()
16 {
17     return m_Center;
18 }
复制代码

获取和判断圆心到点距离函数

isincircle.h

1 #pragma once
2 #include <iostream>
3 #include "point.h";
4 #include "circle.h"
5 using namespace std;
6 string isincircle(Circle& c, point& p);

isincircle.cpp

复制代码
 1 #include "isincircle.h"
 2 string isincircle(Circle& c, point& p)
 3 {
 4     if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) < c.getR())
 5     {
 6         return "点在圆内";
 7     }
 8     if (((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2) == c.getR())
 9     {
10         return "点在圆上";
11     }
12     return "点在圆外";
13 }
复制代码

ssincircle.h

1 #pragma once
2 #include <iostream>
3 #include "point.h";
4 #include "circle.h"
5 using namespace std;
6 int ssincircle(Circle& c, point& p);

ssincircle.cpp

1 include "ssincircle.h"
2 int ssincircle(Circle& c, point& p)
3 {
4     return((c.getcenter().getx() - p.getx()) ^ 2 + (c.getcenter().gety() - p.gety()) ^ 2);
5 }

 

posted on   在野武将  阅读(132)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示