C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心特性,通常被称为用户定义的类型。
类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。类中的数据和方法称为类的成员。函数在一个类中被称为类的成员。
类和对象
C++面向对象的三大特性为:封装、继承、多态。
它认为万事万物都皆为对象,对象上有其属性和行为。
例如:
人可以作为对象,属性有姓名、年龄、身高......,行为有走、跑、跳......
具有相同性质的对象,我们可以抽象称为类。人属于人类,车属于车类。
封装
封装的意义:
- 将属性(成员属性/成员变量)和行为(成员函数/成员方法)作为一个整体,表现生活中的事物
- 将属性和行为加以权限控制
封装意义一:
在设计类的时候,属性和行为写在一起,表现事物。
语法:class 类名{ 访问权限:属性 /行为 };
示例1:设计一个圆类,求圆的周长
# include<iostream>
using namespace std;
const double PI=3.14;
// 设计一个类
// 可见封装的意义,将圆的行为和属性放到一起,来表现圆
class Circle
{
// 访问权限
// 公共权限
public:
// 属性-半径
int r;
// 行为
// 获取圆的周长
double caculate()
{
return 2*PI*r;
}
};
int main(){
// 实例化(通过圆的类 创建具体的圆(对象))
Circle c1;
// 给圆对象的属性进行赋值
c1.r = 10;
cout << "圆的周长为: " << c1.caculate() << end1
system("pause");
return 0;
}
封装意义二:
类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:
1、public 公共权限 成员类内可以访问 类外可以访问
2 protected 保护权限 成员类内可以访问 类外不可以访问 儿子可以访问父亲中的保护内容
3 private 私有权限 成员类内可以访问 类外不可以访问 儿子不可以访问父亲中的私有内容
示例:
#include<iostream>
using namespace std;
#include <string>
class Person
{
public:
// 公共权限
string name;
protected:
// 保护权限
string car;
private:
// 私有权限
string passwword;
public:
void func(){
name = "张三";
car = "拖拉机"
password = 12345
}
};
int main() {
// 实例化对象
Person p1;
p1.name = "李四" // 保护权限内容,类外无法访问
p1.car = "红旗" // 私有权限内容,类外无法访问
p1.password = 123;
system("pause");
return 0;
}
struct和class的区别
在C++中struct和class唯一区别就在于默认的访问权限不同
区别:
- struct默认权限为公共
- class默认权限为私有
示例:
#include<iostream>
using namespace std;
#include <string>
class C1
{
int a;//什么都没有写,即是默认权限私有
};
struct C2
{
int b; // 默认权限是公共
}
int main() {
C1 c1;
c1.a=100; //报错,因为是私有权限
C2 c2;
c2.b=100; // 运行成功,因为权限是公共
system("pause");
return 0;
}
成员属性设置为私有
优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
#include <string>
class Person
{
public:
// 姓名可写
void setname(string n)
{
name = n;
}
// 姓名可读
voide getname()
{
return name;
}
// 年龄可读
int getage()
{
age = 0// 初始化
return age;
}
// 爱人只写
void setlover(string love)
{
lover = love;
}
private:
string name; // 设置为可读可写
int age; // 设置为只读
string lover; // 设置为只写
};
int main() {
Person p1;
p1.setname = "张三";
cout <<"姓名为:" <<p1.getname() << endl;
p1.age = 18; // 不能写,报错
cout << "年龄为:" <<p1.getage() <<endl;
p1.setlover("licy");
system("pause")
return 0;
}
练习案例:设计立方体类
求出立方体的面积和体积
分别用全局函数和成员函数判断两个立方体是否相等
#include<iostream>
using namespace std;
#include <string>
class Cube
{
private: // 属性一般设置为私有,然后提供公有的方法接口
int longth;
int width;
int height;
public: // 因为属性是私有,所以需要设置和获取长宽高
void setl(int l)
{
longth = l;
}
int getl()
{
return longth;
}
void setw(int w)
{
width = w;
}
int getw()
{
return width;
}
void seth(int h)
{
height = h;
}
intn geth()
{
return height;
}
int calcarea()
{
return 2*longth*width+2*longth*height+2*width*height;
}
int calcvoulumn()
{
return longth*width*height;
}
// 利用成员函数判断两个立方体是否相等
// 此时只需要一个参数,利用已知的对象调用成员函数,然后与传进来的参数进行比较
// 因为是在类内,可以访问私有属性
bool isSamebyClass(Cube &c)
{
if (height==c.geth() && longth==c.getl() && width==c.getw())
{
return true;
}
}
};
// 利用全局函数判断两个立方体是否相等(Bool函数)
// Cube c1, Cube c2 这种传入方式是值传入,是拷贝的数据
bool isSame(Cube &c1, Cube &c2) // 传入数据--引用方式(直接是原始数据)
{
if (c1.geth()==c2.geth() && c1.getl()==c2.getl() && c1.getw()==c2.getw())
{
return true;
}
}
int main() {
Cube c1;
c1.setl(10);
c1.setw(10);
c1.seth(10);
cout << "c1的面积是:" << c1.calcarea() << endl;
cout << "c1的体积是:" << c1.calcvoulumn() << endl;
Cube c2;
c2.setl(10);
c2.setw(10);
c2.seth(10);
bool ret = isSame(c1,c2);
if (ret)
{
cout << "c1和c2是相等的" << endl
}
else{
cout << "c1和c2是不等的" << endl
}
// 利用成员函数判断
bool ret =c1.isSamebyClass(c2);
if (ret)
{
cout << "成员函数c1和c2是相等的" << endl
}
else{
cout << "成员函数c1和c2是不等的" << endl
}
system("pause")
return 0;
}
练习案例-点与圆的关系
#include<iostream>
#include<string>
using namespace std;
//核心:在类中可以让另一个类作为本类的成员
//点类
class Point
{
public:
void set_x(int x)//设置点x
{
m_x = x;
}
void set_y(int y)//设置点y
{
m_y = y;
}
int get_x()//获取点x
{
return m_x;
}
int get_y()//获取点y
{
return m_y;
}
private:
int m_x;//点x
int m_y;//点y
};
//圆类
class circle
{
public:
void set_R(int r) //设置半径
{
m_R = r;
}
void set_P(Point p) //设置圆心
{
m_p = p;
}
int get_R() //获取半径
{
return m_R;
}
Point get_P() //获取圆心
{
return m_p;
}
private:
int m_R;
Point m_p; // // 利用点类定义圆心
};
//判断点和圆的关系,点到圆心的距离和半径作比较
void relationship(Point p, circle C)
{
//点到圆心的距离的平方: (x-x0)平方 + (y-y0)平方
int p_distance = ((p.get_x() - C.get_P().get_x()) * (p.get_x() - C.get_P().get_x()) +
(p.get_y() - C.get_P().get_y()) * (p.get_y() - C.get_P().get_y()));
//半径的平方
int r_distance = (C.get_R() * C.get_R());
if (p_distance > r_distance)
{
cout << "点在圆外" << endl;
}
if (p_distance < r_distance)
{
cout << "点在圆内" << endl;
}
if (p_distance == r_distance)
{
cout << "点在圆上" << endl;
}
}
void main()
{
Point C_p;//圆心
C_p.set_x(0);
C_p.set_y(0);
circle c;//圆
c.set_R(10);
c.set_P(C_p);
Point P;//设置一个点
P.set_x(0);
P.set_y(10);
relationship(P, c);//判断点和圆的关系
}
在大型的开发中,把所有的类都写在一个文件中,太大了,所以要做拆分
头文件中写声明,源文件中写实现
具体目录结构如下:
拆分成多个文件,类的声明在头文件中,类实现在源文件中。