C++系列:核心编程

C++核心编程学习记录



前言

学习视频链接:黑马程序员匠心之作|C++教程


一、内存分区模型


二、引用


三、函数提高


四、类和对象


4.1封装


4.2对象的初始化和清理


4.2.3 拷贝构造函数调用时机

C++中拷贝构造函数调用时机通常有三种情况:
使用一个已经创建完毕的对象来初始化一个新对象;
值传递的方式给函数参数传值;
以值方式返回局部对象;
示例:

#include <iostream>
using namespace std;

class Person
{
public:
    //无参(默认)构造函数
    Person()
    {
        cout << "无参构造函数!" << endl;
    }
    //有参构造函数
    Person(int a)
    {
        age = a;
        cout << "有参构造函数!" << endl;
    }
    //拷贝构造函数
    Person(const Person &p)
    {
        age = p.age;
        cout << "拷贝构造函数!" << endl;
    }
    //析构函数
    ~Person()
    {
        cout << "析构函数!" << endl;
    }

public:
    int age;
};

void test01()
{
    Person p1(18);
    //如果不写拷贝构造,编译器会自动添加拷贝构造,并且做浅拷贝操作
    Person p2(p1);

    cout << "p2的年龄为: " << p2.age << endl;
}

void test02()
{
    //如果用户提供有参构造,编译器不会提供默认构造,会提供拷贝构造
    Person p1;     //此时如果用户自己没有提供默认构造,会出错
    Person p2(10); //用户提供的有参
    Person p3(p2); //此时如果用户没有提供拷贝构造,编译器会提供

    //如果用户提供拷贝构造,编译器不会提供其他构造函数
    Person p4;     //此时如果用户自己没有提供默认构造,会出错
    Person p5(10); //此时如果用户自己没有提供有参,会出错
    Person p6(p5); //用户自己提供拷贝构造
}

int main()
{

    // test01();
    test02();

    system("pause");

    return 0;
}
注意:以值传递的方式返回局部对象   此局部等于栈,会自动释放;

4.2.4构造函数调用规则

#include <iostream>
using namespace std;

class Person
{
public:
    //无参(默认)构造函数
    Person()
    {
        cout << "无参构造函数!" << endl;
    }
    //有参构造函数
    Person(int age, int height)
    {

        cout << "有参构造函数!" << endl;

        m_age = age;
        m_height = new int(height);
    }
    //拷贝构造函数
    Person(const Person &p)
    {
        cout << "拷贝构造函数!" << endl;
        //如果不利用深拷贝在堆区创建新内存,会导致浅拷贝带来的重复释放堆区问题
        m_age = p.m_age;
        // m_height = p.m_height; //编译器默认的拷贝函数
        //用深拷贝解决浅拷贝重复释放堆区的问题
        m_height = new int(*p.m_height);
    }

    //析构函数
    ~Person()
    {
        cout << "析构函数!" << endl;
        if (m_height != NULL)
        {
            delete m_height;
            m_height = NULL;//防止野指针出现
        }
    }

public:
    int m_age;
    int *m_height;
};

void test01()
{
    Person p1(18, 180);

    Person p2(p1);

    cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_height << endl;

    cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_height << endl;
}

int main()
{

    test01();

    system("pause");

    return 0;
}

4.3C++对象模型和this指针


4.3.1成员变量和成员函数分开储存

在C++中,类内成员变量和成员函数分开存储的,互不干扰,其中类内只有非静态成员变量才属于类对象上;
代码示例:

#include <iostream>
using namespace std;
//成员变量 和 成员函数 分开存储的 互不干扰
class Person
{
public:
	int m_A;		//非静态成员变量  属于类的对象上
	static int m_B; //静态成员变量  不属于类对象上

	void func() //非静态成员函数 不属于类对象上
	{
	}
	static void func2(){} //非静态成员函数 不属于类对象上
};
int Person::m_B = 100;

