由类创建对象
C++面向对象的三大特性为:封装、继承、多态
C++认为万事万物都皆为对象,对象上有其属性和行为
例如:
 人可以作为对象,属性有姓名、年龄、身高、体重…,行为有走、跑、跳、吃饭、唱歌…
 车也可以作为对象,属性有轮胎、方向盘、车灯…,行为有载人、放音乐、放空调…
 具有相同性质的对象,我们可以抽象称为类,人属于人类,车属于车类
封装
封装的意义
封装是C++面向对象三大特性之一
封装的意义:
- 将属性和行为作为一个整体,表现生活中的事物
 - 将属性和行为加以权限控制
 
封装意义一:
 在设计类的时候,属性和行为写在一起,表现事物
语法: class 类名{ 访问权限: 属性 / 行为 };
示例1:设计一个圆类,求圆的周长
示例代码:
//圆周率const double PI = 3.14;//1、封装的意义//将属性和行为作为一个整体,用来表现生活中的事物//封装一个圆类,求圆的周长//class代表设计一个类,后面跟着的是类名class Circle{public: //访问权限 公共的权限//属性int m_r;//半径//行为//获取到圆的周长double calculateZC(){//2 * pi * r//获取圆的周长return 2 * PI * m_r;}};int main() {//通过圆类,创建圆的对象// c1就是一个具体的圆Circle c1;c1.m_r = 10; //给圆对象的半径 进行赋值操作//2 * pi * 10 = = 62.8cout << "圆的周长为: " << c1.calculateZC() << endl;system("pause");return 0;}
示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
示例2代码:
//学生类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;};int main() {Student stu;stu.setName("德玛西亚");stu.setID(250);stu.showStudent();system("pause");return 0;}
#include <iostream>#include <string>using namespace std;class stu {public:int score1;int score2;string name;double ave() {return (score1 + score2) / 2;}};int main() {stu s1;s1.score1 = 80;s1.score2 = 90;s1.name = "ZhangSan";cout << s1.name << endl << s1.ave() << endl;return 0;}
类的简单定义和C语言中的结构体非常相似,但常规的类中却有着更加复杂的功能。
权限
有三种访问权限
- 公有权限 public 成员类内可以访问 类外可以访问 继承可访问
 - 保护权限 protected 成员类内可以访问 类外不可以访问 继承可访问
 - 私有权限 private 成员类内可以访问 类外不可以访问 继承不可访问 | 权限名 | 类内权限 | 继承权限 | 类外权限 | | —- | —- | —- | —- | | public | 允许 | 允许 | 允许 | | protected | 允许 | 允许 | 不允许 | | private | 允许 | 不允许 | 不允许 |
 
示例:
//三种权限//公共权限 public 类内可以访问 类外可以访问//保护权限 protected 类内可以访问 类外不可以访问//私有权限 private 类内可以访问 类外不可以访问class Person{//姓名 公共权限public:string m_Name;//汽车 保护权限protected:string m_Car;//银行卡密码 私有权限private:int m_Password;public:void func(){m_Name = "张三";m_Car = "拖拉机";m_Password = 123456;}};int main() {Person p;p.m_Name = "李四";//p.m_Car = "奔驰"; //保护权限类外访问不到//p.m_Password = 123; //私有权限类外访问不到system("pause");return 0;}
struct和class区别
在C++中 struct和class唯一的区别就在于 默认的访问权限不同
区别:
- struct 默认权限为公共
 - class 默认权限为私有
```cppclass C1{int m_A; //默认是私有权限};struct C2{int m_A; //默认是公共权限};int main() {C1 c1;c1.m_A = 10; //错误,访问权限是私有C2 c2;c2.m_A = 10; //正确,访问权限是公共system("pause");return 0;}
include
include
using namespace std; 
class Student { public: string name; int sex = 0; int height; int weight; //We can change above four members private: double score1; //This is accessable by a public portal; double score2; //This is accessable by a public portal; double avg; //We cannot access this, but we can read it by public portal; protected: string tel; string add; public: void wScore1(double i_socre) { score1 = i_socre; } void wScore2(double i_score) { score2 = i_score; } double GetAvg() { return (score1 + score2) / 2; } void wTel(string i_tel) { tel = i_tel; } string GetTel() { return tel; } }; int main() { Student s1; s1.wScore1(80); s1.wScore2(90); cout << s1.GetAvg() << endl; s1.wTel(“135132”); cout << s1.GetTel() << endl; return 0; }
```
