类和对象
C++面向对象的三大特性为:封装、继承和多态
C++认为万事万物都皆为对象,对象上有其属性和行为
1.1 封装
1.1.1 封装的意义
- 将属性和行为作为一个整体,表现生活中的事物
语法:class 类名{访问权限:属性 / 行为}; - 将属性和行为加以权限控制
访问权限三种:public protected private
示例1
#include <iostream>
using namespace std;
const double PI = 3.14;
// 设计一个园类,求园的周长
//园求周长的公式:2 * PI * 半径
//class 代表设计一个类,类后面紧跟着的就是类名称
class Circle
{
//访问权限 -- 公共权限
public:
//属性 -- 半径
int m_r;
//行为 -- 获取圆的周长
double calculateZC()
{
return 2 * PI *m_r;
}
};
int main()
{
//通过圆类 创建具体的圆(对象)
Circle cl;
//给圆对象的属性进行赋值
cl.m_r = 10;
// 2 * PI * 10 = 62.8
cout << "圆的周长为:" << cl.calculateZC() << endl;
return 0;
}
示例2
#include <iostream>
#include <string>
using namespace std;
class Student
{
public: //公共权限
//类中的属性和行为 统一称为 成员
//属性 成员属性 成员变量
string m_Name; //姓名
int m_Id; //学号
//行为 成员函数 成员方法
//显示姓名和学号
void showStudent()
{
cout << "姓名:" << m_Name << " " << "学号:" << m_Id << endl;
}
//给姓名赋值
void setName(string name)
{
m_Name = name;
}
};
int main()
{
//创建一个具体学生
Student s1;
//s1.m_Name = "张三";
s1.setName("张三");
s1.m_Id = 1;
//显示学生信息
s1.showStudent();
Student s2;
s2.m_Name = "李四";
s2.m_Id = 2;
s2.showStudent();
return 0;
}
访问权限
#include <iostream>
#include <string>
using namespace std;
//访问权限
//公共权限 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 p1;
p1.m_Name = "李四";
}
1.1.2 struct 和 class 区别
默认访问权限不同
- struct 默认权限为公共
- class 默认权限为私有
1.1.3 成员属性设置为私有
优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
void setName(string name) //设置姓名
{
m_Name = name;
}
string getName() //获取姓名
{
return m_Name;
}
int getAge() //获取年龄 可读可写(修改范围0~150之间)
{
return m_Age;
}
void setAge(int age)
{
if (age < 0 || age > 150)
{
m_Age = 0;
cout << "你这个老妖精!" << endl;
return;
}
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.setName("张三");
cout << "姓名为:" << p.getName() << endl;
p.setAge(18);
cout << "年龄为:" << p.getAge() << endl;
p.setLover("木木");
return 0;
}
练习案例1: 设计立方体类
设计立方体类(Cube)
求出立方体的面积和体积
分别用全局函数和成员函数判断两个立方体是否相等
#include <iostream>
#include <string>
using namespace std;
class Cube
{
public:
void setL(int l)
{
m_L = l;
}
int getL()
{
return m_L;
}
void setH(int h)
{
m_H = h;
}
int getH()
{
return m_H;
}
void setW(int w)
{
m_W = w;
}
int getW()
{
return m_W;
}
int calculateS()
{
return 2 * m_L*m_W + 2 * m_H*m_W + 2 * m_L*m_H;
}
int calculateV()
{
return m_L * m_W * m_H;
}
bool isSameByClass(Cube &c)
{
if (m_L == c.getL() && m_H == c.getH() && m_W == c.getW())
{
return true;
}
return false;
}
private:
int m_H;
int m_L;
int m_W;
};
bool isSame(Cube &c1, Cube &c2)
{
if (c1.getL() == c2.getL() && c1.getH() == c2.getH() && c1.getW() == c2.getW())
{
return true;
}
return false;
}
int main()
{
Cube c1;
c1.setL(10);
c1.setW(20);
c1.setH(30);
cout << "c1的面积为:" << c1.calculateS() << endl;
cout << "c1的体积为:" << c1.calculateV() << endl;
Cube c2;
c1.setL(10);
c1.setW(20);
c1.setH(30);
bool ret = isSame(c1, c2);
if (ret)
{
cout << "c1和c2相等" << endl;
}
else
{
cout << "c1和c2不相等" << endl;
}
ret = c1.isSameByClass(c2);
if (ret)
{
cout << "成员函数判断:c1和c2相等" << endl;
}
else
{
cout << "成员函数判断:c1和c2不相等" << endl;
}
return 0;
}
练习案例2:点和圆关系
设计一个圆形类(Circle)和一个点类(Point),计算点和圆的关系。
m.cpp
#include <iostream>
#include <string>
#include "circle.h"
#include "point.h"
using namespace std;
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);
Point center;
center.setX(10);
center.setY(0);
c.setCenter(center);
Point p; //创建点
p.setX(10);
p.setY(10);
isInCircle(c, p);
return 0;
}
point.cpp
#include "point.h"
using namespace std;
void Point::setX(int x)
{
m_X = x;
}
int Point::getX()
{
return m_X;
}
void Point::setY(int y)
{
m_Y = y;
}
int Point::getY()
{
return m_Y;
}
circle.cpp
#include "circle.h"
using namespace std;
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;
}
point.h
#pragma once
#include <iostream>
using namespace std;
class Point
{
public:
void setX(int x);
int getX();
void setY(int y);
int getY();
private:
int m_X;
int m_Y;
};
circle.h
#pragma once
#include <iostream>
#include <point.h>
using namespace std;
class Circle
{
public:
void setR(int r);
int getR();
void setCenter(Point center);
Point getCenter();
private:
int m_R;
Point m_Center;
};