封装的意义以及示例
封装是C++面向对象三大特征之一
封装的意义:
将属性和行为作为一个整体,表现生活中的事物将属性和行为加以权限控制
语法:class 类名{ 访问权限 : 属性 / 行为 };
类的对象的公共数据成员可以使用直接成员访问运算符 . 来访问。
示例1: 设计一个圆类,求圆的周长
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include<iostream> using namespace std; #define PI 3.14 class Circle { //访问权限 //公共权限 public : //属性 int r; //行为 double calculate() { return 2 * PI * r; } }; int main() { Circle a; a.r = 2; cout << a.calculate() << endl; system ( "pause" ); return 0; } |
示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
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
|
#include<iostream> #include<string> using namespace std; class student { public : string name; int Id; void CinSudent() { cin >> name; cin >> Id; } void ShowStudent() { cout << name << endl; cout << Id << endl; } }; int main() { student a; a.CinSudent(); a.ShowStudent(); system ( "pause" ); return 0; } |
访问权限
类在设计时,可以把属性和行为放在不同的权限下,加以控制
1
2
3
4
|
访问权限有三种: 1. public 公共权限 2. protected 保护权限 3. private 私有权限 |
公共权限 public
成员类内可以访问 类外不可以访问
1
2
3
4
5
6
7
|
class Box { public : double length; void setWidth( double wid ); double getWidth( void ); }; |
保护权限 protected
成员类内可以访问 类外不可以访问 儿子可以访问父亲中的保护内容 protected(受保护)成员变量或函数与私有成员十分相似,但有一点不同, protected(受保护)成员在派生类(即子类)中是可访问的。
1
2
3
4
5
6
7
|
class Box { protected : double length; void setWidth( double wid ); double getWidth( void ); }; |
下面的实例与前面的实例类似,在这里 width 成员可被派生类 smallBox 的任何成员函数访问。
私有权限 private
成员类内可以访问 类外不可以访问
儿子可以访问父亲中的私有内容
私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
默认情况下,类的所有成员都是私有的。例如在下面的类中,width 是一个私有成员,
这意味着,如果您没有使用任何访问修饰符,类的成员将被假定为私有成员
1
2
3
4
5
6
7
|
class Box { private : double length; void setWidth( double wid ); double getWidth( void ); }; |
struct 和 class的区别
在C++中struct 和class的默认访问权限不同
区别:
struct 默认访问权限为公共
class 默认访问权限为私有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include<iostream> using namespace std; struct C1 { int m_A; //默认权限为公有 }; class C2 { int m_A; //默认权限为私有 }; int main() { C1 c1; C2 c2; c1.m_A = 100; //c2.m_A = 100; 此处无法访问 return 0; } |
成员属性私有化
优点1:将所有成员设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性
示例:
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
|
#include<iostream> #include<string> using namespace std; class Person { public : void Setname(string name1) { name = name1; } string Showname() { return name; } int Showage() { return age; } private : //私有权限 string name; int age=18; }; int main() { Person a; a.Setname( "张三" ); cout << "姓名为: " << a.Showname() << endl; cout << "年龄为: " << a.Showage() << endl; system ( "pause" ); return 0; } |
案例1:设计立方体类
要求:
设计立方体
求出立方体的面积和体积
分别用全局函数和成员函数判断两个立方体是否相等
代码实现:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#include<iostream> #include<string> using namespace std; class Cube { public : //设置长 void setL( int l) { m_L = l; } //设置宽 void setW( int w) { m_W = w; } //设置高 void setH( int h) { m_H = h; } //获取长 int getL() { return m_L; } //获取宽 int getW() { return m_W; } //获取高 int getH() { return m_H; } //获取面积 int calculateS() { return 2 * (m_L * m_W + m_L * m_H + m_W * m_H); } //获取体积 int calculateV() { return m_L * m_H * m_W; } //利用成员函数判断两个立方体是否相等 bool isSameByClass(Cube& c) { if (m_H == c.getH() && m_L == c.getL() && m_W == c.getW()) return true ; else return false ; } private : int m_L; //长 int m_W; //宽 int m_H; //高 }; //利用全局函数判断两个立方体是否相等 bool isSame(Cube &c1,Cube &c2) { if (c1.getH() == c2.getH() && c1.getL() == c2.getL() && c1.getW() == c2.getW()) return true ; else return false ; } int main() { Cube c1; c1.setH(10); c1.setL(13); c1.setW(45); cout << "c1面积是:" << c1.calculateS() << endl; cout << "c2体积是:" << c1.calculateV() << endl; Cube c2; c2.setH(10); c2.setL(13); c2.setW(45); //利用全局函数判断 if (isSame(c1, c2)) cout << "c1和c2相等" << endl; else cout << "c1和c2不相等" << endl; //利用成员函数判断 if (c1.isSameByClass(c2)) cout << "c1和c2相等" << endl; else cout << "c1和c2不相等" << endl; system ( "pause" ); return 0; } |
案例2:点和圆的关系
要求:设计一个圆类型(Cricle),和一个(Point),计算点和圆的关系。
1. 点在圆外
2.点在圆内
3.点在圆上
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include<iostream> using namespace std; class Point { public : //设置x void setX( int x) { m_X = x; } //设置y void setY( int y) { m_Y = y; } //获取x int getX() { return m_X; } int getY() { return m_Y; } private : int m_X; int m_Y; }; class Circle { public : //设置半径 void setR( int r) { m_R = r; } //获取半径 int getR() { return m_R; } //设置圆心 void setCenter( int x, int y) { m_Center.setX(x); m_Center.setY(y); } //获取圆心 Point getCenter() { return m_Center; } private : int m_R; //半径 Point m_Center; //圆心 }; //判断点和圆的关系 void isInCircle(Circle& c, Point& p) { //计算距离的平方 int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY()); //计算半径的平方 int RDistance = c.getR() * c.getR(); //判断关系 if (distance == RDistance) cout << "点在圆上" << endl; else if (distance > RDistance) cout << "点在圆外" << endl; else cout << "点在圆内" << endl; } int main() { //创建一个圆 Circle c; c.setR(10); c.setCenter(10, 0); Point p; p.setX(10); p.setY(10); //判断关系 isInCircle(c,p); system ( "pause" ); return 0; } |
总结
到此这篇关于C++类和对象之封装详解的文章就介绍到这了,更多相关C++封装内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_61705102/article/details/122169829