C++核心编程总结

C++核心编程

1. 内存分区模型

  • 代码区
    • 存放函数体的二进制代码,由操作系统进行管理
  • 全局区
    • 存放全局变量、静态变量以及常量,常量区存放const修饰的全局变量和字符串常量
  • 栈区
    • 由编译器自动分配释放,存放函数的参数值,局部变量等
    • 注意不要返回局部变量的地址
  • 堆区
    • 由程序员分配和释放,若不释放,程序结束时由操作系统回收
1.1 程序运行前

在程序编译后,生成了可执行文件,未执行前分为两个区域

  • 代码区
    存放CPU执行的机器指令
    • 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
    • 代码区是只读的,为了防止程序意外修改了它的指令
  • 全局区
    全局变量和静态变量存在这,全局区还包括了常量区,字符串常量和其他常量也存放在此
    • 该区域的数据在程序结束后由操作系统释放
1.2 程序运行后
  • 栈区
    由编译器自动分配释放,存放函数的参数值,局部变量等,注意:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
  • 堆区
    由程序员分配释放,若不释放则程序结束时由操作系统回收,堆区数据利用new关键字进行开辟内存
1.3 new操作符

C++中利用new关键字在堆区开辟内存,堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete
语法:new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针

int* func()
{
	//在堆区创建整型数据
	int* p = new int(10);
	return p;
}

int* p = func();
cout << *p << endl;
delete p;//释放堆区数据,用关键字delete

//在堆区开辟数组
int* arr = new int[100];
for(int i=0;i<100;++i)
{
	arr[i] = i;
}
delete[] arr;//释放堆区数组(记得加[])

2. 引用

作用:给变量起别名
语法:数据类型& 别名=原名

2.1 引用的基本使用

语法:数据类型& 别名=原名

2.2 引用注意事项
  • 引用必须初始化
  • 初始化后不可改变
2.3 引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参

//交换函数
void swap_1(int a,int b)//值传递
{
   int temp = a;
   a = b;
   b = temp;
}

void swap_2(int *a, int *b)//地址传递
{
   int temp = *a;
   *a = *b;
   *b = temp;
}

void swap_3(int &a, int &b)//引用传递
{
   int temp = a;
   a = b;
   b = temp;
}
2.4 引用做函数返回值

作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值

//不要返回局部变量的引用
int& test_1()
{
	int a = 10;//局部变量存放在栈区
	return a;
}

//函数调用可以作左值
int& test_2()
{
	static int a = 10;//静态局部变量,存放在全局区
	return a;
}

int main()
{
	int& ref = test_1();
	cout << "ref=" << ref << endl;//第一次正确是因为编译器做了保留
	cout << "ref=" << ref << endl;
	int& ref2 = test_2();
	cout << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;//没有任何问题
	test_2() = 100;//相当于a=100;
	cout << "ref2=" << ref2 << endl;//&ref2=a;
	cout << "ref2=" << ref2 << endl;


	system("pause");
	return 0;
}
2.5 引用的本质
  • 在C++内部实现是一个指针常量
2.6 常量引用

作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参

void show_value(const int& val)
{
	//val = 1000;//在函数体内无法修改val的值
	cout << "val=" << val << endl;
}

3. 函数提高

3.1 函数默认参数

在C++中,函数的形参列表中的形参是可以有默认值的
语法:返回值类型 函数名(参数=默认值){}

3.2 函数占位参数

C++形参列表中可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名 (数据类型){}

int func(int a, int b=2, int c=3)
{
	return a + b + c;
}

/*int func(int a, int b = 2, int c, int d)//如果某个位置有了默认参数,则之后的所有位置都要有默认参数
{
	return a + b + c + d;
}*/

//声明和实现只能有一个有默认参数
/*int func2(int a = 2, int b = 3);

int func2(int a = 2, int b = 3)
{
	return a + b;
}*/

//占位参数
void func3(int a, int)
{
	cout << "Hello World" << endl;
}

