类和对象
一、封装
1.1 封装定义-属性和行为
封装的意义:
- 将属性和行为作为一个整体,表现生活中的事物
- 将属性和行为加以权限控制
语法: class 类名{ 访问权限: 属性 / 行为 };
示例1:设计一个圆类,求圆的周长
#include<iostream>
using namespace std;
//class代表设计一个类,后面跟着的是类名
class circle
{
public: //访问权限 公共的权限
//---------属性-----------//
int r;
double pi = 3.1415926;
//---------行为-----------//
double circlezc()
{
//获取圆的周长
return 2 * pi * r;
}
};
int main()
{
circle c1;
c1.r = 10;
//返回的是2 * pi * r;
cout << "圆的周长为" <<c1.circlezc()<< endl;
system("pause");
return 0;
}
示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
#include<iostream>
#include<string>
using namespace std;
//学生类
class student
{
public:
//----------属性--------------//
string s_ID;
string s_name;
void showstudent()
{
cout<< "名字:"<< s_name <<"\t学号:"<< s_ID << endl;
}
void setname(string name)
{
s_name = name;
}
void setID(string ID)
{
s_ID = ID;
}
};
int main()
{
student stu;
//stu.s_name = "李东岳";
stu.setname("李东岳");
//stu.s_ID = 1;
stu.setID ("20184080716");
//显示学生信息;
stu.showstudent();
system("pause");
return 0;
}
/*
程序输出:
名字:李东岳 学号:20184080716
*/
1.2 访问权限
访问权限有三种:
公共权限 public ==类内可以访问 类外可以访问==
保护权限 protected ==类内可以访问 类外不可以访问==
私有权限 private ==类内可以访问 类外不可以访问==
#include<iostream>
using namespace std;
class person //默认权限为私有
{
public: //公共权限 类内可以访问 类外可以访问
string name;
protected://保护权限 类内可以访问 类外不可以访问
string car;
private://私有权限 类内可以访问 类外不可以访问
int password;
protected:
void func()
{
name = "张三";
car = "拖拉机";
password = 123456;
}
};
int main()
{
person p1;
p1.name="张三";
//p1.car ="拖拉机" ; 保护权限类外访问不到
//p.m_Password = 123; //私有权限类外访问不到
system("pause");
return 0;
}
1.3 struct和class区别
在C++中 struct和class唯一的区别就在于 默认的访问权限不同
区别:
- struct 默认权限为公共
- class 默认权限为私有
1.4 成员属性设置为私有
优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
#include<string>
class person
{
public:
//设置姓名
void setname(string name)
{
s_name = name;
}
//获取姓名
string getname()
{
return s_name;
}
//设置年龄;可读可写;
void setage(int age)
{
if (age < 0 || age > 150)
{
cout << "你个老妖精!" << endl;
return;
}
s_age = age;
}
//获取年龄
int getage()
{
return s_age;
}
//情人设置为只写;
void setlover(string lover)
{
s_lover = lover;
}
private:
//姓名 可读可写;
string s_name;
int s_age; //只读
string s_lover;
};
int main()
{
person per;
//给name赋值.
per.setname( "李东岳");
cout << "\t名字:" << per.getname() << endl;
//给age赋值
per.setage(2000);
cout << "\t年龄:" << per.getage() << endl;
//给lover赋值;
per.setlover("苍老师");
//cout << "\t情人:" << per.s_lover << endl; 只读属性 不可以访问;
system("pause");
return 0;
}
注意:设置一次就要获取一次。
练习案例1:设计立方体类
设计一个立方体类 求立方体(cube)的面积以及体积
#include<iostream>
using namespace std;
#include<string>
class cube
{
public:
//---------行为--------//
//设置长
void setL(int w_l)
{
c_L = w_l;
}
//获取长
int getL()
{
return c_L;
}
//设置宽
void setW(int w_w)
{
c_W = w_w;
}
//获取宽
int getW()
{
return c_W;
}
//设置高
void setH(int w_h)
{
c_H = w_h;
}
//获取高
int getH()
{
return c_H;
}
//获取面积(公式)
int getarea()
{
return c_L * c_W * 2 + c_L * c_H * 2 + c_H * c_W * 2;
}
//获取体积
int getbulk()
{
return c_L * c_W*c_H;
}
//用成员函数来判断两个函数是否相等;可以在类内访问
/*bool issameClass(cube &cub)
{
if (c_L() == cub.getL() && c_W() == cub.getW() && c_H() == cub.getH())
{
return true;
}
return false;
}*/
//--------属性-------//
//定义属性
private:
int c_L;
int c_W;
int c_H;
int c_area;
int c_bulk;
};
//利用全局函数判断两个立方体是否相等;
//bool返回值类型是 true; false;
bool issame(cube cub1, cube cub2)
{
if (cub1.getL() == cub2.getL() && cub1.getW() == cub2.getW() && cub1.getH() == cub2.getH())
{
return true;
}
return false;
}
int main()
{
//----------那么我就定义一个外部函数 让他可以进行输入--------//
int w_l, w_w, w_h;
//------------注意:只有public 可以输入-----------//
//创建第一个立方体;
cube cub1;
cout << "请输入第一个立方体的长宽高:" << endl;
cout << "长:" ;
cin >> w_l;
cub1.setL(w_l);
cout << "宽:";
cin >> w_w;
cub1.setW(w_w);
cout << "高:";
cin >> w_h;
cub1.setH(w_h);
//cub1.setL(2);
//cub1.setW(7);
//cub1.setH(6);
cout << "立方体1的面积为:"<<cub1.getarea() << endl;
cout << "立方体1的体积为:" << cub1.getbulk() << endl;
//创建第二个立方体;
cube cub2;
cout << "请输入第二个立方体的长宽高:" << endl;
cout << "长:";
cin >> w_l;
cub2.setL(w_l);
cout << "宽:";
cin >> w_w;
cub2.setW(w_w);
cout << "高:";
cin >> w_h;
cub2.setH(w_h);
//cub2.setL(6);
//cub2.setW(7);
//cub2.setH(5);
cout << "立方体2的面积为:" << cub2.getarea() << endl;
cout << "立方体2的体积为:" << cub2.getbulk() << endl;
//用全局函数来判断,用布尔型调用函数;
bool ret = issame(cub1, cub2);
if (ret)
{
cout << "全局函数判断---两个立方体相等:" << endl;
}
else
{
cout << "全局函数判断---两个立方体不相等:" << endl;
}
//用成员函数来判断,用布尔型调用函数;
/* ret = cub1.issameClass(cub2);
if (ret)
{
cout << "成员函数判断-两个立方体相等:" << endl;
}
else
{
cout << "成员函数判断-两个立方体不相等:" << endl;
}
*/
//issame(cub1,cub2);
system("pause");
return 0;
}
练习案例2:点和圆的关系
#include<iostream>
using namespace std;
//先定义一个点的类;
class point //也属于一个自己定义的类型
{
public:
//设置x;
void setx(int x)
{
p_x = x;
}
//获取x;
int getx()
{
return p_x;
}
//设置y;
void sety(int y)
{
p_y = y;
}
//获取y;
int gety()
{
return p_y;
}
private:
int p_x;
int p_y;
};
//先定义一个圆的类;
class circle
{
public:
//设置半径;
void setr(int r)
{
c_r = r;
}
//获取半径;
int getr()
{
return c_r;
}
//设置圆心;
void setcenter(point center)
{
c_center = center;
}
//获取圆心;
point getcenter()
{
return c_center;
}
private:
//定义一个半径;
int c_r;
point c_center;//圆心
};
//判断点和圆的位置关系;
//------------------圆------------点--//
void isincircle(circle &c, point &p)
{
//计算点到圆心的距离;
//获取点到圆心的距离
int distence = (c.getcenter().getx() - p.getx())*(c.getcenter().getx() - p.getx()) +
(c.getcenter().gety() - p.gety())*(c.getcenter().gety() - p.gety());
//计算半径的平方;
//获取半径的平方
int R1 = c.getr()*c.getr();
//比较点到圆心的距离;
if (distence == R1)
{
cout << "则点在圆上!" << endl;
}
else if (distence < R1)
{
cout << "则点在圆内!" << endl;
}
else
{
cout << "则点在圆外!" << endl;
}
}
int main()
{
circle c;
point center;
//设置圆心的点
c.setr(10);
center.setx(10);
center.sety(10);
c.setcenter(center);
//设置一个点
point p;
p.setx(10);
p.sety(5);
//在主函数里调用这个数
isincircle(c, p);
system("pause");
return 0;
}
二、对象特性
- 生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
- C++中的面向对象来源于生活,每个对象也都会有初始设置以及 对象销毁前的清理数据的设置。
2.1 构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
- 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
- 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作
构造函数语法:类名(){}
- 构造函数,没有返回值也不写void
- 函数名称与类名相同
- 构造函数可以有参数,因此可以发生重载
- 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
析构函数语法: ~类名(){}
- 析构函数,没有返回值也不写void
- 函数名称与类名相同,在名称前加上符号 ~
- 析构函数不可以有参数,因此不可以发生重载
- 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
#include<iostream>
using namespace std;
//先定义一个类;
class person
{
public:
//1. 构造函数,没有返回值也不写void
//函数名称与类名相同
//构造函数可以有参数,因此可以发生重载
//程序在调用对象时候会自动调用构造,无须手动调用, 而且只会调用一次
person()
{
cout << "构造函数,没有返回值也不写void" << endl;
}
//2. 析构函数
//对象早在销毁前,会自动调用析构函数,而且只能调用一次;
~person()
{
cout << "构造函数,没有返回值也不写void" << endl;
}
};
void test1()
{
person p;
}
int main()
{
test1();
//person p;
system("pause");
return 0;
}
2.2 函数的分类及调用
两种分类方式:
按参数分为: 有参构造和无参构造
按类型分为: 普通构造和拷贝构造
三种调用方式:
括号法
显示法
隐式转换法
构造函数调用规则如下:
如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
如果用户定义拷贝构造函数,c++不会再提供其他构造函数
#include<iostream>
using namespace std;
class person
{
public:
//构造一个函数;
person()
{
cout << "无参构造函数调用!" << endl;
}
//构造一个有参函数
person(int a)
{
p_age = a;
cout << "有参构造函数调用" << endl;
}
//构造一个拷贝构造函数;
person(const person&p)
{
p_age = p.p_age;
cout << "拷贝构造函数调用!" << endl;
}
//构造析构函数;
~person()
{
cout << "析构函数调用调用!" << endl;
}
int p_age;
};
//*--------使用一个已经创建完毕的对象来初始化一个新对象---------//
void test01()
{
//如果不写拷贝构造,编译器会自动添加拷贝构造,并且做浅拷贝操作
person p1(2);
person p2(p1);
cout << "p2的年龄为: " << p2.p_age << endl;
}
void test02()
{
//如果用户提供有参构造,编译器不会提供默认构造,会提供拷贝构造
person p1; //此时如果用户自己没有提供默认构造,会出错
person p2(10); //用户提供的有参
person p3(p2); //此时如果用户没有提供拷贝构造,编译器会提供
//如果用户提供拷贝构造,编译器不会提供其他构造函数
person p4; //此时如果用户自己没有提供默认构造,会出错
person p5(10); //此时如果用户自己没有提供有参,会出错
person p6(p5); //用户自己提供拷贝构造
}
int main()
{
test01();
system("pause");
return 0;
}
2.2 拷贝构造函数调用时机
C++中拷贝构造函数调用时机通常有三种情况
- 使用一个已经创建完毕的对象来初始化一个新对象
- 值传递的方式给函数参数传值
- 以值方式返回局部对象
#include<iostream>
using namespace std;
//*使用一个已经创建完毕的对象来初始化一个新对象
//*值传递的方式给函数参数传值
//*以值方式返回局部对象
class person
{
public:
//构造一个函数;
person()
{
cout << "无参构造函数调用!" << endl;
}
//构造一个有参函数
person(int a)
{
p_age = a;
cout << "有参构造函数调用" << endl;
}
//构造一个拷贝构造函数;
person(const person&p)
{
p_age = p.p_age;
cout << "拷贝构造函数调用!" << endl;
cout << "p_age =" << p_age<<endl;
}
//构造析构函数;
~person()
{
cout << "析构函数调用调用!" << endl;
}
int p_age;
};
//*--------使用一个已经创建完毕的对象来初始化一个新对象---------//
void test01()
{
person p1(2);
person p2(p1);
}
//*值传递的方式给函数参数传值 相当于person p1 = p
void work(person p1)
{
}
void test02()
{
person p;
work(p);
}
//*以值方式返回局部对象
person doWork2()
{
person p1;
cout << (int *)&p1 << endl;
return p1;
}
void test03()
{
person p = doWork2();
cout << (int *)&p << endl;
}
int main()
{
cout << "tese 01" << endl;
test01();
cout << "tese 02" << endl;
test02();
cout << "tese 03" << endl;
test03();
system("pause");
return 0;
}
/*
程序输出:
tese 01
有参构造函数调用
拷贝构造函数调用!
p_age =2
析构函数调用调用!
析构函数调用调用!
tese 02
无参构造函数调用!
拷贝构造函数调用!
p_age =-858993460
析构函数调用调用!
析构函数调用调用!
tese 03
无参构造函数调用!
012FF748
拷贝构造函数调用!
p_age =-858993460
析构函数调用调用!
012FF840
析构函数调用调用!
*/
2.3 构造函数调用规则
构造函数调用规则如下:
- 如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
- 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
#include<iostream>
using namespace std;
class person
{
public:
//构造一个函数;
person()
{
cout << "无参构造函数调用!" << endl;
}
//构造一个有参函数
person(int a)
{
p_age = a;
cout << "有参构造函数调用" << endl;
}
//构造一个拷贝构造函数;
person(const person&p)
{
p_age = p.p_age;
cout << "拷贝构造函数调用!" << endl;
}
//构造析构函数;
~person()
{
cout << "析构函数调用调用!" << endl;
}
int p_age;
};
//*--------使用一个已经创建完毕的对象来初始化一个新对象---------//
void test01()
{
//如果不写拷贝构造,编译器会自动添加拷贝构造,并且做浅拷贝操作
person p1(2);
person p2(p1);
cout << "p2的年龄为: " << p2.p_age << endl;
}
void test02()
{
//如果用户提供有参构造,编译器不会提供默认构造,会提供拷贝构造
person p1; //此时如果用户自己没有提供默认构造,会出错
person p2(10); //用户提供的有参
person p3(p2); //此时如果用户没有提供拷贝构造,编译器会提供
//如果用户提供拷贝构造,编译器不会提供其他构造函数
person p4; //此时如果用户自己没有提供默认构造,会出错
person p5(10); //此时如果用户自己没有提供有参,会出错
person p6(p5); //用户自己提供拷贝构造
}
int main()
{
test01();
system("pause");
return 0;
}
2.4 深拷贝与浅拷贝
浅拷贝:简单的赋值拷贝操作(缺点就是不能拷贝堆区的空间)
深拷贝:在堆区重新申请空间,进行拷贝操作
#include<iostream>
using namespace std;
//先定义一个类;
class person
{
public:
//1. 构造函数,没有返回值也不写void
person()
{
cout << "构造函数,没有返回值也不写void" << endl;
}
person(int a,int height)
{
p_age = a;
p_height = new int(height); //堆区的数据由程序员手动开辟数据
cout << "有参构造函数调用" << endl;
}
//自己实现拷贝构造函数;
person(const person&p)
{
p_age = p.p_age;
//深拷贝操作;
//p_height = p.p_height; 编译器默认实现的就是这行代码;
p_height = new int(*p.p_height);
}
//2. 析构函数
//对象早在销毁前,会自动调用析构函数,而且只能调用一次;
~person()
{
//析构代码,将堆区开辟的数据做释放操作;
if (p_height != NULL)
{
delete p_height;
}
cout << "构造函数,没有返回值也不写void" << endl;
}
int p_age;
int *p_height;
};
void test01()
{
person p1(10,180);
cout << "p1的年龄为:" <<p1.p_age<<endl<<"p1的身高为:"<<*p1.p_height<< endl;
person p2(p1);
cout << "p1的年龄为:" << p2.p_age << endl << "p1的身高为:" << *p2.p_height << endl;
}
int main()
{
test01();
//person p;
system("pause");
return 0;
}
/*
程序输出:
有参构造函数调用
p1的年龄为:10
p1的身高为:180
p1的年龄为:10
p1的身高为:180
构造函数,没有返回值也不写void
构造函数,没有返回值也不写void
*/
2.5 初始化列表
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)... {}
class Person {
public:
Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c)
{
}
private:
int m_A;
int m_B;
int m_C;
}
int main()
{
Person p(1, 2, 3);
p.PrintPerson();
system("pause");
return 0;
}
2.6 类对象作为类成员
#include<iostream>
using namespace std;
#include<string>
//类对象作为类成员;
//手机类
class phone
{
public:
phone(string pname)
{
m_pname = pname;
cout << "phone 的构造函数调用!" << endl;
}
~phone()
{
cout << "phone 的析构函数:" << endl;
}
//手机品牌名称;
string m_pname;
};
//人类
class person
{
public:
person(string name,string pname):m_name(name),m_phone(pname)
{
cout << "person 的构造函数调用!" << endl;
}
~person()
{
cout << "person 的析构函数:" << endl;
}
public:
//手机主人的名字
string m_name;
//手机型号
phone m_phone;
};
void test01()
{
person p1("李东岳", "华为");
cout << "姓名:" << p1.m_name << "手机:" << p1.m_phone.m_pname<<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
/*
程序输出:
phone 的构造函数调用!
person 的构造函数调用!
姓名:李东岳手机:华为
person 的析构函数:
phone 的析构函数:
*/
//当类中成员是其他类对象时,我们称该成员为 对象成员
//构造的顺序是 :先调用对象成员的构造,再调用本类构造
//析构顺序与构造相反
2.7 静态成员函数
静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员
静态成员分为:
- 静态成员变量
- 所有对象共享同一份数据
- 在编译阶段分配内存
- 类内声明,类外初始化
- 静态成员函数
- 所有对象共享同一个函数
- 静态成员函数只能访问静态成员变量
#include<iostream>
using namespace std;
#include<string>
class person
{
public:
//静态成员函数;
static void func1()
{
p_name = 100; //静态成员可以访问静态成员变量;
//pp = name; 静态成员函数不可以访问非静态成员变量
cout << "static void func1() 的静态成员函数调用;" << endl;
}
static int p_name; //类内声明 类外必须初始化一下;
//int pp;
private:
static void func2()
{
cout << "static void func2() 的静态成员函数调用;" << endl;
}
};
int person::p_name = 0; //类内声明 类外必须初始化一下;
void test01()
{
person p;
p.func1();
//通过类名进行访问
person::func1();
//类外访问不到私有静态函数;
//person::func2(); 不可访问
}
int main()
{
test01();
system("pause");
return 0;
}
/*
程序输出:
static void func1() 的静态成员函数调用;
static void func1() 的静态成员函数调用;
*/
成员变量和成员函数分开存储
在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上
非静态成员变量占对象空间
静态成员变量不占对象空间
函数也不占对象空间,所有函数共享一个函数实例
静态成员函数也不占对象空间
2.8 this指针概念
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
- 当形参和成员变量同名时,可用this指针来区分
- 在类的非静态成员函数中返回对象本身,可使用return *this
this 就是指向自己本身的值;
#include<iostream>
using namespace std;
#include<string>
class person
{
public:
person(int age)
{ //this 指针指向的是被调用的成员函数所属的对象;
this->age = age;
}
//------------把person 传进去----------//
person& addperson(person &p)
{
this->age += p.age;
//-----------返回对象本身-------------//
return *this;
}
void ShowClassName()
{
cout << "我是Person类!" << endl;
}
//---------空指针访问成员函数------------//
void ShowPerson()
{
if (this == NULL)
{ //空指针可以访问成员函数;
return;
}
cout << mage << endl;
}
int age;
int mage;
};
void test01()
{
person p1(10);
cout << "P1 =" << p1.age << endl;
}
//返回对象本身用 *this;
void test02()
{
person p1(10);
person p2(15);
p2.addperson(p1).addperson(p1);
//----输出年龄-----//
//-----链式编程思想--------//
cout << "addperson P2 " << p2.age << endl;
}
void test03()
{
person * p = NULL;
p->ShowClassName(); //空指针,可以调用成员函数
p->ShowPerson(); //但是如果成员函数中用到了this指针,就不可以了
}
int main()
{
test01();
test02();
test03();
system("pause");
return 0;
}
/*
程序输出:
P1 =10
addperson P2 35
我是Person类!
*/
注意:C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针 如果用到this指针,需要加以判断保证代码的健壮性;
判断方式:
if (this == NULL)
{ //空指针可以访问成员函数;
return;
}
2.9 const修饰成员函数
常函数:
- 成员函数后加const后我们称为这个函数为常函数
- 常函数内不可以修改成员属性
- 成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
- 声明对象前加const称该对象为常对象
- 常对象只能调用常函数
class Person
{
public:
Person()
{
m_A = 0;
m_B = 0;
}
//this指针的本质是一个指针常量,指针的指向不可修改
//如果想让指针指向的值也不可以修改,需要声明常函数
void ShowPerson() const {
//const Type* const pointer;
//this = NULL; //不能修改指针的指向 Person* const this;
//this->mA = 100; //但是this指针指向的对象的数据是可以修改的
//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
this->m_B = 100;
}
void MyFunc()
{
//mA = 10000;
}
public:
int m_A;
mutable int m_B; //可修改 可变的
};
//const修饰对象 常对象
void test01()
{
const Person person; //常量对象
cout << person.m_A << endl;
//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
person.m_B = 100; //但是常对象可以修改mutable修饰成员变量
//常对象访问成员函数
person.MyFunc(); //常对象不能调用普通函数
}
int main()
{
test01();
system("pause");
return 0;
}