类和对象-封装
13.0 前言
C++面向对象的三个特性:封装、继承、多态。
C++认为万事万物都皆为对象,对象上有其属性和行为。
类中的属性和行为统称为成员。
属性也称为成员属性或成员变量。
行为也成为成员函数或成员方法。
13.1 封装的意义
封装是C++面向对象三大特性之一。
封装的意义:
- 将属性和行为作为一个整体,表现生活中的事物。
- 将属性和行为加以权限控制。
13.1.1 封装意义一
将属性和行为作为一个整体,表现生活中的事物。
语法:
注意:
类中的属性不可以初始化, 类的定义实际上相当于是类型的声明,并没有分配存储空间,初始化后的数据没有地方存放。
class 类名{访问权限: 属性/行为}
示例:
#include<iostream>
using namespace std;
const double PI = 3.14;
//设计一个圆类, 求圆的周长
//周长公式:2 * PI * 半径
//class代表设计一个类, 类后面紧跟着的是类名称
class Circle
{
//访问权限
//设置公共权限,类内外都可以访问
public:
//属性
//半径
//int m_r=5;//不可以初始化,因为类中属性在定义的时候并没有分配存储空间,实例化的时候才开始储存。
int m_r;
//行为
//获取圆的周长
double calculateZC()
{
return 2 * PI * m_r;
}
};
int main()
{
//类的实例化----通过圆这个类来创建具体的圆对象
Circle c1;
c1.m_r = 10;
cout << "圆的周长是:" << c1.calculateZC() << endl;
system("pause");
return 0;
}
案例:设计学生类
#include<iostream>
using namespace std;
class Student
{
//权限
public:
//属性
string s_name;
int s_id;
//行为
void setName(string name)
{
s_name = name;
}
void setID(int id)
{
s_id = id;
}
void showStudent()
{
cout << "姓名: " << s_name << " 学号: " << s_id << endl;
}
};
int main()
{
//实例化
Student s1;
s1.s_name = "张三";
s1.s_id = 2022;
s1.showStudent();
//通过行为赋值
Student s2;
s2.setName("李四");
s2.setID(2023);
s2.showStudent();
system("pause");
return 0;
}
13.1.2 封装意义二
将属性和行为加以权限控制。
访问权限有三种:
public | 公共权限 | 类内可以访问,类外也可以访问 |
protected | 保护权限 | 类内可以访问,类外不可以访问 |
private | 私有权限 | 类内可以访问,类外不可以访问 |
#include<iostream>
using namespace std;
//三种权限
//public 类内可以访问,类外也可以访问
//protected 类内可以访问,类外不可以访问 儿子可以访问父亲中的保护内容
//private 类内可以访问,类外不可以访问 儿子不可以访问父亲中的私有内容
class Person
{
//公共权限
public:
string m_Name; //姓名
//保护权限
protected:
string m_Car; //汽车
//私有权限
private:
int m_password;//银行卡密码
public:
void fun() //类内访问没有问题
{
m_Name = "张三";
m_Car = "拖拉机";
m_password = 123456;
}
};
int main()
{
Person p1;
//类外访问
p1.m_Name = "李四";
//p1.m_Car = "奔驰"; 保护权限,类外不可以访问,报错
//p1.m_password = 654321; 私有权限,类外不可以访问,报错
p1.fun();
system("pause");
return 0;
}
13.2 struct和class的区别
在C++中struct和class唯一的区别就在于 默认的访问权限不同。
区别:
- struct 默认权限为公共–public
- class 默认权限为私有–private
#include<iostream>
using namespace std;
class C
{
int m_A;
};
struct S
{
int m_A;
void s()
{
cout << "123" << endl;
}
};
int main()
{
//C c;
//c.m_A = 100; 报错,因为默认权限是私有权限
S s;
s.m_A = 10;
s.s();
system("pause");
return 0;
}
13.3 成员属性设置为私有
- 优点1: 将所有成员属性设置为私有,可以自己控制读写权限
- 优点2: 对于写权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
//成员属性设置为私有
class Person
{
public:
//名字 读写
void setName(string name)
{
m_Name = name;
}
string getName()
{
return m_Name;
}
// 年龄 只读 ----> 可读可写
int getAge()
{
//int age = 0;
return m_Age;
}
void setAge(int age)
{
if (age < 0 || age>100)
{
m_Age = 0; //默认0岁
cout << "别闹" << endl;
return;
}
else { m_Age = age; }
}
//情人 只写
void setLover(string lover)
{
m_Lover = lover;
}
private:
string m_Name;
int m_Age; //类的属性不可以初始化
string m_Lover;
};
int main()
{
Person p;
//p.m_Name = "张三"; 私有属性,不可从外部访问
p.setName("张三");
cout << "姓名为: " << p.getName() << endl;
//p.m_Age = 10; 私有属性,不可从外部访问
p.setAge(-10);
cout << "年龄为: " << p.getAge() << endl;
//p.m_lover = "李四"; 私有属性,不可从外部访问
p.setLover("李四");
system("pause");
return 0;
}
13.4 案例1:设计立方体类
#include<iostream>
using namespace std;
class Cube
{
public:
void set_LWH()
{
cout << "请输入立方体的长" << endl;
cin >> m_L;
cout << "请输入立方体的宽" << endl;
cin >> m_W;
cout << "请输入立方体的高" << endl;
cin >> m_H;
}
double get_L()
{
return m_L;
}
double get_W()
{
return m_W;
}
double get_H()
{
return m_H;
}
double S()
{
return 2 * (m_L * m_W + m_L * m_H + m_W * m_H);
}
double V()
{
return m_L * m_W * m_H;
}
//利用成员函数判断两个立方体是否相等
bool isSameByClass(Cube& c1)
{
if (c1.get_L() == m_L && c1.get_W() == m_W && c1.get_H() == m_H) { return true; }
else { return false; }
}
private:
double m_L, m_W, m_H;
};
//利用全局函数判断 两个立方体是否相同
bool isSame(Cube& c1, Cube& c2)
{
if (c1.get_L() == c2.get_L()&& c1.get_W() == c2.get_W()&& c1.get_H() == c2.get_H())
{
return true;
}
else { return false; }
}
int main()
{
double L = 0, W = 0, H = 0;
Cube c1,c2;
c1.set_LWH();
cout << "第一个立方体长,宽,高分别为:" << c1.get_L()
<< "," << c1.get_W() << "," << c1.get_H() << endl;
cout << "第一个立方体面积为: " << c1.S() << endl;
cout << "第一个立方体体积为: " << c1.V() << "\n" << endl;
c2.set_LWH();
cout << "第二个立方体长,宽,高分别为:" << c1.get_L()
<< "," << c1.get_W() << "," << c1.get_H() << endl;
cout << "第二个立方体面积为: " << c2.S() << endl;
cout << "第二个立方体体积为: " << c2.V() << "\n" << endl;
if (isSame(c1, c2) == 1)
{
cout << "全局函数判断结果: 这两个立方体相等" << endl;
}
else {
cout << "全局函数判断结果: 这两个立方体不等" << endl;
}
if (c2.isSameByClass(c1) == 1)
{
cout << "成员函数判断结果: 这两个立方体相等" << endl;
}
else {
cout << "成员函数判断结果: 这两个立方体不等" << endl;
}
system("pause");
return 0;
}
13.5 案例2:点和圆的关系(分文件定义声明类)
一种是定义和声明都分开的写法,如下:
1.创建point.h的头文件
#pragma once //防止多次包含
#include<iostream>
using namespace std;
//只声明即可
class Point
{
public:
void setX(int x);
void setY(int y);
int getX();
int getY();
private:
int m_X;
int m_Y;
};
2.创建point.cpp的源文件
#include"point.h"
void Point::setX(int x) //设置作用域,表示这个函数是Point这个类的成员函数
{
m_X = x;
}
void Point::setY(int y)
{
m_Y = y;
}
int Point::getX()
{
return m_X;
}
int Point::getY()
{
return m_Y;
}
3.创建circle.h的头文件
#pragma once //防止多次包含
#include<iostream>
#include"point.h"
using namespace std;
class Circle
{
public:
void setR(int r);
void setCenter(Point center);
int getR();
Point getCenter();
private:
int m_R;
Point m_Center;
};
4.创建circle.cpp的源文件
#include"circle.h"
void Circle::setR(int r)
{
m_R = r;
}
void Circle::setCenter(Point center)
{
m_Center = center;
}
int Circle::getR()
{
return m_R;
}
Point Circle::getCenter()
{
return m_Center;
}
另外一种就是直接在头文件中定义
5.创建test.h
#pragma once //防止多次包含
#include<iostream>
using namespace std;
class test
{
public:
int m_x;
};
6.在主文件中以下代码:
#include<iostream>
#include<cmath>
using namespace std;
#include"point.h"
#include"circle.h"
#include"test.h"
void isInCircle(Circle& c, Point& p)
{
int distance =
pow(c.getCenter().getX() - p.getX(), 2) + pow(c.getCenter().getY() - p.getY(), 2);
int R =pow(c.getR(),2);
if(R==distance)
{
cout << "点在圆上" << endl;
}
if (R > distance)
{
cout << "点在圆内" << endl;
}
if (R < distance)
{
cout << "点在圆外" << endl;
}
}
int main()
{
Circle c;
Point p,center;
c.setR(10);
center.setX(5);
center.setY(5);
c.setCenter(center);
p.setX(10);
p.setY(10);
isInCircle(c, p);
//下面的仅仅测试在头文件中定义类,可以删.
test t;
t.m_x = 10;
cout << t.m_x << endl;
system("pause");
return 0;
}