int main()
{
	cout << func(1) << endl;//传参就可以只传a的值
	cout << func(1,3) << endl;//传了就用传的值 
	//cout << func2() << endl;
	func3(10, 10);//占位参数必须赋值
	system("pause");
	return 0;
}
3.3 函数重载
  • 作用:函数名相同,提高复用性
  • 满足条件:
    • 同一作用域下
    • 函数名称相同
    • 函数参数类型或者个数不同或者顺序不同
  • 注意:函数的返回值不可以作为函数重载的条件

4. 类和对象

C++面向对象的三大特性为:封装、继承、多态

4.1 封装
4.1.1 封装的意义
  • 将属性和行为作为一个整体,表现生活中的事物
  • 将属性和行为加以权限控制

在设计类时,属性和行为写在一起,表现事物
语法:class 类名{ 访问权限:属性/行为 }
类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有:

  • public 公共权限,类内类外都可以访问
  • protected 保护权限,类内可以访问,类外不可访问(子类可以访问父类中的内容)
  • private 私有权限,类内可以访问,类外不可访问(子类不可访问父类中的内容)
4.1.2 struct和class的区别
  • struct默认权限为公有
  • class默认权限为私有
4.1.3 成员属性设置为私有
  • 将所有成员属性设置为私有,可以自己控制读写权限
  • 对于写权限,我们可以检测数据的有效性
4.2 对象的初始化和清理
4.2.1 构造函数和析构函数

对象的初始化和清理是两个非常重要的安全问题,C++提供了构造函数和析构函数解决上述问题,这两个函数是编译器自动调用的,若我们不提供构造和析构,编译器提供的构造和析构是空实现

  • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用
  • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作

构造函数:
语法:类名(){}

  1. 没有返回值也不写void
  2. 函数名称与类名相同
  3. 可以有参数,因此可以发生重载
  4. 程序在调用对象时自动调用构造,无需手动调用且只调用一次

析构函数:
语法:~类名(){}

  1. 没有返回值也不写void
  2. 函数名称与类名相同,在名称前加~
  3. 不可以有参数,不可以发生重载
  4. 程序在对象销毁前会自动调用析构,无需手动调用且只调用一次
4.2.2 构造函数的分类和调用
  • 按参数分类:有参构造和无参构造
  • 按类型分类:普通构造和拷贝构造

三种调用方式为:

  • 括号法
  • 显示法
  • 隐式转换法
#include<iostream>
#include<string>
using namespace std;

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

	int age;
};

void test1()
{
	person p1;//创建类的时候自动调用构造函数且只调用一次
	//在栈上的数据,test1()执行完之后自动释放这个对象
}

void test2()
{
	//括号法调用构造函数
	person p1;//默认构造函数的调用(不加())
	person p2(10);//有参构造函数的调用
	person p3(p2);//拷贝构造函数的调用
	cout << "p2的年龄为:" << p2.age << endl;
	cout << "p3的年龄为:" << p3.age << endl;//p3是拷贝的p2
	
	//显示法调用构造函数
	person p4;
	person p5 = person(10);
	person p6 = person(p5);

	person(20);//匿名对象,当前行执行结束马上回收
	cout << "asa" << endl;
	//person(p6);//不要用拷贝构造函数初始化匿名对象

	//隐式转换法
	person p7 = 10;//等价于p5那种写法
	person p9 = p2;//等价于p3那种写法
}
int main()
{
	//test1();
	test2();
	//person p2;//在main函数执行完之后才会执行析构
	system("pause");
	return 0;
}
4.2.3拷贝构造函数调用时机

C++中拷贝构造函数调用时机有:

  • 使用一个已经创建完毕的对象来初始化一个新对象
  • 值传递的方式给函数参数传值
  • 以值方式返回局部对象
#include<iostream>
#include<string>
using namespace std;

class person
{
public:
	person()
	{
		cout << "person的无参构造函数调用" << endl;
	}
	person(int s_age)
	{
		age = s_age;
		cout << "person的有参构造函数调用" << endl;
	}
	person(const person& p)
	{
		age = p.age;
		cout << "person的拷贝构造函数调用" << endl;
	}
	~person()
	{
		cout << "person的析构函数调用" << endl;
	}
	int age;
};

void test1()
{
	person p1(20);
	person p2(p1);
	cout << "p2的年龄为:" << p2.age << endl;
}