void test01()
{
	Person p;
	//空对象占用内存空间为:0 4 1    答:1
	// C++编译器会给每一个空对象也分配一个字节的空间,为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址
	cout << "size of p:" << sizeof(p) << endl;
}
void test02()
{
	//只要不属于空对象,定义是啥类型就是几个字节
	Person p;
	cout << "size of p:" << sizeof(p) << endl;
}

int main()
{
	// test01();
	test02();
	system("pause");
	return 0;
}

注:如果一个类的内部为空,此时他占用的字节并不是0,应该为1,因为C++的编译器会自动给空对象分配一个存储字节,用以区分存储位置;
错误:在定义静态成员变量后想要输出一下m_B时发现并不能调用类;
原因:键入以后提示没有访问权限,想起一开始根据视频创建空类时,没有给其加公用属性,加上public后就可以用两种方式对其进行访问了;

4.3.2 this指针概念

通过4.3.1我们知道在C++中成员变量和成员函数是分开存储的,每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码。
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象,this指针是隐含每一个非静态成员函数内的一种指针。它并不需要定义,直接使用即可。
this指针的用途:
*当形参和成员变量同名时,可用this指针来区分
在类的非静态成员函数中返回对象本身,可使用return this

代码示例:

#include <iostream>
using namespace std;

class Person
{
private:
	/* data */
public:
	Person(int age)
	{
		// this指针,指向所调用的成员函数变量,所属的对象 防止重名
		this->age = age;
	}

	Person &Personaddage(Person &p) //如果不带引用,则变为上几节学的拷贝函数,将会创建一个新的存储空间赋值
	{
		this->age += this->age;
		return *this; //表示本体*this
	}

	int age; //演示出现重名现象,正常可以用下面形式定义成员变量
			 // int m_Age;
};
// 1、解决名称冲突
void test01()
{
	Person p1(18);
	cout << "p1的年龄是:" << p1.age << endl;
}

// 2、返回对象本身用*this
void test02()
{
	Person p1(10);
	Person p2(10);
	//链式编程思想   与cout类似,可以无限堆加
	p2.Personaddage(p1).Personaddage(p1);

	cout << "p2的年龄是:" << p2.age << endl;
}

int main()
{

	test01();
	test02();

	system("pause");
	return 0;
}

注意:返回本身时,应该注意要使用引用的方法,如果是值返回的方式,会创建一个新的对象,即转化为拷贝函数;

4.3.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include <iostream>
using namespace std;

//空指针访问成员函数

