目录
属 性 和 行 为 作 为 整 体
C++ 面向对象的三大行为:封 装、继 承、多 态
-
C++认为 万 物 皆 为 对 象 ,对象上有属性和行为
人作为对象时,属性有 姓名、年龄、身高、体重...,行为有 走、跑、跳...
封装的意义
- 将属性和行为作为一个整体,表现生活中的万物
- 将属性和行为加以权限控制
封装意义1
将属性和行为作为一个整体,表现事物
-
语法:class 类名 { 访问权限: 属性 / 行为 };
设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
// 圆周率
const double PI = 3.14;
// class 代表要设计一个类,后紧跟着的就是类的名称(自定义)
class Circle
{
// 类的具体内容
// 访问权限
public:
// 属性 - 变量
int m_r; // 半径
// 行为 - 函数
double calculateCC() // 获取圆的周长
{
return 2 * PI * m_r;
}
};
// 设计一个圆类
int main()
{
// 通过圆类创建一个具体的圆(对象)
Circle c1;
// 给圆对象进行初始化
c1.m_r = 10; // 半径
cout << "圆的周长:" << c1.calculateCC() << endl;
system("pause");
return 0;
}
设 计 学 生 类
通过实例化对象对属性进行赋值,再使用类的行为
#include<iostream>
#include<string>
using namespace std;
class Student
{
// 权限
public:
// 属性
string m_Name;
string m_Id;
int m_Age;
string m_Sex;
// 行为
void Showstudent()
{
cout << "姓名:" << m_Name << endl;
cout << "学号:" << m_Id << endl;
cout << "年龄:" << m_Age << endl;
cout << "性别:" << m_Sex << endl;
}
};
int main()
{
// 实例化对象,通过类创建一个具体对象
Student s1;
// 类的属性赋值
s1.m_Name = "李华";
s1.m_Id = "2022";
s1.m_Age = 19;
s1.m_Sex = "男";
// 类的行为
s1.Showstudent();
system("pause");
return 0;
}
通过行为对属性进行赋值操作
#include<iostream>
#include<string>
using namespace std;
class Student
{
// 权限
public:
// 属性
string m_Name;
string m_Id;
int m_Age;
string m_Sex;
// 行为
void Showstudent()
{
cout << "姓名:" << m_Name << endl;
cout << "学号:" << m_Id << endl;
cout << "年龄:" << m_Age << endl;
cout << "性别:" << m_Sex << endl << endl;
}
// 行为对属性进行操作
// 给姓名赋值
void SetName(string name)
{
m_Name = name;
}
// 给学号赋值
void SetId(string id)
{
m_Id = id;
}
// 给年龄赋值
void SetAge(int age)
{
m_Age = age;
}
// 给性别赋值
void SetSex(string sex)
{
m_Sex = sex;
}
// 集合到一起
void SetAttribute(string name, string id, int age, string sex)
{
m_Name = name;
m_Id = id;
m_Age = age;
m_Sex = sex;
}
};
int main()
{
// 实例化对象,通过类创建一个具体对象
Student s1;
// 类的赋值
s1.m_Name = "李华";
s1.m_Id = "2022";
s1.m_Age = 19;
s1.m_Sex = "男";
// 类的行为
s1.Showstudent();
Student s2;
// 类的赋值
s2.SetName("李四");
s2.SetId("2021");
s2.SetAge(20);
s2.SetSex("男");
// 类的行为
s2.Showstudent();
Student s3;
// 类的赋值
s3.SetAttribute("张三", "2020", 18, "男");
// 类的行为
s3.Showstudent();
system("pause");
return 0;
}
类中的属性和行为 统称为 成员
-
属性 成员属性 成员变量
-
行为 成员函数 成员方法
访 问 权 限
类在设计时,可以把属性和行为放在不同的权限下,加以控制
-
访问权限有三种:
- public 公 共 权 限 成员 类内可以访问,类外可以访问
- protected 保 护 权 限 成员 类内可以访问,类外不可以访问
- private 私 有 权 限 成员 类内可以访问,类外不可以访问(更私有)
类内指的是 class 的大括号内
#include<iostream>
#include<string>
using namespace std;
class Person
{
// 公共权限
public:
string m_Name;
// 保护权限
protected:
string m_Phone;
// 私有权限
private:
string m_Password;
public:
void func()
{
m_Name = "李华";
m_Phone = "HUWEI";
m_Password = "123456789";
}
};
试着在主函数中(类外)对 类属性 m_Phone 和 m_Password 进行修改
发现可选项只有 公共权限 的 m_Name 和 func
强行访问 会报错
error C2248: “Person::m_Phone”: 无法访问 protected 成员(在“Person”类中声明)
private 私有权限也一样在类外无法访问
struct 和 class 区别
在 C++ 中 struct 和 class 唯一的区别在于 默认访问权限不同,struct 是为了兼容 C 的存在,C++ 中尽量使用 class
-
区别:
- struct 默认权限 公 共 权 限
- class 默认权限 私 有 权 限
成 员 属 性 私 有 化
优点:
- 将所有成员属性设为私有,可以自己控制读写权力
- 对于写的权限,可以检测数据的有效性
对外设置 public 接口
#include<iostream>
#include<string>
using namespace std;
class Person
{
// 设置 public 接口
public:
// 设置姓名(写)
void SetName(string name)
{
m_Name = name;
}
// 获取姓名(读)
string GetName()
{
return m_Name;
}
// 设置年龄(写)
void SetAge(int age)
{
m_Age = age;
}
// 获取性别(读)
string GetSex()
{
return m_Sex;
}
// 设为私有权限
private:
string m_Name; // 姓名 可读可写
int m_Age; // 年龄 只写
string m_Sex; // 性别 只读
};
int main()
{
Person p1;
p1.SetAge(10);
p1.SetName("李华");
cout << p1.GetName() << endl;
system("pause");
return 0;
}
检测数据的有效性
// 设置年龄(写)
void SetAge(int age) // 0~150
{
if (age < 0 || age > 150)
{
cout << "error age" << endl;
return;
}
m_Age = age;
}
立 方 体 类
设计立方体(Cube)
-
求出立方体的 面积 和 体积
-
分别用全局函数和成员函数判断两个立方体是否相等
#include<iostream>
using namespace std;
class Cube
{
public:
// 设置高
void SetH(int H)
{
m_H = H;
}
// 获取高
int GetH()
{
return m_H;
}
// 设置长
void SetL(int L)
{
m_L = L;
}
// 获取长
int GetL()
{
return m_L;
}
// 设置宽
void SetW(int W)
{
m_W = W;
}
// 获取宽
int GetW()
{
return m_W;
}
// 计算面积
int CalculatedArea()
{
return m_H * m_L * 2 +
m_H * m_W * 2 +
m_W * m_L * 2;
}
// 计算体积
int CalculatedVolume()
{
return m_H * m_L * m_W;
}
// 判断两个立方体是否相等
bool IsEqual(Cube &c2) // 为了传入的更简单一点选择引用
{
if (m_H == c2.GetH() && m_L == c2.GetL() && m_W == c2.GetW())
return true;
else
return false;
}
private:
// 属性
int m_H; // 高
int m_L; // 长
int m_W; // 宽
};
// 全局函数 判断
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(10);
c1.SetW(10);
// 创建一个立方体
Cube c2;
c2.SetH(10);
c2.SetL(10);
c2.SetW(10);
cout << "立方体的面积:" << c1.CalculatedArea() << endl;
cout << "立方体的体积:" << c1.CalculatedVolume() << endl;
// 利用成员函数
if (c1.IsEqual(c2))
{
cout << "相同" << endl;
}
else
{
cout << "不同" << endl;
}
// 利用全局函数
if (IsSame(c1, c2))
{
cout << "相同" << endl;
}
else
{
cout << "不同" << endl;
}
system("pause");
return 0;
}
点 和 圆 关 系
设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系
#include<iostream>
using namespace std;
// 设计一个点的类
class Point
{
public:
// 设置 m_X
void SetX(int x)
{
m_X = x;
}
// 获取 m_X
int GetX()
{
return m_X;
}
// 设置 m_Y
void SetY(int y)
{
m_Y = y;
}
// 获取 m_Y
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(Point center)
{
m_Center = center;
}
// 获取圆心
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;
// 实例化点对象
// 圆的圆心点
Point pc;
pc.SetX(0);
pc.SetY(0);
// 点
Point p;
p.SetX(0);
p.SetY(10);
// 设置半径
c.SetR(10);
// 设置圆心
c.SetCenter(pc);
// 判断
IsInCircle(c, p);
system("pause");
return 0;
}
头文件和源文件封装
circle.h 头文件
.h 头文件 里对 类的函数 和 类的变量 进行声明
#pragma once
#include<iostream>
using namespace std;
#include"point.h"
// 设计一个圆形类
class Circle
{
public:
// 设置半径
void SetR(int r);
// 获取半径
int GetR();
// 设置圆心
void SetCenter(Point center);
// 获取圆心
Point GetCenter();
private:
// 属性
int m_R; // 半径
Point m_Center; // 圆的圆心
};
IsInCircle.h 头文件
#pragma once
#include<iostream>
using namespace std;
#include"circle.h"
#include"point.h"
void IsInCircle(Circle& c, Point& p);
Point.h 头文件
#pragma once
#include<iostream>
using namespace std;
//声明
// 设计一个点的类
class Point
{
public:
// 设置 m_X
void SetX(int x);
// 获取 m_X
int GetX();
// 设置 m_Y
void SetY(int y);
// 获取 m_Y
int GetY();
private:
int m_X;
int m_Y;
};
circle.cpp 源文件
.cpp 对声明的函数进行实现
#include"point.h"
#include"circle.h"
// 设置半径
void Circle::SetR(int r)
{
m_R = r;
}
// 获取半径
int Circle::GetR()
{
return m_R;
}
// 设置圆心
void Circle::SetCenter(Point center)
{
m_Center = center;
}
// 获取圆心
Point Circle::GetCenter()
{
return m_Center;
}
// 属性
int m_R; // 半径
Point m_Center; // 圆的圆心
IsInCircle.cpp 头文件
#include"IsInCircle.h"
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;
}
}
point.cpp 头文件
#include"point.h" // 包含 point 头文件
// 设计一个点的类
// 设置 m_X
void Point::SetX(int x) // 加上作用域 Point::
{
m_X = x;
}
// 获取 m_X
int Point::GetX()
{
return m_X;
}
// 设置 m_Y
void Point::SetY(int y)
{
m_Y = y;
}
// 获取 m_Y
int Point::GetY()
{
return m_Y;
}
int m_X;
int m_Y;
main.cpp 主函数源文件
#include<iostream>
using namespace std;
#include"circle.h"
#include"point.h"
#include"IsInCircle.h"
int main()
{
// 实例化圆对象
Circle c;
// 实例化点对象
// 圆的圆心点
Point pc;
pc.SetX(0);
pc.SetY(0);
// 点
Point p;
p.SetX(0);
p.SetY(10);
// 设置半径
c.SetR(10);
// 设置圆心
c.SetCenter(pc);
// 判断
IsInCircle(c, p);
system("pause");
return 0;
}