void do_work(person p)
{

}

void test2()
{
	person p;
	do_work(p);//值传递的方式给函数参数传值
}

person do_work2()
{
	person p1;
	cout << (int)&p1 << endl;
	return p1;
}

void test3()
{
	person p = do_work2();//值方式返回局部对象
	cout << (int)&p << endl;//p和p1地址不同,p是拷贝的一份p1的副本
}
int main()
{
	//test1();
	//test2();
	test3();
	system("pause");
	return 0;
}
4.2.4 构造函数调用规则

默认情况下,C++编译器至少给一个类添加3个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则:

  • 如果用户定义有参构造函数,C++不再提供默认无参构造,但会提供默认拷贝构造
  • 如果用户定义拷贝构造,C++不再提供其他构造函数
4.2.5 深拷贝与浅拷贝
  • 深拷贝:在堆区重新申请空间,进行拷贝操作
  • 浅拷贝:简单的赋值拷贝操作
  • 如果属性有在堆区开辟的,一定要自己实现拷贝构造函数,防止浅拷贝带来的问题
#include<iostream>
#include<string>
using namespace std;

class person
{
public:
	person()
	{
		cout << "person的无参构造函数调用" << endl;
	}
	person(int s_age,int s_height)
	{
		age = s_age;
		height = new int(s_height);//将height的数据开辟到堆区(括号里面是赋值)
		cout << "person的有参构造函数调用" << endl;
	}
	//自己实现拷贝构造函数,解决浅拷贝带来的问题
	person(const person& p)
	{
		age = p.age;
		//height = p.height;//编译器浅拷贝实现的内容
		//浅拷贝就是将p1的数据逐字节的拷贝到p2中
		height = new int(*p.height);//深拷贝操作
		cout << "person的拷贝构造函数调用" << endl;
	}
	~person()
	{
		//析构代码,将堆区开辟的数据进行释放
		if (height != NULL)
		{
			delete height;
			height = NULL;
		}
		cout << "person的析构函数调用" << endl;
	}
	int age;
	int* height;
};

void test1()
{
	person p1(18, 160);
	cout << "p1的年龄为:" << p1.age << "身高为:" << *p1.height << endl;
	person p2(p1);
	cout << "p2的年龄为:" << p2.age << "身高为:" << *p2.height << endl;//即使自己不写拷贝构造函数,编译器也会进行浅拷贝操作
	//浅拷贝带来的问题就是堆区内存重复释放,p2释放执行析构函数时释放了一次,p1执行析构函数时也释放了一次
	//深拷贝的时候p2执行拷贝构造函数,height是重新在堆区开辟的一块空间,与p1的堆区空间是不同的
}
int main()
{
	test1();
	system("pause");
	return 0;
}
4.2.6 初始化列表

作用:用来初始化属性
语法:构造函数(): 属性1(值1),属性2(值2),… {}

class person
{
public:
	//传统初始化操作
	/*person(int a, int b, int c)
	{
		A = a;
		B = b;
		C = c;
	}*/
	
	//初始化列表
	person(int a,int b,int c) :A(a), B(b), C(c)
	{

	}
	int A, B, C;
};
4.2.7 类对象作为类成员
  • 一个类中的成员可以是另一个类的对象,称为对象成员
4.2.8静态成员

静态成员变量:

  • 所有对象共享同一份数据
  • 在编译阶段分配内存
  • 类内声明,类外初始化

静态成员函数:

  • 所有对象共享同一个函数
  • 静态成员函数只能访问静态成员变量
  • 在成员变量和成员函数前加上static,称为静态成员
class person
{
public:
	//静态成员函数
	static void func()
	{
		A = 100;//静态成员函数可以访问静态成员变量
		//B = 20;//不可访问非静态成员变量
		cout << "static func的调用" << endl;
	}
	static int A;//静态成员变量
	int B;//非静态成员变量
};

int person::A = 0;//静态成员变量类外初始化
void test1()
{
	person p;
	p.func();//通过对象访问
	person::func();//通过类名访问(在person作用域下)
}
4.3 C++对象模型和this指针
4.3.1 成员变量和成员函数分开存储
  • 在C++中,类内的成员变量和成员函数分开存储,只有非静态成员变量才属于类的对象