class Person
{
private:
	/* data */
public:
	void showPersonName()
	{
		cout << "this is class name" << endl;
	}
	void ShowPersonAge()
	{
		// cout << "age=" <<m_Age << endl;
		//一般默认的this->m_Age  此时指针地址若是空,对于一个空的类储存单元访问其m_Age属性  属于无中生有
		//可以加
		if (this == NULL)
		{
			return;
		}
		cout << "age=" << this->m_Age << endl;
	}
	int m_Age;
};
void test01()
{
	Person *p = NULL;
	//空指针,可以调用成员函数
	p->ShowPersonAge();
	//
	p->showPersonName();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.3.4 const修饰成员函数

常函数:

成员函数后加const后我们称为这个函数为常函数
常函数内不可以修改成员属性
成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:

声明对象前加const称该对象为常对象
常对象只能调用常函数

#include <iostream>
using namespace std;

// const修饰的成员函数、成员变量
// 常函数
class Person
{
private:
public:
	// this指针的本质是一个指针常量,指针的指向是不可以修改的  但是指针指向的内存保存的数据可以修改
	// 只有一个this->m_A时等于Person * const this 如果想让指针指向的值也不可以修改,需要声明常函数  -----> const Person * const this;
	// ()后面的const相当于const Person * const this 第一个const  在成员函数后面加上const 相当于修饰this指针的指向,让指针指向的值也不可以修改
	void showPerson() const
	{
		this->m_B = 100;
		// this->m_A = 100;
		// this = NULL; //指针不能修改指针的指向   Person * const this
	}
	void func()
	{
	}

	Person()
	{
	}

	int m_A;
	mutable int m_B; //特殊变量,在const修饰的常函数之中也可也修改值的变量  加关键字 mutable
};

void test01()
{
	Person p;
	p.showPerson();
}
//常对象
void test02()
{
	const Person p; //在对象前面加个常 变成常对象  创建常对象时 应该在类里创建构造函数否则会报错
	// p.m_A = 100;//常对象不可以修改
	p.m_B = 100;	// m_B是特殊值,在常对象下可以修改
	p.showPerson(); //常对象只能调用常函数
					// p.func();//不兼容的类型限定符
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

注意:每一个成员函数里的this指针;
this指针的本质 指针常量 指向不可以修改 但是指向的值可以修改

4.4 友元

生活中你的家有客厅(Public),有你的卧室(Private)

客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去

但是呢,你也可以允许你的好闺蜜好基友进去。

在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术

友元的目的就是让一个函数或者类 访问另一个类中私有成员

友元的关键字为 friend

友元的三种实现

  • 全局函数做友元
  • 类做友元
  • 成员函数做友元

4.4.1全局函数做友元

#include <iostream>
using namespace std;
//全局函数做友元

//建筑物
class Building
{
	// goodGay是Building的友元,因此可以访问其私有成员
	friend void goodGay(Building *building);

public:
	Building()
	{
		m_BedRoom = "卧室";
		m_SettingRoom = "客厅";
	}

private:
	string m_BedRoom;

public:
	string m_SettingRoom;
};
//创建一个全局函数
void goodGay(Building *building) //指针应该传一个地址
{

	cout << "好基友全局函数正在访问:" << building->m_SettingRoom << endl;
	cout << "好基友全局函数正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
	Building building;
	goodGay(&building);
}

void test02()
{
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

4.4.2 类做友元

#include <iostream>
using namespace std;
//类做友元
class Building; //声明一下

class goodGay
{
private:
public:
	goodGay();
	void vsisit();
	Building *building;
};

//建筑物
class Building
{
	//告诉编译器 goodGay类是Building类的友元   前者可以访问后者的私有内容
	friend class goodGay;

public:
	Building();

private:
	string m_BedRoom;

public:
	string m_SettingRoom;
};
Building::Building()
{
	this->m_BedRoom = "卧室";
	this->m_SettingRoom = "客厅";
}
goodGay::goodGay()
{
	building = new Building; //   注意-------------------类里使用指针一定要分配一个新的内存
}
void goodGay::vsisit()
{
	cout << "好基友全局函数正在访问:" << building->m_SettingRoom << endl;
	cout << "好基友全局函数正在访问:" << building->m_BedRoom << endl;
}

void test01()
{
	goodGay gg;
	gg.vsisit();
}

void test02()
{
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

注意:类内使用指针一定要在构造函数中用new分配一个新的内存

4.4.3 成员函数做友元

#include <iostream>
using namespace std;
//成员函数做友元
class Building;
class goodGay
{
private:
	/* data */
public:
	goodGay();
	void visit01(); //让visit01函数可以访问Building中的私有成员
	void visit02(); //让visit02函数不可以访问Building中的私有成员
	Building *building;
};

//指针指向所指对象的各种东西时用 -> 。
//类指向类下面的各种东西时,要用:: 。
//对象指向对象下面的各种东西时用 . 。

class Building
{
	friend void goodGay::visit01(); //不带goodGay::作用域的话代表是全局函数

private:
	string m_BedRoom;

public:
	Building();
	string m_SetRoom;
};
//类外实现成员函数  记得使用this->
Building::Building()
{
	this->m_BedRoom = "卧室";
	this->m_SetRoom = "客厅";
}
goodGay::goodGay()
{
	building = new Building; //创建一个Building类型的对象堆区,并且用building存储
}
void goodGay::visit01()
{
	cout << "visit01正在访问 " << building->m_SetRoom << endl;
	cout << "visit01正在访问 " << building->m_BedRoom << endl;
}

void goodGay::visit02()
{
	cout << "visit02正在访问 " << building->m_SetRoom << endl;
	// cout << "visit02正在访问 " << building->m_BedRoom << endl;
}

void test01()
{
	goodGay gg;
	gg.visit01();
	gg.visit02();
}

void test02()
{
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
/*
成员函数做友元易错点:
1.因为Goodgay类需要声明Building类变量,所以Building类必须Goodgay类之前声明(前向声明);
2.因为Building的定义中需要将Goodgay类的成员函数声明成友元成员函数,所以Building类必须Goodgay类之后定义;
3.因为Goodgay中的构造函数需要调用Building的构造函数,所以Goodgay类中构造函数的实现必须在Building类的定义之后;
总结:
1. Building类在Goodgay类前面声明,后面定义;
2. Goodgay类定义“类内”时不实现 构造函数 和 需要friend的成员函数;
3. Goodgay在Building类定义后,“类外”实现构造函数 和 需要friend的成员函数;
 */

成员函数做友元易错点:
1.因为Goodgay类需要声明Building类变量,所以Building类必须Goodgay类之前声明(前向声明);
2.因为Building的定义中需要将Goodgay类的成员函数声明成友元成员函数,所以Building类必须Goodgay类之后定义;
3.因为Goodgay中的构造函数需要调用Building的构造函数,所以Goodgay类中构造函数的实现必须在Building类的定义之后;

总结:

  1. Building类在Goodgay类前面声明,后面定义;
  2. Goodgay类定义“类内”时不实现 构造函数 和 需要friend的成员函数;
  3. Goodgay在Building类定义后,“类外”实现构造函数 和 需要friend的成员函数;

4.5 运算符重载


运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加的运算

#include <iostream>
using namespace std;
// 重载之后的+只是简写, 还是得按照参数列表传值
// 加号运算符重载
// 1、成员函数重载 +号   本质运算是 Person p3 = p1.operator+(p2);
// 2、全局函数重载 +号   本质运算是 person p3 = p1.operator+(p1,p2)

class Person
{
private:
	/* data */
public:
	// Person operator+(Person &p)// P为值传递,无法改变实参,&p为引用传递,可以改变实参
	// {
	// 	Person temp;
	// 	temp.m_A = this->m_A + p.m_A;
	// 	temp.m_B = this->m_B + p.m_B;
	// 	return temp;
	// }

	int m_A;
	int m_B;
};
// 2、全局函数重载 +号
Person operator+(Person &p1, Person &p2) // P为值传递,无法改变实参,&p为引用传递,可以改变实参
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//函数重载
Person operator+(Person &p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}
void test01()
{
	//这里其实写的有点麻烦 不如用括号法加上构造函数来赋值
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	Person p3;
	p3 = p1 + p2;
	Person p4;
	p4 = p1 + 25;
	cout << "p3.m_A=" << p3.m_A << endl;
	cout << "p3.m_B=" << p3.m_B << endl;
	cout << "p4.m_A=" << p4.m_A << endl;
	cout << "p4.m_B=" << p4.m_B << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

总结1:重载之后的+只是简写, 原则上还是得按照参数列表传值

4.5.2 左移运算符重载

作用:可以输出自定义数据类型
通过重载左移运算符,让编译器知道如何操作形似cout<<p该类运算符

#include <iostream>
using namespace std;

class Person
{
	friend ostream &operator<<(ostream &out, Person &p);

private:
	int m_A;
	int m_B;

public:
	Person(int a, int b);
	// p<<cout 成员函数实现不了重载左移运算符   本质上  p.operator<<(cout)   简化表示其实是 p<< cout  并不是我们想要的cout << p;
	// void operator<<(Person &p) {}
	// int m_A;
	// int m_B;
};

Person::Person(int a, int b)
{
	this->m_A = a;
	this->m_B = b;
}
//只能使用全局函数重载左移运算符
//本质 operator<<(cout,p)  简化为cout << p 注意: void operator<<(ostream &out, Person &p)仅仅实现一次  cout<<p   联想上几次课this返回对象本身实现的链式编程,一定要加引用& 否则会创建一个新的的对象
ostream &operator<<(ostream &out, Person &p) //可以把引用&cout换为&out  因为引用本身就有起别名的功能
{
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}
void test()
{
	Person p1(10, 20);
	cout << p1 << " hello world " << endl;//链式编程
}
int main()
{
	test();
	system("pause");
	return 0;
}

注意:成员函数不能够进行左移运算符重载,只有全局函数可以进行左移运算符重载
注意:cout 隶属于 ostream输出流,链式编程的代表就是cout
注意:链式编程一定要在函数返回前加上&,否则将会创建一个新的对象

4.5.3 递增运算符重载

作用:通过重载递增运算符,实现自己的整形数据前置递增和后置递增运算

#include <iostream>
using namespace std;
//重载递增运算符
//自定义整型
class MyInteger
{
	friend ostream &operator<<(ostream &cout, const MyInteger &myint);

private:
	int m_Num;

public:
	MyInteger();
	~MyInteger();
	//重载前置++运算符
	MyInteger &operator++() //返回引用是因为不想重新生成一个新的对象,链式编程思想,只对一个对象做重复操作
	{
		m_Num++;
		return *this; //*this解引用表示自身
	}
	//重载后置++运算符
	//此时不加引用,返回一个值是因为已经建立新对象temp,且如果返回一个引用则会使得temp变为局部对象变量,局部对象变量会再当前函数结束后自动释放掉
	MyInteger operator++(int) // int代表占位参数,可以用于区分前置和后置递增
	{
		// 1.先记录当时结果
		MyInteger temp = *this;
		// 2.后进行递增
		m_Num++;
		// 3.最后再返回记录的结果
		return temp;
	}
};

MyInteger::MyInteger()
{
	this->m_Num = 0;
}

MyInteger::~MyInteger()
{
}
//重载左移<<运算符
// ostream &operator<<(ostream &cout, MyInteger myint)//后置的时候不能进行链式运算
ostream &operator<<(ostream &cout, const MyInteger &myint)
{
	cout << myint.m_Num;
	return cout;
}

void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl; //直接加入操作符会提示并不存在,原因是myint是我们自定义的整型数据
	cout << myint << endl;
}
// C++中产生的临时对象是不可修改的,即默认为const的
//非const引用只能绑定到与该引用同类型的对象,但是非常量对象可以绑定到const引用上,
//因此可以去掉左移重载参数的类的引用符,即ostream &operator<<(ostream &cout, MyInt myint)
//或者加上const变为常量引用,即ostream &operator<<(ostream &cout, const MyInt &myint)
//注意该两种方法一定要记得把最上面的友元修改为相同的形式,否则会报错
void test02()
{
	MyInteger myint1;

	cout << myint1++ << endl;
	cout << myint1 << endl;
}
int main()
{

	test01();
	test02();
	system("pause");
	return 0;
}

注意:前置运算符需要引用,后置运算符需要返回值
注意:区分前置运算符重载和后置运算符重载的关键是占位符 int
注意:后置递增报错问题,原理以及两种解决方案:

C++中产生的临时对象是不可修改的,即默认为const的
非const引用只能绑定到与该引用同类型的对象,但是非常量对象可以绑定到const引用上,
因此可以去掉左移重载参数的类的引用符,即ostream &operator<<(ostream &cout, MyInt myint)
或者加上const变为常量引用,即ostream &operator<<(ostream &cout, const MyInt &myint)
注意该两种方法一定要记得把最上面的友元修改为相同的形式,否则会报错

注意:第三种方法搞定后置递增运算报错,不需要修改重载左移运算符函数,使用后置递增运算myint1++;cout<<myint1<<out;此方法仅能将运算符后置运算之后的值输出,不可以输出myint1的值
问题遗留:该节虽然实现了前置运算符和后置运算符的重载,对自定义整型参数进行了递增运算,但是基于链式编程的思想,仅完成了前置递增运算,后置递增运算并没有实现,后续如果有能力应该对此节后置递增运算符重新构建;
拓展递减运算重载

#include <iostream>
using namespace std;
//重载递增运算符
//自定义整型
class MyInteger
{
	friend ostream &operator<<(ostream &cout, const MyInteger &myint);

private:
	int m_Num;

public:
	MyInteger();
	~MyInteger();
	//重载前置++运算符
	MyInteger &operator++()
	{
		m_Num++;
		return *this; //*this解引用表示自身
	}
	//重载后置++运算符
	MyInteger operator++(int) // int代表占位参数,可以用于区分前置和后置递增
	{
		// 1.先记录当时结果
		MyInteger temp = *this;
		// 2.后进行递增
		m_Num++;
		// 3.最后再返回记录的结果
		return temp;
	}

	//重载前置--运算符
	MyInteger operator--()
	{
		m_Num--;
		return *this;
	}

	//重载后置--运算符 int 相当于占位符
	MyInteger operator--(int)
	{
		//先记录当前这个数
		MyInteger temp;
		//进行--操作
		m_Num--;
		//返回开始记录的那个数
		return temp;
	}
};

MyInteger::MyInteger()
{
	this->m_Num = 3;
}

MyInteger::~MyInteger()
{
}
//重载左移<<运算符

ostream &operator<<(ostream &cout, const MyInteger &myint)
{
	cout << myint.m_Num;
	return cout;
}

void test01()
{
	MyInteger myint;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}

void test02()
{
	MyInteger myint;

	cout << myint++ << endl;
	cout << myint << endl;
}
void test03()
{
	MyInteger myint;

	cout << myint--<<endl;
	cout << myint << endl;
}
void test04()
{
	MyInteger myint;

	cout << --myint << endl;
	cout << myint << endl;
}
int main()
{

	test01();
	test02();
	test03();
	test04();
	system("pause");
	return 0;
}

4.5.4 赋值运算符重载

c++编译器至少给一个类添加4个函数

默认构造函数(无参,函数体为空)
默认析构函数(无参,函数体为空)
默认拷贝构造函数,对属性进行值拷贝
赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include <iostream>
using namespace std;

//赋值运算符重载
class Person
{
private:
public:
	Person(int age);
	~Person();
	Person &operator=(Person &p)
	{
		//编译器提供的浅拷贝 m_Age=P.m_Age;

		//应该先判断是否有属性在堆区,如果有应该先释放干净,然后再进行深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//深拷贝
		this->m_Age = new int(*p.m_Age);
		//返回对象本身
		return *this; //链式编程思想
	}
	int *m_Age;
};

Person::Person(int age)
{
	this->m_Age = new int(age); //堆区  由程序员手动开启,由程序员手动释放  因此需要加个析构韩硕
}

Person::~Person()
{
	if (m_Age != NULL)
	{
		delete m_Age;
		m_Age = NULL;
	}
}

void test01()
{
	Person p1(18);
	Person p2(28);
	Person p3(38);
	p3 = p2 = p1; //赋值操作
	cout << "p1岁数:" << *p1.m_Age << endl;
	cout << "p2岁数:" << *p2.m_Age << endl;
	cout << "p3岁数:" << *p3.m_Age << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}


//是指针的值,存储的是地址。所以本质上拷贝过去的值是一个地址值。这也导致了,两个不同的指针,指向同一块儿内存。看上去像是改变了'值',实际上只是挪了以下指针的指向而已。但是原本类里开辟的区域,它的值可以一直存在的,该多少还是多少。因此,这根本不算值拷贝

注意:因为第二次判断时是判断p2中的地址是不是空而不是判断堆区的内存,两个指针地址都指向同一个内存,因此会出现重析构运行bug
解决方案:利用深拷贝解决浅拷贝带来的问题

4.5.5 关系运算符重载

作用:重载等于==和不等于!=两个关系运算符,用于自定义类型对象的对比操作

#include <iostream>
using namespace std;

//重载   关系运算符
class Person
{
private:
public:
	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	bool operator==(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
			return false;
	}
	bool operator!=(Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return false;
		}
		else
			return true;
	}
	string m_Name;
	int m_Age;
};

void test01()
{
	Person p1("Tom", 19);

	Person p2("Tom", 18);
	if (p1 == p2)
	{
		cout << "equivalent" << endl;
	}
	else
	{
		cout << "not equivalent!!!" << endl;
	}
	if (p1 != p2)
	{
		cout << "not equivalent" << endl;
	}
	else
	{
		cout << "equivalent!!!" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.5.6 函数调用运算符重载

函数调用运算符 () 由于重载后使用的方式非常像函数的调用,因此称为仿函数
作用:仿函数比较灵活多变 此处用于实现一个打印输出类
拓展:匿名函数 即直接使用重载后的函数调用运算符(没有声明)

#include <iostream>
using namespace std;

//重载  函数调用运算符 ()
//打印输出的一个类
class MYPrint
{
private:
	/* data */
public:
	void operator()(string test)
	{
		cout << test << endl; // test不加双引号,直接输出string test
	}
};

void test01()
{
	MYPrint myPrint;
	myPrint("hello world!"); //仿函数
}
//仿函数非常灵活,没有固定的写法
//加法类
class MYAdd
{
private:
	/* data */
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
};
void test02()
{
	MYAdd myadd;//myadd算作起名了
	int sum = myadd(100, 100);
	cout << sum << endl;
	//匿名函数对象
	cout << MYAdd()(100, 100) << endl;
	//特点当前行执行完立即被释放
}
int main()
{
	test01();
	test02();

	system("pause");
	return 0;
}

4.6 继承

继承是面向对象三大特性之一
有些类与类之间存在特殊的关系,定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性。
这个时候我们就可以考虑利用继承的技术,减少重复代码

4.6.1 继承的基本语法

继承的好处:减少重复代码
继承使用语法: class 子类 : 继承方式 父类
子类 也称为 派生类
父类 也称为 基类
子类中包含两大部分:
一部分是从父类通过不同方式继承,一部分是自己本身区别于父类增加的。
从父类继承过来的部分表现子类共性,而新增的部分则体现了子类个性。

#include <iostream>
using namespace std;
//继承实现
//公共页面
class BasePage
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}

	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java,Python,C++...(公共分类列表)" << endl;
	}
};
//继承的好处:减少重复代码
//继承使用语法: class 子类 : 继承方式 父类
//子类 也称为 派生类
//父类 也称为 基类
// Java页面
class Java : public BasePage //此处继承方式是public 公共继承方式
{
public:
	void content()
	{
		cout << "JAVA学科视频" << endl;
	}
};
// Python页面
class Python : public BasePage
{
public:
	void content()
	{
		cout << "Python学科视频" << endl;
	}
};
// C++页面
class CPP : public BasePage
{
public:
	void content()
	{
		cout << "C++学科视频" << endl;
	}
};

void test01()
{
	// Java页面
	cout << "Java下载视频页面如下: " << endl;
	Java ja;
	ja.header();
	ja.footer();
	ja.left();
	ja.content();
	cout << "--------------------" << endl;

	// Python页面
	cout << "Python下载视频页面如下: " << endl;
	Python py;
	py.header();
	py.footer();
	py.left();
	py.content();
	cout << "--------------------" << endl;

	// C++页面
	cout << "C++下载视频页面如下: " << endl;
	CPP cp;
	cp.header();
	cp.footer();
	cp.left();
	cp.content();
}

int main()
{

	test01();

	system("pause");

	return 0;
}



总结

此系列用于记录平时学习C++过程中的笔记;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会是要长脑子了吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值