前言
C++面向对象的三大特性就是:封装,继承,多态
封装的意义在于将属性和行为作为一个整体,表现甚或中的食物并将属性加以权限控制
例如:人的属性,不仅包括年龄,身高,体重等固有属性,还包括妻子,儿子,父母。等社会属性
一、类的调用
代码如下(示例):
class 类名
{
//访问权限
public;
//属性
//行为
}
其中,访问权限有三种:Public(公共权限)、Protect(保护权限)、private(私有权限);
**公共权限:**类内可以访问,类外也可以访问;
保护权限:类内可以访问,类外不能访问;
私有权限:类内可以访问,类外不能访问;
其中保护权限和私有权限,在继承上有所不同;
属性和行为都属于成员,其中成员函数就是行为,例如:定义一个正方体,它的边长就是它的属性,类内设置函数定义计算面积体积,就属于行为。
二、类与结构体的区别
类和结构体的区别:
- struct默认权限为公共
- class默认权限为私有
成员属性设置为私有的优点在于: - 将所有的成员设置为私有,可以自己控制读写(通过设置set和get功能函数);
- 对于写权限,我们可以检测数据的有效性(在set函数内部设置条件);
三、实例练习
练习实例1:设计立方体类
功能:
- 设计立方体类
- 求出立方体的面积和体积
- 分别用全局函数和成员函数判断两个立方体是否相等
#include<stdio.h>
#include<string>
#include <iostream>
#define True 1
using namespace std;
class cube
{
private://Property is best set to private
int m_L, m_W,m_H;
public:
//set/get length
void setL(int l)
{
m_L = l;
}
int getL()
{
return m_L;
}
//set/get wide
void setW(int w)
{
m_W = w;
}
int getW()
{
return m_W;
}
//set/get high
void setH(int h)
{
m_H = h;
}
int getH()
{
return m_H;
}
//suface area of cube
int CubeS()
{
return 2 * m_L * m_W +2 * m_L * m_H +2 * m_W * m_H;
}
//volume of cube
int CubeV()
{
return m_L * m_W * m_H;
}
void isEquality2(cube& A1)
{
if ((A1.getL(), A1.getW(), A1.getH())== (m_L, m_W, m_H))
{
cout << "two cube is equal!!!" << endl;
}
else
{
cout << "two cube is not equal!!!" << endl;
}
}
};
//Check whether two cuboids are equal
void isEquality1(cube &A1, cube &A2)
{
if ((A1.getL(),A1.getW(),A1.getH())==(A2.getL(),A2.getW(),A2.getH()))
{
cout <<"two cube is equal!"<<endl;
}
else
{
cout <<"two cube is not equal!"<<endl;
}
}
int main(void)
{
int a = 0,b=0,c=0;//define the length of the cube
cube cube1;
cube cube2;
while (True)
{
cout << "please input the length of the cube(length,wide,high>0):" << endl;
cin >> a;
cin >> b;
cin >> c;
if ((a > 0)&&(b>0)&&(c>0))
{
cube1.setL(a);
cube1.setW(b);
cube1.setH(c);
cout<<"Surface area of the cube:"<<cube1.CubeS()<<endl;
cout << "Volume of the cube:" <<cube1.CubeV()<< endl;
break;
}
else
{
cout << "Invalid input please re-enter!" << endl;
}
}
while (True)
{
cout << "please input the length of the cube(length,wide,high>0):" << endl;
cin >> a;
cin >> b;
cin >> c;
if ((a > 0) && (b > 0) && (c > 0))
{
cube2.setL(a);
cube2.setW(b);
cube2.setH(c);
cout << "Surface area of the cube:" << cube1.CubeS() << endl;
cout << "Volume of the cube:" << cube1.CubeV() << endl;
break;
}
else
{
cout << "Invalid input please re-enter!" << endl;
}
}
//check two cube
isEquality1(cube1, cube2);
cube1.isEquality2(cube2);
return 0;
}
结果图:
注意:全局函数和成员函数的不同之处,全局函数要输入两个形参,成员函数只用一个。
练习实例2:点和圆的关系
功能:设计一个圆形类,和一个点类,计算点和圆的关系
分析:点类有点的横坐标和纵坐标属性
圆有圆心坐标和半径属性
//Gets the relationship between points and circles
#include<iostream>
using namespace std;
class Point
{
public:
void set_x(int x)
{
m_x = x;
}
int get_x()
{
return m_x;
}
void set_y(int y)
{
m_y = y;
}
int get_y()
{
return m_y;
}
private:
int m_x;
int m_y;
};
class Circle
{
public:
void set_r(int r)
{
m_r = r;
}
int get_r()
{
return m_r;
}
void set_center(Point c)
{
center = c;
}
Point get_center()
{
return center;
}
private:
int m_r;
Point center;
};
void circle_point(Point& P,Circle& C)
{
int Distance = (P.get_x() - C.get_center().get_x()) * (P.get_x() - C.get_center().get_x()) +
(P.get_y() - C.get_center().get_y()) * (P.get_y() - C.get_center().get_y());
Distance = sqrt(Distance);
if (Distance > C.get_r())
{
cout << "点在圆外。" << endl;
}
else if (Distance == C.get_r())
{
cout << "点在圆上。" << endl;
}
else
{
cout << "点在圆内。" << endl;
}
}
int main(void)
{
Point P,C_P;
Circle C;
int C_x, C_y, C_r,P_x,P_y;
cout << "请输入圆心横坐标X:";
cin >> C_x;
cout << "请输入圆心纵坐标Y:";
cin >> C_y;
cout << "请输入圆半径:";
cin >> C_r;
cout << "请输入点横坐标X:";
cin >> P_x;
cout << "请输入点纵坐标Y:";
cin >> P_y;
C_P.set_x(C_x);
C_P.set_y(C_y);
C.set_center(C_P);
C.set_r(C_r);
P.set_x(P_x);
P.set_y(P_y);
circle_point(P,C);
return 0;
}
结果图:
注意:类之间可以做嵌套使用。但是在使用过程中很容易造成思路混乱。
总结
类很类似于结构体,很多时候它们能够通用,但是似乎来说,它们又有写区别:
- 结构体能够创造一个链表,但是我现在没有看到class类这样做
- class类可以定义行为函数,但是结构体内部似乎没有在内部构建函数的做法
- class类能够构建只读,但是struct就不行