4.3.2 this指针概念

每一个非静态成员函数只会诞生一份函数实例,多个同类型的对象会共用一块代码
C++提供特殊的对象指针,即this指针,来区分是哪个对象调用非静态成员函数
this指针指向被调用成员函数所属的对象
用途:

  • 当形参和成员变量名同名时,可用this指针区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include<iostream>
using namespace std;
	
class person
{
public:
	person(int age)
	{
		//this指针指向被调用成员函数所属的对象
		this->age = age;//this指针解决形参和成员变量名冲突
	}
	person& person_add_age(person& p)//用引用的方式返回,就一直是p2,若返回值,则每次调用完返回的都是一个新的对象
	{
		this->age += p.age;
		return *this;//返回调用该非静态成员函数的对象本身
	}
	int age;
};
void test1()
{
	person p1(18);
	person p2(10);
	p2.person_add_age(p1).person_add_age(p1);//可以连续调用了(链式编程思想)
	cout << "p1年龄为" << p1.age << endl;
	cout << "p2年龄为" << p2.age << endl;
}
int main()
{
	test1();
	system("pause");
	return 0;
}
4.3.3 空指针访问成员函数
  • C++中空指针是可以调用成员函数的,但是要注意有没有用到this指针,若用了this指针,要加以判断保证代码的健壮性
4.3.4 const修饰成员函数

常函数:

  • 成员函数后加const称之为常函数
  • 常函数内不可修改成员属性
  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数
#include<iostream>
using namespace std;

class person
{
public:
	//this指针的本质是指针常量,指针的指向不可修改
	//person *const this
	void show_person() const//这样加const,this指针指向的值都不可修改了
	{
		//m_A = 100;//等价于this->m_A=100,隐藏了this指针
		//this = NULL;//this指针指向不可修改
		m_B = 100;
	}
	void func()
	{

	}
	int m_A;
	mutable int m_B;//特殊变量,在常函数中也可修改
};

void test1()
{
	person p;
	//p.show_person();
}

void test2()
{
	const person p;//在对象前面加const,变为常对象
	//p.m_A = 100;
	p.m_B = 120;//m_B在常对象中也可以修改
	p.show_person();//常对象只能调用常函数
	//p.func();
}
int main()
{
	test1();
	system("pause");
	return 0;
}

4.4 友元

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

  • 全局函数做友元
  • 类做友元
  • 成员函数做友元
4.5 运算符重载

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

4.5.1 加号运算符重载
  • 实现两个自定义数据类型的相加
