forked from bbw7561135/Numerical-Algorithms-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.cpp
More file actions
190 lines (136 loc) · 3.07 KB
/
class.cpp
File metadata and controls
190 lines (136 loc) · 3.07 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
using namespace std;
//圆周率
const double PI = 3.14;
//1、封装的意义
//将属性和行为作为一个整体,用来表现生活中的事物
//封装一个圆类,求圆的周长
//class代表设计一个类,后面跟着的是类名
class Circle
{
public: //访问权限 公共的权限
//属性
int m_r;//半径
//行为
//获取到圆的周长
double calculateZC()
{
//2 * pi * r
//获取圆的周长
return 2 * PI * m_r;
}
}; //like struct should have ; when ends
//学生类
class Student
{
public:
void setName(string name)
{
m_name = name;
}
void setID(int id)
{
m_id = id;
}
void showStudent()
{
cout << "name:" << m_name << " ID:" << m_id << endl;
}
public:
string m_name;
int m_id;
};
//三种权限
//公共权限 public 类内可以访问 类外可以访问
//保护权限 protected 类内可以访问 类外不可以访问
//私有权限 private 类内可以访问 类外不可以访问
//在C++中 struct和class唯一的区别就在于 默认的访问权限不同
//struct 默认权限为公共
//class 默认权限为私有
class Person
{
//姓名 公共权限
public:
string m_Name;
//汽车 保护权限
protected:
string m_Car;
//银行卡密码 私有权限
private:
int m_Password;
public:
void func()
{
m_Name = "Amao";
m_Car = "Audi";
m_Password = 123456;
}
};
class Personal
{
public:
//姓名设置可读可写
void setName(string name)
{
m_Name = name;
}
string getName()
{
return m_Name;
}
//设置年龄
void setAge(int age)
{
if (age < 0 || age > 150)
{
cout << "你个老妖精!" << endl;
return;
}
m_Age = age;
}
//获取年龄
int getAge()
{
return m_Age;
}
//情人设置为只写
void setLover(string lover)
{
m_Lover = lover;
}
private:
string m_Name; //可读可写 姓名
int m_Age; //只读 年龄
string m_Lover; //只写 情人
};
int main()
{
//通过圆类,创建圆的对象
// c1就是一个具体的圆
Circle c1;
c1.m_r = 10; //给圆对象的半径 进行赋值操作
//2 * pi * 10 = = 62.8
cout << "圆的周长为: " << c1.calculateZC() << endl;
Student stu;
stu.setName("Messi");
stu.setID(10);
stu.showStudent();
Person p;
p.func();
cout << p.m_Name << endl;
p.m_Name = "Adong";
cout << p.m_Name << endl;
//p.m_Car = "奔驰"; //保护权限类外访问不到
//p.m_Password = 123; //私有权限类外访问不到
Personal pp;
//姓名设置
pp.setName("Amao");
cout << "姓名: " << pp.getName() << endl;
//年龄设置
pp.setAge(31);
cout << "年龄: " << pp.getAge() << endl;
//情人设置
pp.setLover("Binbin");
//cout << "情人: " << p.m_Lover << endl; //只写属性,不可以读取
return 0;
}