一、定义
构造函数:在定义一个类对象时会自动调用,可用于实现一些功能,比如new一个内存。
- 构造函数,没有返回值也不写void
- 函数名称与类名相同
- 构造函数可以有参数,因此可以发生重载
- 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
析构函数:在类对象销毁时执行,可用于实现一些功能,比如delete一个内存。
- 析构函数,没有返回值也不写void
- 函数名称与类名相同,在名称前加上符号 ~
- 析构函数不可以有参数,因此不可以发生重载
- 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
#include <iostream>
using namespace std;
class Person
{
public:
Person() { cout << "无参构造函数" << endl; }
Person(int n_age) :age(n_age) { cout << "有参构造函数" << endl; }
Person(const Person& p) {
age = p.age;
cout << "复制构造函数" << endl;
}
~Person() { cout << "析构函数" << endl; }
void show_age(int n_age)
{
this->age = n_age;
cout << n_age << endl;
}
private:
int age;
};
int main()
{
// 1、无参构造函数
Person p1;
// 2、有参构造函数
Person p2(10);
// 3、复制构造函数
Person p3(p2);
p3.show_age(120);
// 构造函数的定义
// 1、括号
Person p4(10);
// 2、显式法
Person p5 = Person(100);
// 3、隐式法(当类只有一个参数时,可以使用隐式法)
Person p6 = 10;
cout << "函数即将结束,开始析构" << endl;
return 0;
}
1.2 explicit关键字
可以取消类的隐式构造
#include <iostream>
#include <string>
using namespace std;
class Phone
{
public:
Phone(string name) {
this->phone_name = name;
}
Phone() {}
~Phone() {}
string phone_name;
};
int main()
{
string name = "huawei";
Phone p = name;
cout << p.phone_name << endl;
return 0;
}
加入explicit关键字后可以取消隐式构造
#include <iostream>
#include <string>
using namespace std;
class Phone
{
public:
explicit Phone(string name) {
this->phone_name = name;
}
Phone() {}
~Phone() {}
string phone_name;
};
int main()
{
string name = "huawei";
// 错误,此时不能隐式构造
Phone p = name;
cout << p.phone_name << endl;
return 0;
}
二、复制构造函数
C++中拷贝构造函数调用时机通常有三种情况
- 使用一个已经创建完毕的对象来初始化一个新对象
- 值传递的方式给函数参数传值
- 以值方式返回局部对象
#include <iostream>
using namespace std;
class Person
{
public:
Person() { cout << "无参构造函数" << endl; }
Person(int n_age) :age(n_age) { cout << "有参构造函数" << endl; }
Person(const Person& p) {
age = p.age;
cout << "复制构造函数" << endl;
}
~Person() { cout << "析构函数" << endl; }
void show_age(int n_age)
{
this->age = n_age;
cout << n_age << endl;
}
private:
int age;
};
void test01(Person p1)
{
cout << "类对象作为函数形参传递,调用复制构造函数" << endl;
return;
}
Person test02()
{
Person p2(100);
cout << "函数返回值为类对象时,调用复制构造函数" << endl;
return p2;
}
int main()
{
Person p(100);
Person p1(p);
test01(p);
Person p2 = test02();
return 0;
}
三、浅拷贝/深拷贝
当类中含有指针类型成员变量时,需要进行深拷贝:
3.1 浅拷贝
#include <iostream>
#include <math.h>
using namespace std;
class Person
{
public:
Person() { }
Person(int n_age,int n_height) {
age = n_age;
height = new int(n_height);
}
Person(const Person& p) {
age = p.age;
// 浅拷贝
height = p.height;
}
~Person() {
if (height != NULL)
{
cout << "释放内存" << endl;
delete height;
}
}
void show_age(int n_age)
{
this->age = n_age;
cout << n_age << endl;
}
private:
int age;
int* height;
};
int main()
{
Person p(23,160);
Person p1(p);
return 0;
}
浅拷贝中,p和p1的height指向同一个内存,当p释放掉内存中的数据之后,p1所指向的内存中的数据为空,此时再释放的话会报错,因为这片内存中的数据已经不存在了。(注意,释放的不是内存,而是内存中的数据)
3.2 深拷贝
#include <iostream>
#include <math.h>
using namespace std;
class Person
{
public:
Person() { }
Person(int n_age,int n_height) {
age = n_age;
height = new int(n_height);
}
Person(const Person& p) {
age = p.age;
// 深拷贝
height = new int(*(p.height));
}
~Person() {
if (height != NULL)
{
cout << "释放内存" << endl;
delete height;
}
}
void show_age(int n_age)
{
this->age = n_age;
cout << n_age << endl;
}
private:
int age;
int* height;
};
int main()
{
Person p(23,160);
Person p1(p);
return 0;
}
四、类对象作为类成员
先调用成员类的构造,然后是该类的构造。析构顺序相反。
#include <iostream>
using namespace std;
class Phone
{
public:
Phone(string n_name) :phone_name(n_name) {
cout << "Phone构造函数" << endl;
}
Phone() {}
~Phone() {
cout << "Phone析构函数" << endl;
}
private:
string phone_name;
};
class Person
{
public:
Person() {}
Person(string n_name, string n_phone):m_name(n_name),m_phone(n_phone) {
cout << "Person构造函数" << endl;
}
~Person(){
cout << "Person析构函数" << endl;
}
private:
string m_name;
Phone m_phone;
};
int main()
{
Person person1("xiaoming", "huawei");
return 0;
}
五、this指针
5.1 this指针使用的原因
在类中,非静态成员变量属于类对象,而非静态成员函数为所有类对象共享,不属于某个类对象:
#include <iostream>
#include <string>
using namespace std;
class Phone
{
public:
explicit Phone(string name) {
this->phone_name = name;
}
Phone() {}
~Phone() {}
void test_func() { cout << "成员函数不占用类对象的内存" << endl; }
string phone_name;
};
int main()
{
string name = "huawei";
// 错误,此时不能隐式构造
cout << sizeof(name) << endl;
Phone p(name);
// 可见类对象只占有一个string类型变量的内存
cout << sizeof(p) << endl;
return 0;
}
5.2 this指针
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
this指针的作用:
用于区分是哪个类对象调用了成员函数
this指针本质上是一个指针常量,因此其指向的对象不能变,指向的对象的值可以变
this指针的用途
当形参和成员变量同名时,可用this指针来区分
在类的非静态成员函数中返回对象本身,可使用return *this
#include <iostream>
#include <string>
using namespace std;
class Phone
{
public:
explicit Phone(string name) {
this->phone_name = name;
}
Phone() {}
~Phone() {}
void put_name(string phone_name) {
// this->phone_name表示类的成员变量
this->phone_name = phone_name;
}
// 使用this指针返回类自身
Phone& get_phone_info()
{
this->phone_name += "10";
return *this;
}
string phone_name;
};
int main()
{
Phone p;
p.put_name("华为");
cout << p.phone_name << endl;
p.get_phone_info().get_phone_info().get_phone_info();
cout << p.phone_name << endl;
return 0;
}
六、友元
友元的目的就是让一个函数或者类 访问另一个类中私有成员
友元的三种实现
- 全局函数做友元
- 类做友元
- 成员函数做友元
6.1全局函数做友元
#include <iostream>
#include <string>
using namespace std;
class room
{
// 表明全局函数visit时友元,可以访问私有变量
friend void visit(room myroom);
public:
room() {}
room(string bedr) :bedroom(bedr) {}
~room() {}
private:
string bedroom;
};
void visit(room myroom)
{
cout << "go to " << myroom.bedroom << endl;
}
int main()
{
room myroom("dk's room");
visit(myroom);
return 0;
}
6.2类做友元
#include <iostream>
#include <string>
using namespace std;
class room;
class person
{
public:
person() {}
person(string per) :name(per) {}
~person() {}
void visit();
private:
string name;
// 这里必须是指针,因为如果是变量的话
// 编辑器不知道room类占了多少内存
// 也就没法开辟内存
room* myroom;
};
class room
{
// person类时友元,可以访问room类的私有变量
friend class person;
public:
room() {}
~room() {}
private:
string bedroom;
};
void person::visit()
{
this->myroom = new room;
this->myroom->bedroom = "bedroom";
// 此时person类中的room类对象myroom可以访问room类中的私有变量
cout << this->name << " is visiting " << this->myroom->bedroom << endl;
return;
}
int main()
{
person myfriend("liming");
myfriend.visit();
return 0;
}
6.3成员函数做友元
#include <iostream>
#include <string>
using namespace std;
class room;
class person
{
public:
person() {}
person(string per) :name(per) {}
~person() {}
void visit();
private:
string name;
// 这里必须是指针,因为如果是变量的话
// 编辑器不知道room类占了多少内存
// 也就没法开辟内存
room* myroom;
};
class room
{
// person类中的visit成员函数做友元,可以访问room类的私有变量
friend void person::visit();
public:
room() {}
~room() {}
private:
string bedroom;
};
void person::visit()
{
this->myroom = new room;
this->myroom->bedroom = "bedroom";
// 此时person类中的room类对象myroom可以访问room类中的私有变量
cout << this->name << " is visiting " << this->myroom->bedroom << endl;
return;
}
int main()
{
person myfriend("liming");
myfriend.visit();
return 0;
}
七、运算符重载
7.1 加法运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
Person() {}
Person(int m_age):n_age(m_age) {}
~Person() {}
//相当于一个成员函数 +运算符重载1 p1.operator+(p2)
Person operator+(Person p)
{
Person temp;
temp.n_age = this->n_age + p.n_age;
return temp;
}
public:
int n_age;
};
// 相当于一个函数 operator+(p,age)
Person operator+(Person p,int age)
{
Person temp;
temp.n_age = p.n_age + age;
return temp;
}
int main()
{
Person p1(20);
Person p2(20);
Person p3 = p1 + p2;
cout << p3.n_age << endl;
Person p4 = p1 + 20;
cout << p4.n_age << endl;
}
7.2 输出运算符重载
#include<iostream>
using namespace std;
class Person
{
public:
Person() {}
Person(int m_age):n_age(m_age) {}
~Person() {}
public:
int n_age;
};
ostream& operator<<(ostream& out, Person p)
{
out << "age is " << p.n_age << endl;
return out;
}
int main()
{
Person p1(20);
cout << p1 << "20" << endl;
}
7.3 函数调用运算符重载(仿函数)
- 函数调用运算符 () 也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;
class mycomp
{
public:
void operator()(int a,int b)
{
cout << a + b << endl;
}
};
int main()
{
mycomp com1;
com1(10, 20);
return 0;
}
八、多态
多态是C++的重要特征,与封装、继承并称为C++的三大特征
多态分为静态多态、动态多态
静态多态:函数的地址的编译时刻确定,主要包括 函数重载 和 运算符重载
动态多态:函数的地址在执行时刻确定,通过 派生类 和 虚函数实现
动态多态满足的条件
(1)存在继承关系
(2)子类重写父类的虚函数(函数名,返回值类型,形参完全相同)
注意,这里和继承中的同名函数重写不同。如果是同名函数的重写,父类和子类的同名函数的地址在编译阶段就固定了。而多态中,父类中的虚函数的地址在编译的时候是不确定的。
其类似于函数的重载
多态优点:代码组织结构清晰,可读性强,利于前期和后期的扩展以及维护(不用去修改源码,直接在子类中重写虚函数即可)
多态使用条件:父类的指针或引用指向子类的对象
8.1 多态示例
8.1.1示例1
#include<iostream>
#include<string>
using namespace std;
class game
{
public:
virtual void func(string name)
{
cout << "I like playing " << name << endl;
}
};
class BH3:public game
{
public:
BH3(string name) :n_name(name) {}
void func(string name)
{
cout << "I like playing " << this->n_name << endl;
}
string n_name;
};
class YS:public game
{
public:
YS(string name) :n_name(name) {}
void func(string name)
{
cout << "I like playing " << this->n_name << endl;
}
string n_name;
};
void test_func(game& mygame)
{
mygame.func("game");
}
int main()
{
BH3 bh3("崩坏3");
test_func(bh3);
YS ys("原神");
test_func(ys);
return 0;
}
8.1.2 示例2
#include<iostream>
using namespace std;
// 运算器基类
class calculate
{
public:
virtual int getresult(int x1,int x2)
{
return 0;
}
};
// 加法运算器
class addcalculate:public calculate
{
public:
int getresult(int x1,int x2)
{
return x1 + x2;
}
};
// 减法运算器
class subcalculate :public calculate
{
public:
int getresult(int x1, int x2)
{
return x1 - x2;
}
};
int main()
{
// 构建一个加法运算器
// 父类的指针或引用指向子类的对象
calculate* cal1 = new addcalculate();
cout << "加法结果" << cal1->getresult(10, 20) << endl;
// 构建一个减法运算器
calculate* cal2 = new subcalculate();
cout << "减法结果" << cal2->getresult(10, 20) << endl;
}
8.2 纯虚函数/抽象类
在多态中,父类中的虚函数一般不会使用,使用的是子类中重写的虚函数。
所以,一般将父类的虚函数写为纯虚函数,格式为:
virtual 返回值类型 函数名 (参数列表)= 0 ;
含有纯虚函数的类称为抽象类
抽象类的特点:
子类必须重写抽象类中的纯虚函数,否则子类也是抽象类
抽象类无法进行初始化
#include<iostream>
using namespace std;
class Calculator
{
public:
// 纯虚函数,此时Calculator为抽象类,无法进行初始化
virtual int calculate(int x1, int x2) = 0;
};
class AddCalculator :public Calculator
{
public:
virtual int calculate(int x1, int x2)
{
return x1 + x2;
}
};
class SubCalculate:public Calculator
{
public:
virtual int calculate(int x1, int x2)
{
return x1 - x2;
}
};
int main()
{
// 错误,抽象类无法进行初始化
// Calculator* cal = new Calculator();
Calculator* Add_Cal = new AddCalculator;
cout << Add_Cal->calculate(10, 20) << endl;
Calculator* Sub_Cal = new SubCalculate;
cout << Sub_Cal->calculate(10, 20) << endl;
}
8.3 虚析构/纯虚析构
多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码。
此时需要在父类中设置虚析构。
虚析构和纯虚析构共性:
- 可以解决父类指针释放子类对象
- 都需要有具体的函数实现
虚析构和纯虚析构区别:
- 如果是纯虚析构,该类属于抽象类,无法实例化对象
#include<iostream>
using namespace std;
// 运算器基类
class calculate
{
public:
calculate() { cout << "父类的构造函数" << endl; }
~calculate() { cout << "父类的析构函数" << endl; }
virtual int getresult(int x1, int x2)
{
return 0;
}
};
// 加法运算器
class addcalculate :public calculate
{
public:
addcalculate() { cout << "子类的构造函数" << endl; }
~addcalculate() { cout << "子类的析构函数" << endl; }
int getresult(int x1, int x2)
{
return x1 + x2;
}
};
int main()
{
calculate* calculator = new addcalculate;
delete calculator;
}
此时的输出为:
父类的构造
子类的构造
父类的析构
此时没有调用子类的析构,如果子类在构造中开辟了内存,在析构中释放了内存。此时子类中开辟的内存无法被释放,从而产生内存的泄露。
为解决此问题,将父类的析构函数改为虚析构函数。
#include<iostream>
using namespace std;
// 运算器基类
class calculate
{
public:
calculate() { cout << "父类的构造函数" << endl; }
virtual ~calculate() { cout << "父类的析构函数" << endl; }
int getresult(int x1, int x2)
{
return 0;
}
virtual int* test() { return NULL; }
};
// 加法运算器
class addcalculate :public calculate
{
public:
addcalculate() {
cout << "子类的构造函数" << endl;
val = new int(10);
}
~addcalculate() {
cout << "子类的析构函数" << endl;
delete val;
}
int getresult(int x1, int x2)
{
return x1 + x2;
}
int* test()
{
return val;
}
int* val;
};
int main()
{
calculate* calculator = new addcalculate();
int* space = calculator->test();
delete calculator;
cout << *space << endl;
}
此时*space为一个垃圾数据,说明该内存内部的数据已经被释放。如果父类不是虚析构函数的话,输出就会是10,说明内存中的数据没有被释放。