class person
{
public:
	/*person operator+(person& 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;
};

//成员函数重载本质调用
//person p3=p1.operator+(p2)

person operator+(person& p1, person& p2)//全局函数实现加号运算符重载
{
	person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}

//全局函数本质调用
//person p3=operator+(p1,p2)
//运算符重载也可以发生函数重载

void test1()
{
	person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	person p2;
	p2.m_A = 10;
	p2.m_B = 10;
	person p3 = p1 + p2;
	cout << "p3.m_A=" << p3.m_A << endl;
	cout << "p3.m_B=" << p3.m_B << endl;
}
4.5.2 左移运算符重载
  • 可以输出自定义数据类型
ostream& operator<<(ostream &cout, person &p)//全局函数重载左移运算符
{
	cout << "m_A=" << p.m_A << " m_B=" << p.m_B;
	return cout;
}

void test2()
{
	person p;
	p.m_A = 10;
	p.m_B = 10;
	cout << p << endl;
}
4.5.3 递增运算符重载
  • 通过重载递增运算符,实现自己的整型数据
#include<iostream>
using namespace std;


class my_integer
{
	 friend ostream& operator<<(ostream& cout, my_integer myint);
public:
	my_integer()
	{
		m_num = 0;
	}
	//重载前置++运算符,返回引用是为了一直对一个数据进行递增操作
	my_integer& operator++()
	{
		m_num++;//先递增
		return *this;//再将自身返回
	}
	//重载后置++运算符,int代表占位参数,可以用于区分前置和后置递增
	//只能返回值,因为temp是一个局部变量,调用完函数后就释放了
	my_integer operator++(int)
	{
		my_integer temp = *this;//先记录当时的结果
		m_num++;//后递增
		return temp;
	}
private:
	int m_num;
};

//重载左移运算符
ostream& operator<<(ostream& cout, my_integer myint)
{
	cout << myint.m_num;
	return cout;
}
void test1()
{
	my_integer myint;
	cout << ++(++myint) << endl;
	cout << myint << endl;
}

void test2()
{
	my_integer myint;
	cout << myint++ << endl;
	cout << myint << endl;
}
int main()
{
	//test1();
	test2();
	system("pause");
	return 0;
}
4.5.4 赋值运算符重载

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

  • 默认构造函数(无参,函数体为空)
  • 默认析构函数(无参,函数体为空)
  • 默认拷贝构造函数,对属性进行值拷贝
  • 赋值运算符operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include<iostream>
using namespace std;

class person
{
public:
	person(int age)
	{
		m_Age = new int(age);
	}
	int* m_Age;
	~person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	//重载赋值运算符
	person& operator=(person& p)
	{
		//编译器提供的是浅拷贝,即m_Age=p.m_Age
		//应该先判断是否有属性在堆区,若有先释放干净,然后再深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		m_Age = new int(*p.m_Age);
		return *this;
	}
};

void test1()
{
	person p1(18);
	person p2(20);
	person p3(30);
	p3 = p2 = p1;//连续调用operator=函数
	cout << "p1年龄为:" << *p1.m_Age << endl;
	cout << "p2年龄为:" << *p2.m_Age << endl; 
	cout << "p3年龄为:" << *p3.m_Age << endl;
}
//堆区内存重复释放
int main()
{
	test1();
	system("pause");
	return 0;
}
4.5.5 关系运算符重载
  • 重载关系运算符,可以让两个自定义对象进行对比操作
class person
{
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;
	}
	string m_name;
	int m_age;
};
4.5.6 重载函数调用运算符
  • 函数调用运算符 () 也可以重载
  • 由于重载后使用方式非常像函数的调用,所以也称仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
#include<string>
using namespace std;

class my_print
{
public:
	//重载函数调用运算符
	void operator()(string test)
	{
		cout << test << endl;
	}
};

void my_print2(string test)
{
	cout << test << endl;
}
void test1()
{
	my_print myprint;
	myprint("caonima");//非常像一个函数调用,所以称为仿函数
	my_print2("caonima");
}

class my_add
{
public:
	//重载函数调用运算符
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};

void test2()
{
	my_add myadd;
	int ret= myadd(100, 145);//其实是myadd对象调用()运算符
	cout << "ret=" << ret << endl;
}
int main()
{
	test1();
	test2();
	system("pause");
	return 0;
}
4.6 继承
4.6.1 继承的基本语法
  • 继承的好处:减少重复代码
  • 语法:class 子类 : 继承方式 父类
  • 子类也称派生类,父类也称基类
4.6.2 继承方式
class A
{
public:
	int a;
protected:
	int b;
private:
	int c;
}
//公共继承:
class B : public A            
{
public:
	int a;
protected:
	int b;
//不可访问:
	int c;
}

//保护继承:
class B : protected A            
{
protected:
	int a;
	int b;
//不可访问:
	int c;
}

//私有继承:
class B : private A            
{
private:
	int a;
	int b;
//不可访问:
	int c;
}
4.6.3 继承继承中的对象模型
#include<iostream>
#include<string>
using namespace std;

class base
{
public:
	int m_A;
protected:
	int m_B;
private:
	int m_C;
};

class son :public base
{
public:
	int m_D;
};

void test1()
{	
	//父类中所有非静态成员属性都会被子类继承下去
	//父类中私有成员属性是被编译器隐藏了,所以访问不到,但确实继承下去了
	cout << "size of son=" << sizeof(son) << endl;//16B
}

int main()
{
	test1();
	system("pause");
	return 0;
}
4.6.4 继承中构造和析构的顺序

先构造父类再构造子类,析构的顺序和构造的顺序相反

4.6.5 继承中同名成员处理方式
  • 访问子类同名成员,直接访问
  • 访问父类同名成员,需要加作用域
4.6.6 继承中同名静态成员处理方式
  • 访问子类同名成员,直接访问即可
  • 访问父类同名成员,需要加作用域
#include<iostream>
#include<string>
using namespace std;

//静态成员变量,类内声明,类外初始化
class base
{
public:
	static int m_A;
	static void func()
	{
		cout << "base-static void func()" << endl;
	}
};
int base::m_A = 100;

class son :public base
{
public:
	static int m_A;
	static void func()
	{
		cout << "son-static void func()" << endl;
	}
};
int son::m_A = 200;

//同名静态成员变量
void test1()
{
	//通过对象访问
	son s;
	cout << "son m_A=" << s.m_A << endl;
	cout << "base m_A=" << s.base::m_A << endl;
	//通过类名访问
	cout << "son m_A=" << son::m_A << endl;
	//第一个::表示通过类名的方式访问,第二个::表示访问父类作用域下
	cout << "base m_A=" << son::base::m_A << endl;
}


//同名静态成员函数
void test2()
{
	son s;
	//通过对象访问
	s.func();
	s.base::func();
	//通过类名访问
	son::func();
	son::base::func();
}
int main()
{
	//test1();
	test2();
	system("pause");
	return 0;
}
4.7 多态
4.7.1 多态的基本概念

多态分为两类:

  • 静态多态:函数重载和运算符重载属于静态多态,复用函数名
  • 动态多态:派生类和虚函数实现运行时多态
    静态多态和动态多态的区别:
  • 静态多态的函数地址早绑定:编译阶段确定函数地址
  • 动态多态的函数地址晚绑定:运行阶段确定函数地址
#include<iostream>
#include<string>
using namespace std;

//动态多态满足条件
//1、有继承关系
//2、子类重写父类的虚函数
//动态多态使用
//父类指针或者引用指向子类对象

//动物类
class Animal
{
public:
	//虚函数,地址晚绑定,执行的时候才确定函数地址
	virtual void speak()
	{
		cout << "动物在说话" << endl;
	}
};

//猫类
class Cat :public Animal
{
public:
	void speak()
	{
		cout << "小猫在说话" << endl;
	}
};

//狗类
class Dog :public Animal
{
public:
	void speak()
	{
		cout << "小狗在说话" << endl;
	}
};

//地址早绑定,在编译阶段就确定了函数地址
void do_speak(Animal &animal)
{
	animal.speak();//加了virtual后,speak函数有了多种形态,在运行时才确定找哪个对象
}

void test1()
{
	Cat cat;
	do_speak(cat);//父类的引用指向子类的对象(不加virtual就是动物在说话)
	Dog dog;
	do_speak(dog);
}

int main()
{
	test1();
	system("pause");
	return 0;
}
4.7.2 纯虚函数和抽象类

在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容,因此可以将虚函数改为纯虚函数
语法:virtual 返回值类型 函数名 (参数列表)=0;
当类中有了纯虚函数,这个类也称为抽象类
抽象类特点:

  • 无法实例化对象
  • 子类必须重写抽象类中的纯虚函数,否则也属于抽象类
class base
{
public:
	//纯虚函数
	//只要有一个虚函数,该类就称为抽象类
	virtual void func() = 0;
};

class son :public base
{
public:
	//子类必须重写父类的纯虚函数,否则无法也创建对象
	virtual void func()
	{
		cout << "老子重写了" << endl;
	}
};
void test1()
{
	son s;
	s.func();
	base* b = new son;
	b->func();
	delete b;
}
4.7.3 虚析构和纯虚析构

多态使用时,若子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
解决方法:将父类的析构函数改为虚析构纯虚析构
虚析构和纯虚析构共性:

  • 可以解决父类指针释放子类对象问题
  • 都要有具体的函数实现
    虚析构和纯虚析构区别:
  • 若为纯虚析构,该类为抽象类,无法实例化对象
    虚析构语法:virtual ~类名() {}
    纯虚析构语法:virtual ~类名()=0;
#include<iostream>
#include<string>
using namespace std;

//动物类
class Animal
{
public:
	Animal()
	{
		cout << "Animal构造函数调用" << endl;
	}
	//利用虚析构可以解决父类指针析构的时候不会执行子类析构的问题
	/*virtual ~Animal()
	{
		cout << "Animal虚析构函数调用" << endl;
	}*/
	//当然纯虚析构函数也可以,不过要在类外实现
	virtual ~Animal() = 0;
	//纯虚函数
	virtual void speak() = 0;
};
Animal::~Animal()
{
	cout << "Animal纯虚析构函数调用" << endl;
}
//猫类
class Cat :public Animal
{
public:
	Cat(string name)
	{
		cout << "Cat构造函数调用" << endl;
		m_name = new string (name);
	}
	void speak()
	{
		cout << *m_name<<"小猫在说话" << endl;
	}
	~Cat()
	{
		if (m_name != NULL)
		{
			cout << "Cat析构函数调用" << endl;
			delete m_name;
			m_name = NULL;
		}

	}
	string *m_name;
};

void test1()
{
	Animal* animal = new Cat("汤姆");
	animal->speak();
	//父类指针在析构的时候不会调用子类中析构函数,导致子类中的堆区数据无法释放,从而内存泄漏
	delete animal;
}

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

5. 文件操作

程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化
C++中对文件进行操作需要包含头文件fstream
文件类型分为:

  • 文本文件:文件以文本的ASCII码形式存储在计算机中
  • 二进制文件:文件以文本的二进制形式存储在计算机中
    操作文件的三大类:
  • ofstream:写操作
  • ifstream:读操作
  • fstream:读写操作
5.1 文本文件
5.1.1 写文件
  1. 包含头文件:#include<fstream>
  2. 创建流对象:ofstream ofs;
  3. 打开文件:ofs.open("文件路径",打开方式)
  4. 写数据:ofs<<"写入的数据";
  5. 关闭文件:ofs.close();
5.1.2 读文件
  1. 包含头文件:#include<fstream>
  2. 创建流对象:ifstream ifs;
  3. 打开文件并判断文件是否打开成功:ifs.open("文件路径",打开方式);
  4. 读数据:四种方式读取
  5. 关闭文件:ifs.close();
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void test1()
{
	ifstream ifs;//创建流对象
	ifs.open("test.txt", ios::in);//打开文件并判断是否打开成功
	if (!ifs.is_open())//is_open()返回值是bool类型,若打开成功则返回true
	{
		cout << "文件打开失败" << endl;
		return;
	}
	//读数据
	//第一种
	/*char buf1[1024] = { 0 };//将读的数据存放到字符数组中
	while (ifs >> buf1)//按行读取数据,读不到了则返回假
	{
		cout << buf1 << endl;
	}*/

	//第二种
	/*char buf2[1024] = { 0 };
	while (ifs.getline(buf2, sizeof(buf2)))//使用ifs流对象的成员函数getline按行将数据读到buf2中
	{
		cout << buf2 << endl;
	}*/

	//第三种
	string buf3;
	while (getline(ifs, buf3))//使用全局函数getline将数据读到字符串buf3中
	{
		cout << buf3 << endl;
	}

	//第四种(不推荐)
	//使用ifs成员函数get将文件按字符读入字符c中
	/*char c;
	while ((c = ifs.get()) != EOF)//EOF:end of file
	{
		cout << c;
	}*/
	ifs.close();
}

int main()
{
	test1();
	system("pause");
	return 0;
}
5.2 二进制文件
5.2.1 写文件
  • 二进制方式写文件主要利用流对象调用成员函数write
  • 函数原型:ostream& write(const char *buffer,int len);
  • 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数
5.2.2 读文件
  • 二进制方式读文件主要利用流对象调用成员函数read
  • 函数原型:istream& read(const char *buffer,int len);
  • 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数
5.3 文件打开方式
  • ios::in 为读文件而打开文件
  • ios::out 为写文件而打开文件
  • ios::ate 初始位置:文件尾
  • ios::app 追加方式写文件
  • ios::trunc 如果文件存在先删除,再创建
  • ios::binary 二进制方式

注意:文件打开方式可以配合使用
例如:ios::binary | ios::out,用二进制方式写文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值