内存、引用、类和对象、文件

一、内存的分区模型

代码区:二进制代码

全局区:全局变量、静态变量、常数

栈区:编译器自动分配、参数值、局部变量

堆区:程序员分配释放

1. 程序执行前

代码区:共享、只读

全局区:

不在全局区中:局部变量、const修饰的局部变量

全局区中:全局变量、静态变量、字符串常量、const修饰的全局变量

#include<iostream>
using namespace std;
//全局变量
int g_a = 10;
int g_b = 10;

const int c_g_a = 10;
int main()
{
	//全局区
	//全局变量、静态变量、常量
	//创建普通局部变量
	int a = 10;
	int b = 20;
	cout << "局部变量a的地址为:" << (int)&a << endl;
	cout << "局部变量b的地址为:" << (int)&b << endl;

	cout << "全局变量变量g_a的地址为:" << (int)&g_a << endl;
	cout << "全局变量g_b的地址为:" << (int)&g_b << endl;
	//静态变量,在普通变量前加static
	static int s_a = 10;
	static int s_b = 10;
	cout << "静态变量s_a的地址为:" << (int)&s_a << endl;
	cout << "静态变量s_b的地址为:" << (int)&s_b << endl;
	//常量
	//字符串常量
	cout << "字符串常量的地址为:" << (int)&"hello world" << endl;
	//const修饰的变量
	//const修饰的全局变量、const修饰的局部变量
	cout << "全局常量的地址为:" << (int)&c_g_a << endl;
	const int c_l_a = 10;
	cout << "局部常量的地址为:" << (int)&c_l_a << endl;
	system("pause");
	return 0;
}

2. 程序运行后

栈区: 注意事项:数据由编译器管理开辟和释放, 不要返回局部变量的地址

#include<iostream>
using namespace std;
int* func()
{
	int a = 10;//栈区数据执行完自动释放
	return &a;
}
int main()
{
	//接收返回值
	int *p = func();
	cout << *p << endl;//第一次可打印正确的,编译器做了保留
	cout << *p << endl;//第二次不保留了
	system("pause");
	return 0;
}

堆区:程序员分配释放,用new在堆区开辟空间

#include<iostream>
using namespace std;
int* func()
{
	//new
	//指针在栈,指针指向的在堆
	int *p=new int(10);
	return p;
}
int main()
{
	int *p = func();
	cout << *p << endl;
	system("pause");
	return 0;
}

3.new 操作符

#include<iostream>
using namespace std;
//new的基本语法
int * func()
{
	//在堆区创建整形数据
	//new返回的是该数据类型的指针
	int *p = new int(10);
	return p;
}
void test01()
{
	int *p = func();
	cout << *p << endl;
	//如果想释放堆区的数据,利用delete
	delete p;
	cout << *p << endl;//内存已释放
}
//在堆区利用new开辟数组
void test02()
{
	int *arr=new int[10];//有10个元素
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i + 100;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
	}
	//释放堆区数组,加[]
	delete[] arr;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

二、引用

1.引用的基本作用

给变量起别名

数据类型 &别名=原名

#include<iostream>
using namespace std;

int main()
{
	int a = 10;
	int &b = a;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	b = 100;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

2.引用注意事项

引用必须初始化;int &b   错误

初始化后,不可改变  int &b=a; int &b = c;   错误

3. 引用做函数参数

可以简化指针修改实参

#include<iostream>
using namespace std;
//交换函数
//值传递
void myswap01(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	/*
	cout << "swap01a=" << a << endl;
	cout << "swap01b=" << b << endl;
	*/
}
//地址传递
void myswap02(int *a, int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
	
}
//引用传递
void myswap03(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;

}
int main()
{
	int a = 10;
	int b = 20;
	//myswap01(a, b);//值传递,形参不会修饰实参
	//myswap02(&a, &b);//地址传递,形参会修饰实参
	myswap03(a, b);引用传递,形参会修饰实参
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

4.引用做函数返回值

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

#include<iostream>
using namespace std;
//引用做函数的返回值
//不要返回局部变量的引用
int& test01()
{
	int a = 10;//局部变量,在栈区,用完释放
	return a;
}
//函数调用可作为左值
int& test02()
{
	static int a = 10;//在全局区
	return a;
}
int main()
{
	/*
	int &ret = test01();
	cout << "ret=" << ret << endl;//第一次正确,做了保留
	cout << "ret=" << ret << endl;//第二次错误
	*/
	int &ret2 = test02();
	cout << "ret2=" << ret2 << endl;
	cout << "ret2=" << ret2 << endl;
	test02()=1000;//如果函数的返回值是引用,这个函数调用可作为左值
	cout << "ret2=" << ret2 << endl;
	cout << "ret2=" << ret2 << endl;

	system("pause");
	return 0;
}

5. 引用的本质

是指针常量  指针的指向不变,但指向的内容变化

#include<iostream>
using namespace std;
void func(int &ref)
{
	ref = 100;
}
int main()
{
	int a = 10;
	int &ref = a;
	ref = 2000;
	cout << "a=" <<a << endl;
	cout << "ref=" << ref << endl;
	system("pause");
	return 0;
}

6.常量引用

用来修饰形参,防止误操作

#include<iostream>
using namespace std;
//打印数据函数
void showvalue(const int &val)
{
	//val = 1000;//有const 不可更改值
	cout << "val=" << val<<endl;
}
int main()
{
	int a = 100;
	showvalue(a);
	cout << "a=" << a << endl;;
	system("pause");
	return 0;
}

三、函数高级

1.函数默认参数

形参可以有默认值

返回值类型 函数名(参数=默认值){}

#include<iostream>
using namespace std;
int func(int a, int b=20, int c=30)//如果某个位置有了默认参数,从这个位置开始,从左到右都必须有
{
	return a + b + c;
}
int func2(int a = 10, int b = 20);//声明和函数只能一个有默认值
int func2(int a, int b)
{
	return a + b;
}
int main()
{
	cout << func(10,30) << endl;//如果传了用传的,没有传用默认的
	system("pause");
	return 0;
}

2.函数占位参数

用来占位,函数调用时必须填补

返回值类型 函数名(数据类型){}

#include<iostream>
using namespace std;
//占位参数还可以有默认值
void func(int a,int =10)
{
	cout << "This is a func" << endl;
}
int main()
{
	func(10);
	system("pause");
	return 0;
}

3.函数重载

(1)函数名可以相同,提高复用性

满足条件:同一作用域下;函数名称相同;函数参数个数、类型、顺序不同

#include<iostream>
using namespace std;
//都在全局域、函数名相同、参数不同
void func()
{
	cout << "func的调用" << endl;
}
void func(int a)
{
	cout << "func(int a)的调用!" << endl;
}
void func(double a)
{
	cout << "func(double a)的调用!" << endl;
}
void func(double a,int b)
{
	cout << "func(double a,int b)的调用!" << endl;
}
void func(int a, double b)
{
	cout << "func(int a, double b)的调用!" << endl;
}
//注意:函数的返回值不可以作为函数重载的条件
int func(int a, double b)  //错误
{
	cout << "func(int a, double b)的调用!" << endl;
}
int main()
{
	func();
	func(10);
	func(3.14);
	func(3.14, 10);
	func(10, 3.14);
	system("pause");
	return 0;
}

(2)注意事项

引用作为重载条件

函数重载遇到函数默认参数

#include<iostream>
using namespace std;
//1.引用作为重载条件
void func(int &a)
{
	cout << "func(int &a)的调用" << endl;
}
void func(const int &a)  //int &a=10不合法
{
	cout << "func(const int &a)的调用" << endl;
}
//2.函数重载碰到函数默认参数
void func2(int a,int b=10)  
{
	cout << "func2(int a,int b)的调用" << endl;
}
void func2(int a)
{
	cout << "func2(int a)的调用" << endl;
}
int main()
{
	int a = 10;
	func(a);//用的是第一个,因为a是一个变量
	func(10);//调用下一个
	//func2(10);错误,两个调用相同
	system("pause");
	return 0;
}

四、类和对象

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

万物皆为对象 ,对象有其属性和行为

类:具有相同性质的对象

1. 封装

1.1 封装的意义

(1)将属性和行为作为整体,表现事物

#include<iostream>
using namespace std;
//设计圆类,求周长
const double pi = 3.14;
//类后是名称
class circle
{
	//访问权限
public://公共权限
	//属性:半径
	int m_r;
	//行为:求周长
	double calcularZC()
	{
		return 2 * pi*m_r;
	}
};
int main()
{
	//通过圆类,创建具体的圆(对象)
	//实例化(通过一个类,创建一个对象)
	circle c1;
	//属性赋值
	c1.m_r = 10;
	cout << "圆的周长为:" << c1.calcularZC() << endl;
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<string>
//设计一个学生类,属性有姓名和学号
//可给姓名、学号赋值,显示姓名、学号
//设计学生类
class student
{
public:
	//类中的属性和行为  称为成员
	//属性 成员属性 成员变量
	//行为  成员函数 成员方法
	string m_name;
	int m_id;
	void showstudent()
	{
		cout << "姓名:" << m_name << "   学号:" << m_id << endl;
	}
	//给姓名赋值
	void setname(string name)
	{
		m_name = name;
	}
	void setid(int id)
	{
		m_id = id;
	}
};
int main()
{
	student s1;
	//赋值
	//s1.m_name = "张三";
	//s1.m_id = 1;
	s1.setname("张三");
	s1.setid(1);
	//显示
	s1.showstudent();
	student s2;
	//赋值
	s2.m_name = "李四";
	s2.m_id = 2;
	//显示
	s2.showstudent();
	system("pause");
	return 0;
}

(2)访问权限 public protected private

#include<iostream>
using namespace std;
#include<string>
//public 类内类外都可访问
//protected 类内可访问 类外不可以 在继承中,儿子可以访问父亲的保护内容
class person
{
public:
	string m_name;
protected:
	string m_car;
private:
	int m_password;
public:
	void func()
	{
		m_name = "张三";
		m_car = "拖拉机";
		m_password = 123456;

	}
};
int main()
{
	person p1;
	p1.m_name = "李四";
	system("pause");
	return 0;
}

1.2 struct和class的区别

struct 默认为共有

class默认为私有

#include<iostream>
using namespace std;
#include<string>
class C1
{
	int m_a;
};
struct C2
{
	int m_a;
};
int main()
{
	C1 c1;
	c1.m_a = 100;//不可访问
	C2 c2;
	c2.m_a = 100;//正确
	system("pause");
	return 0;
}

1.3. 成员属性设置为私有

优点:将所有成员属性设置为私有,可自己控制读写权限

          对于写权限,可检测数据的有效性

#include<iostream>
using namespace std;
#include<string>
class person
{
public:
	//写姓名
	void setname(string name)
	{
		m_name = name;
	}
	//读姓名
	string getname()
	{
		return m_name;
	}
	int getage()
	{
		m_age = 0;//初始化
		return m_age;
	}
	void setlover(string lover)
	{
		m_lover = lover;
	}
private:
	string m_name; //可读可写
	int m_age;//只读
	string m_lover;//只写
};

int main()
{
	person p;
	p.setname("张三");
	cout<<"姓名:"<<p.getname()<<endl; 
	cout << "年龄:" << p.getage() << endl;
	p.setlover("小白");
	system("pause");
	return 0;
}

1.4.案例

#include<iostream>
using namespace std;
#include<string>
class cube
{
public:
	//设置长
	void setL(int l)
	{
		m_L = l;
	}
	//获取长
	int getL()
	{
		return m_L;
	}
	//设置宽
	void setW(int w)
	{
		m_W = w;
	}
	//获取宽
	int getW()
	{
		return m_W;
	}
	//设置高
	void setH(int h)
	{
		m_H = h;
	}
	//获取高
	int getH()
	{
		return m_H;
	}
	//计算面积
	int calculateS()
	{
		return 2 * m_L*m_W + 2 * m_L*m_H + 2 * m_H*m_W;
	}
	//计算体积
	int calculateV()
	{
		return m_L*m_W*m_H;
	}
	//成员函数判断
	bool isseambyclass(cube &c)
	{
		if (m_L == c.getL() && m_W == c.getW() && m_H == c.getH())
		{
			return true;
		}
		return false;
	}
private:
	int m_L;
	int m_W;
	int m_H;
	
};
//全局判断立方体相等
bool Isseam(cube &c1,cube &c2)
{
	if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH())
	{
		return true;
	}
	return false;
}
int main()
{
	cube c1;
	c1.setL(10);
	c1.setW(10);
	c1.setH(10);
	cout << "c1的面积为:" << c1.calculateS() << endl;
	cout << "c1的体积为:" << c1.calculateV() << endl;
	cube c2;
	c2.setL(10);
	c2.setW(10);
	c2.setH(10);
	bool ret = Isseam(c1, c2);
	if (ret)
	{
		cout << "c1和c2是相等的!" << endl;
	}
	else
	{
		cout << "c1和c2是不相等的!" << endl;
	}
	ret =c1.isseambyclass(c2);
	if (ret)
	{
		cout << "成员函数判断:c1和c2是相等的!" << endl;
	}
	else
	{
		cout << "成员函数判断:c1和c2是不相等的!" << endl;
	}
	system("pause");
	return 0;
}

案例2

#include<iostream>
using namespace std;
class point
{
public:
	//设置x
	void setx(int x)
	{
		m_x = x;
	}
	//获取
	int getx()
	{
		return m_x;
	}
	//设置y
	void sety(int y)
	{
		m_y = y;
	}
	//获取
	int gety()
	{
		return m_y;
	}
private:
	int m_x;
	int m_y;
};
class circle
{
public:
	void setr(int r)
	{
		m_r = r;
	}
	int getr()
	{
		return m_r;
	}
	void setcenter(point center)
	{
		m_center=center;
	}
	point getcenter()
	{
		return m_center;
	}
private:
	int m_r;//半径
	//在类中可以让另一个类作为本类中的成员
	point m_center;//圆心
};
//判断点圆关系
void isincircle(circle &c, point &p)
{
	int distance =
		(c.getcenter().getx() - p.getx())*(c.getcenter().getx() - p.getx()) +
		(c.getcenter().gety() - p.gety())*(c.getcenter().gety() - p.gety());
	int rdistance = c.getr()*c.getr();
	if (distance == rdistance)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance > rdistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}
int main()
{
	//创建圆
	circle c;
	c.setr(10);
	point center;
	center.setx(10);
	center.sety(0);
	c.setcenter(center);
	//创建点
	point p;
	p.setx(10);
	p.sety(11);
	//判断
	isincircle(c, p);
	system("pause");
	return 0;
}

分开在头文件和cpp中

point.h

#pragma once
#include<iostream>
using namespace std;
class point
{
public:
	//设置x
	void setx(int x);
	//获取
	int getx();
	//设置y
	void sety(int y);
	//获取
	int gety();
private:
	int m_x;
	int m_y;
};

point.cpp

#include"point.h"
//设置x
void point::setx(int x)
{
	m_x = x;
}
//获取
int point::getx()
{
	return m_x;
}
//设置y
void point::sety(int y)
{
	m_y = y;
}
//获取
int point::gety()
{
	return m_y;
}

circle.h

#pragma once
#include<iostream>
using namespace std;
#include"point.h"
class circle
{
public:
	void setr(int r);
	int getr();
	void setcenter(point center);
	point getcenter();
private:
	int m_r;//半径
			//在类中可以让另一个类作为本类中的成员
	point m_center;//圆心
};

circle.cpp

#include"circle.h"
void circle::setr(int r)
{
	m_r = r;
}
int circle::getr()
{
	return m_r;
}
void circle::setcenter(point center)
{
	m_center = center;
}
point circle::getcenter()
{
	return m_center;
}

main函数

#include<iostream>
using namespace std;
#include"circle.h"
#include"point.h"
//判断点圆关系
void isincircle(circle &c, point &p)
{
	int distance =
		(c.getcenter().getx() - p.getx())*(c.getcenter().getx() - p.getx()) +
		(c.getcenter().gety() - p.gety())*(c.getcenter().gety() - p.gety());
	int rdistance = c.getr()*c.getr();
	if (distance == rdistance)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance > rdistance)
	{
		cout << "点在圆外" << endl;
	}
	else
	{
		cout << "点在圆内" << endl;
	}
}
int main()
{
	//创建圆
	circle c;
	c.setr(10);
	point center;
	center.setx(10);
	center.sety(0);
	c.setcenter(center);
	//创建点
	point p;
	p.setx(10);
	p.sety(11);
	//判断
	isincircle(c, p);
	system("pause");
	return 0;
}

2.对象的初始化和清理

2.1 构造函数和析构函数

编译器自动调用,如果我们不提供,编译器会提供,但是是空实现

构造函数:用于创建对象时为对象的成员属性赋值,由编译器自动调用,无须手动

析构函数:对象销毁前系统自动调用,执行清理工作

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

       没有返回值也不写void;函数名称与类名相同;构造函数可以有参数,因此可重载;自动调用,只可调动一次

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

      没有返回值也不写void;函数名称与类名相同,在名称前加~;不可以有参数;自动调用,只一次

#include<iostream>
using namespace std;
class person
{
public:
	//构造函数
	person()
	{
		cout << "person构造函数的调用" << endl;
	}
	//析构函数
	~person()
	{
		cout << "person析构函数的调用" << endl;
	}
};

void test01()
{
	person p;//在栈上的数据,test01实现完就释放了
}
int main()
{
	test01();//结果既有构造、也有析构的输出
	person p;//只有构造
	system("pause");
	return 0;
}

2.2 构造函数的分类及调用

两种分类方式:按参数:有参、无参   按类型:普通构造、拷贝构造

三中调用方式:括号法,显示法,隐式转换法

#include<iostream>
using namespace std;
class person
{
public:
	person()
	{
		cout << "person的无参构造函数调用" << endl;
	}
	person(int a)
	{
		age = a;
		cout << "person的有参构造函数调用" << endl;
	}
	//拷贝构造函数
	person(const person &p)
	{
		//将传入的人的身上所有属性,拷贝到我身上
		age = p.age;
		cout << "person的拷贝构造函数调用" << endl;
	}
	~person()
	{
		cout << "person的析构函数调用" << endl;
	}
	int age;
};
//调用
void test01()
{
	//括号法
	/*
	person p1;//默认构造函数调用
	person p2(10);//有参构造函数
	person p3(p2);//拷贝构造函数
	//注意事项1
	//调用默认构造函数时,不要加()
	//下面这行代码,编译器会认为是一个函数声明
	      //person p1();
	cout << "p2的年龄为:" << p2.age << endl;
	cout << "p3的年龄为:" << p3.age << endl;
	*/
	//显示法
	person p1;
	person p2 = person(10);//有参构造
	person p3 = person(p2);//拷贝构造
	person(10);//匿名对象  特点:当前行执行结束后,系统会自动回收
	//注意事项2:不要利用拷贝构造函数,初始化匿名对象  编译器会认为person (p3)==person p3		   
	//隐式转换法
	person p4 = 10;//相当于person p4=person(10);
	person p5 = p4;//拷贝构造函数
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.3 拷贝构造函数调用时机

使用一个已经创建完毕的对象来初始化一个新对象;

值传递的方式给函数参数传值

以值方式返回局部对象

#include<iostream>
using namespace std;
//拷贝构造函数

class person
{
public:
	person()
	{
		cout << "person默认构造函数调用" << endl;
	}
	person(int age)
	{
		cout << "person有参构造函数调用" << endl;
		m_age = age;
	}
	person(const person &p)
	{
		cout << "person拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}
	~person()
	{
		cout << "person析构函数调用" << endl;
	}
	int m_age;
};
//使用一个已经创建的对象初始化一个新对象
void test01()
{
	person p1(20);
	person p2(p1);
	cout << "p2的年龄为:" << p2.m_age << endl;
}
//值传递的方式给函数参数传值
void dowork(person p)
{

}
void test02()
{
	person p;
	dowork(p);
}
//以值方式返回局部对象
person dowork2()
{
	person p1;
	cout << (int*)&p1 << endl;
	return p1;
}
void test03()
{
	person p = dowork2();
	cout << (int*)&p << endl;
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

2.4 构造函数调用规则

默认下,至少给一个类添加三个函数

若定义有参构造函数,不再构造无参,提供默认拷贝构造

若定义拷贝构造函数,不再提供其他的

2.5 深拷贝与浅拷贝

浅拷贝:简单的赋值

深拷贝:在堆区重新申请空间,进行拷贝

#include<iostream>
using namespace std;
class person
{
public:
	person()
	{
		cout << "person默认构造函数调用" << endl;
	}
	person(int age,int height)
	{
		m_age = age;
		m_height=new int(height);
		cout << "person有参构造函数调用" << endl;
	}
	//自己实现拷贝构造函数,解决浅拷贝带来的问题
	person(const person &p)
	{
		cout << "person拷贝构造函数调用" << endl;
		m_age = p.m_age;
		//m_height = p.m_height;编译器默认实现
		//深拷贝操作
		m_height = new int(*p.m_height);
	}
	~person()
	{
		//析构代码,将堆区开辟的数据做释放操作
		if (m_height != NULL)
		{
			delete m_height;
			m_height = NULL;
		}
		cout << "person析构函数调用" << endl;
	}
	int m_age;
	int *m_height;
};

void test01()
{
	person p1(18,160);
	cout << "p1的年龄为:" << p1.m_age << "  p1的身高为:" <<* p1.m_height<< endl;
	person p2(p1);
	cout << "p2的年龄为:" << p2.m_age  << "  p2的身高为:" <<* p2.m_height<<endl;
}

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

如果属性有在堆区开辟的,一定要自己构造拷贝构造函数,避免浅拷贝出现的问题

2.6 初始化列表

构造函数():属性1(值1),

#include<iostream>
using namespace std;
//初始化列表
class person
{
public:
	//传统初始化操作
	/*
	person(int a, int b, int c)
	{
		m_a = a;
		m_b = b;
		m_c = c;
	}
	*/
	//初始化列表
	person(int a,int b,int c) :m_a(a), m_b(b), m_c(c)
	{

	}
	int m_a;
	int m_b;
	int m_c;
};
void test01()
{
	//person p(10, 20, 30);
	person p(30,20,20);
	cout << "m_a=" << p.m_a << endl;
	cout << "m_b=" << p.m_b << endl;
	cout << "m_c=" << p.m_c << endl;
}

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

2.7 类对象作为类成员

构造函数的调用:先调用类对象,再调用类

析构函数的调用:先调用类,再调用类对象

#include<iostream>
using namespace std;
#include<string>
class phone
{
public:
	phone(string pname)
	{
		cout << "phone的构造函数调用" << endl;
		m_pname = pname;
	}
	~phone()
	{
		cout << "phone的析构函数调用" << endl;
	}
	string m_pname;
};
class person
{
public:
	//phone m_phone=m_pname  隐式转换法
	person(string name, string pname):m_name(name),m_phone(pname)
	{
		cout << "person的构造函数调用" << endl;
	}
	~person()
	{
		cout << "person的析构函数调用" << endl;
	}
	string m_name;
	phone m_phone;
};
void test01()
{
	person p("张三", "苹果MAX");
	cout << p.m_name << "拿着" << p.m_phone.m_pname << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.8 静态成员

静态成员变量:所有对象共享数据,编译阶段分配内存,类内生命,类外初始化

静态成员函数:所有对象共享一个函数,只能访问静态成员变量

#include<iostream>
using namespace std;
#include<string>
//静态成员函数
class person
{
public:
	static void func()
	{
		m_a = 100;//静态函数可访问静态变量
		cout << "static void func调用" << endl;
	}
	static int m_a;//静态成员变量
	int m_b;//非静态
};
int person::m_a = 0;
void test01()
{
	//1.通过对象访问
	person p;
	p.func();
	//2.通过类名访问
	person::func();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3. c++对象模型和this指针

3.1 成员变量与成员函数分开存储

#include<iostream>
using namespace std;
#include<string>
class person
{
	int m_a;//非静态成员变量,属于类的对象上
	static int m_b;//静态成员变量,不属于类的对象上
	void func() {};//非静态成员函数 不属于类的对象上
	static void func2() {};//静态成员函数 不属于类的对象上
};
void test01()
{
	person p;
	//空对象占用内存为:1   为了区分空对象占内存的位置
	cout << "size of p=" << sizeof(p) << endl;
}
void test02()
{
	person p;  //整形4
	cout << "size of p=" << sizeof(p) << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

3.2 this 指针

this指针指向被调用的成员函数所属的对象

不需定义,直接使用

用途:当形参和成员变量同名时来区分;在类的非静态成员函数中返回对象本身,可使用return *this

#include<iostream>
using namespace std;
#include<string>
class person
{
public:
	person(int age)
	{
		this->age = age;
	}
	person& personaddage(person &p)
	{
		this->age += p.age;
		return *this;
	}
	int age;

};
//解决名称冲突
void test01()
{
	person p1(18);
	cout << "p1的年龄:" << p1.age << endl;
}
//返回对象本身用*this
void test02()
{
	person p1(10);
	person p2(10);
	//链式编程思想
	p2.personaddage(p1).personaddage(p1).personaddage(p1).personaddage(p1);
	cout << "p2的年龄:" << p2.age << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

3.3 空指针访问成员函数

#include<iostream>
using namespace std;
class person
{
public:
	void showclassname()
	{
		cout << "This is a person class" << endl;
	}
	void showpersonage()
	{
		if (this == NULL)
		{
			return;
		}
		cout << "age=" << this->m_age<<endl;
	}
	int m_age;
};
void test01()
{
	person *p = NULL;
	p->showclassname();
	p->showpersonage();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.4 const修饰成员函数

常函数:成员函数后加const;不可修改成员属性;成员属性声明时加mutable,常函数中依旧可更改

常对象:声明对象前加const;常对象只能调用常函数

#include<iostream>
using namespace std;
//常函数
class person
{
public:
	//this是指针常量,指向不可改变
	//在成员函数后面加const,修饰的是this指针,让他指向的值也不能更改
	void showperson()const
	{
		this->m_b = 100;
		//this->m_a = 100;//错误
	}
	int m_a;
	mutable int m_b;//特殊变量,在长函数中,也可修改
};
void test01()
{
	person p;
	p.showperson();
}
//常对象
void teat02()
{
	const person p;
	//p.m_a = 100;不可修改
	p.m_b = 100;
	p.showperson();//只能调用常函数
}
int main()
{
	
	system("pause");
	return 0;
}

4.友元

让一个函数或者类访问另一个类中私有成员

三种实现

4.1 全局函数做友元

#include<iostream>
using namespace std;
#include<string>
class building
{
	//goodgay可访问building 的私有成员
	friend void goodgay(building *build);
public:
	building()
	{
		m_sittingroom = "客厅";
		m_bedroom = "卧室";
	}
public:
	string m_sittingroom;//客厅
private:
	string m_bedroom;
};
//全局函数
void goodgay(building *build)
{
	cout << "好基友全局函数正在访问:" << build->m_sittingroom<<endl;
	cout << "好基友全局函数正在访问:" << build->m_bedroom << endl;
}
void test01()
{
	building build;
	goodgay(&build);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.2 类做友元

#include<iostream>
using namespace std;
#include<string>
class building;
class goodgay
{
public:
	goodgay();
	void visit();//参观函数访问building 中的属性
	building * build;

};
class building
{
	friend class goodgay;
public:
	building();
public:
	string m_sittingroom;
private:
	string m_bedroom;
};
//类外写成员函数
building::building()
{
	m_sittingroom = "客厅";
	m_bedroom = "卧室";
}
goodgay::goodgay()
{
	//创建建筑物对象
	build = new building;
}
void goodgay::visit()
{
	cout << "好基友类正在访问" << build->m_sittingroom << endl;
	cout << "好基友类正在访问" << build->m_bedroom << endl;
}
void test01()
{
	goodgay gg;
	gg.visit();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

4.3成员函数做友元

#include<iostream>
using namespace std;
#include<string>
class building;
class goodgay
{
public:
	goodgay();
	void visit();//参观函数访问building 中的属性
	void visit2();//参观函数不可访问building 中的属性
	building * build;

};
class building
{
	//goodgay下的visit可以访问building 内容
	friend void goodgay::visit();
public:
	building();
public:
	string m_sittingroom;
private:
	string m_bedroom;
};
//类外写成员函数
building::building()
{
	m_sittingroom = "客厅";
	m_bedroom = "卧室";
}
goodgay::goodgay()
{
	//创建建筑物对象
	build = new building;
}
void goodgay::visit()
{
	cout << "visit正在访问" << build->m_sittingroom << endl;
	cout << "visit正在访问" << build->m_bedroom << endl;
}
void goodgay::visit2()
{
	cout << "visit2正在访问" << build->m_sittingroom << endl;
	//cout << "visit2正在访问" << build->m_bedroom << endl;
}
void test01()
{
	goodgay gg;
	gg.visit();
	gg.visit2();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.运算符重载

重新定义,赋予另一种功能,以适应不同的数据类型

5.1 加号重载

实现两个自定义数据类型相加

#include<iostream>
using namespace std;
//加号运算符重载
class person
{
public:
	//1.成员函数重载+号
	/*
	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 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 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=p1.operator+(p2)
	//全局函数重载本质调用  person p3=operator+(p1,p2)
	person p3 = p1 + p2;
	//运算符重载,也可以发生函数重载
	person p4 = p1 + 10;
	cout << "p4.m_a=" << p4.m_a << endl;
	cout << "p4.m_b=" << p4.m_b << endl;
	cout << "p3.m_a=" << p3.m_a << endl;
	cout << "p3.m_b=" << p3.m_b << endl;

}

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

5.2 左移运算符重载

可输出自定义类型

#include<iostream>
using namespace std;
//左移重载
class person
{
	friend ostream & operator<<(ostream &cout, person &p);
public:
	person(int a, int b)
	{
		m_a = a;
		m_b = b;
	}
private:
	//成员函数重载,不能利用 ,因为无法实现cout再左侧
	//void operator<<(cout)
	int m_a;
	int m_b;
};
//全局函数  链式编程
ostream & operator<<(ostream &cout, person &p)//本质可简化为cout<<p 引用
{
	cout << "m_a=" << p.m_a << "  m_b=" << p.m_b << endl;
	return cout;
}
void test01()
{
	person p(10,10);
	//p.m_a = 10;
	//p.m_b = 10;
	cout << p << endl;//链式编程思想
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.3 递增运算符重载

实现自己的整形数据

#include<iostream>
using namespace std;
//自定义整形
class myinteger
{
	friend ostream & operator<<(ostream &cout, myinteger &myint);
public:
	myinteger()
	{
		m_num = 0;
	}
	//重载前置++运算符,返回引用是为了一直对一个数据进行递增操作
	myinteger& operator++()
	{
		//先做++
		m_num++;
		//再做自身返回
		return *this;
	}
	//重载后置++运算符
	//void operator++(int)   int 代表占位参数,区分前置和后置
	myinteger operator++(int)
	{
		//先记录当时结果
		myinteger temp = *this;
		//后递增
		m_num++;
		//最后将记录结果返回
		return temp;
	}
private:
	int m_num;
};
//重载左移
ostream & operator<<(ostream &cout, 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;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

5.4 赋值运算符重载

#include<iostream>
using namespace std;
class person
{
public:
	person(int age)
	{
		m_age = new int(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;
	}
	int *m_age;
};
void test01()
{
	person p1(18);
	person p2(20);
	person p3(30);
	p3=p2 = p1;//赋值操作
	cout << "p1的年龄为:" << *p1.m_age << endl;
	cout << "p2的年龄为:" << *p2.m_age << endl;
	cout << "p3的年龄为:" << *p3.m_age << endl;
}
int main()
{
	test01();
	/*
	int a = 10;
	int b = 20;
	int c = 30;
	c = b = a;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl;
	*///正确
	system("pause");
	return 0;
}

5.5 关系运算符重载

#include<iostream>
using namespace std;
#include<string>
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;
		}
		return false;
	}
	//重载!=号
	bool operator!=(person &p)
	{
		if (this->m_name == p.m_name&&this->m_age == p.m_age)
		{
			return false;
		}
		return true;
	}
	string m_name;
	int m_age;
};
void test01()
{
	person p1("Tom", 18);
	person p2("Tom", 18);
	if (p1 == p2)
	{
		cout << "p1和p2是相等的!" << endl;
	}
	else
	{
		cout << "p1和p2是不相等的!" << endl;
	}
	if (p1!=p2)
	{
		cout << "p1和p2是不相等的!" << endl;
	}
	else
	{
		cout << "p1和p2是相等的!" << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5.6 函数调用运算符重载

()

仿函数、无固定写法

#include<iostream>
using namespace std;
#include<string>
//打印类
class Myprint
{
public:
	//重载
	void operator()(string test)
	{
		cout << test << endl;
	}
};
void Myprint02(string test)
{
	cout << test << endl;
}
void test01()
{
	Myprint myprint;
	myprint("hello world");//使用起来类似函数调用
	Myprint02("hello world");//函数调用
}
//仿函数很灵活
class Myadd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};
void test03()
{
	Myadd myadd;
	int ret = myadd(100, 1000);
	cout << "ret=" << ret << endl;
	//匿名函数对象
	cout << Myadd()(100, 1000) << endl;
}
int main()
{
	test01();
	test03();
	system("pause");
	return 0;
}

6. 继承

定义类时,下级别的成员除了有上一级的共性,还有自己的特性

可考虑利用继承,减少重复代码

6.1 继承的基本语法

class 子类:继承方式 父类

子类也成为派生类,父类也称为基类

#include<iostream>
using namespace std;
#include<string>
//普通实现页面
/*
//Java页面
class Java
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}
	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java python C++...(公共分类列表)" << endl;
	}
	void content()
	{
		cout << "Java学科视频" << endl;
	}
};
//Python页面
class Python
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}
	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java python C++...(公共分类列表)" << endl;
	}
	void content()
	{
		cout << "Python学科视频" << endl;
	}
};
//C++
class C
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}
	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java python C++...(公共分类列表)" << endl;
	}
	void content()
	{
		cout << "C++学科视频" << endl;
	}
};
*/
//继承实现页面
class basepage
{
public:
	void header()
	{
		cout << "首页、公开课、登录、注册...(公共头部)" << endl;
	}
	void footer()
	{
		cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
	}
	void left()
	{
		cout << "Java python C++...(公共分类列表)" << endl;
	}

};
//页面
class Java :public basepage
{
public:
	void content()
	{
		cout << "Java学科视频" << endl;
	}
};
class Python :public basepage
{
public:
	void content()
	{
		cout << "Python学科视频" << endl;
	}
};
class C :public basepage
{
public:
	void content()
	{
		cout << "C++学科视频" << endl;
	}
};

void test01()
{
	cout << "Java下载视频页面如下:" << endl;
	Java ja;
	ja.header();
	ja.footer();
	ja.left();
	ja.content();
	cout << "--------------------------" << endl;
	cout << "Python下载视频页面如下:" << endl;
	Python py;
	py.header();
	py.footer();
	py.left();
	py.content();
	cout << "--------------------------" << endl;
	cout << "C下载视频页面如下:" << endl;
	C cc;
	cc.header();
	cc.footer();
	cc.left();
	cc.content();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6.2 继承方式

公共、保护、私有

#include<iostream>
using namespace std;
//公共
class base1
{
public:
	int m_a;
protected:
	int m_b;
private:
	int m_c;
};
class son1 :public base1
{
public:
	void func()
	{
		m_a = 10;//父类中的公共权限到子类中依旧是公共
		m_b = 10;//父类中的保护权限到子类中依旧是保护
		//m_c = 10;//父类中的私有权限成员,子类访问不到

	}
};
void test01()
{
	son1 s1;
	s1.m_a = 100;
	//s1.m_b = 100;//类外访问不到保护权限
}
//保护继承
class base2
{
public:
	int m_a;
protected:
	int m_b;
private:
	int m_c;
};
class son2 :protected base2
{
public:
	void func()
	{
		m_a = 10;//父类中公共成员变为子类的保护成员
		m_b = 20; //父类中保护成员仍然是保护
		//m_c = 30;不可访问
	}
};
void test02()
{
	son2 s2;
	//s2.m_a = 100;类外不可访问
}
//私有继承
class base3
{
public:
	int m_a;
protected:
	int m_b;
private:
	int m_c;
};
class son3 :private base3
{
public:
	void func()
	{
		m_a = 10;//父类中公共成员变为子类的私有成员
		m_b = 20; //父类中保护成员变为子类的私有成员
	   //m_c = 30;不可访问
	}
};
void test03()
{
	son3 s3;
	//s3.m_a = 100;类外不可访问
}
int main()
{

	system("pause");
	return 0;
}

6.3 继承中的对象模型

从父类继承过来的成员,哪些属于子类对象中?

#include<iostream>
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;
};
//利用开发人员命令提示工具查看对象模型
//跳转盘符 E:
//跳转文件路径  cd 具体路径下
//查看命令
// cl /d1 reportSingleClassLayout类名 文件名
void test01()
{
	//父类中所有非静态成员属性都会被子类继承
	//父类中私有成员属性是被编译器给隐藏了,因此访问不到
	cout << "size of son=" << sizeof(son) << endl;//结果为16
}

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

6.4 继承中构造和析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数

#include<iostream>
using namespace std;
class base
{
public:
	base()
	{
		cout << "base构造函数!" << endl;
	}
	~base()
	{
		cout << "base析构函数!" << endl;
	}
};
class son :public base
{
public:
	son()
	{
		cout << "son构造函数!" << endl;
	}
	~son()
	{
		cout << "son析构函数!" << endl;
	}
};
void test01()
{
	//base b;
	//顺序为:先构造父类,再构造子类;先析构子类,再析构父类
	son s;
}
int main()
{

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

6.5继承同名成员处理方式

当父类与子类出现同名的成员,如何通过子类对象访问到子类或父类同名数据

访问子类同名成员,直接访问

访问父类同名成员 ,需要加作用域

#include<iostream>
using namespace std;
class base
{
public:
	base()//构造函数
	{
		m_a=100;
	}
	void func()
	{
		cout << "base-func()调用" << endl;
	}
	void func(int a)
	{
		cout << "base-func(int a)调用" << endl;
	}
	int m_a;
};
class son :public base
{
public:
	son()
	{
		m_a = 200;
	}
	void func()
	{
		cout << "son-func()调用" << endl;
	}
	int m_a;
};
void test01()
{
	son s;
	cout << "son m_a=" << s.m_a << endl;
	cout << "base m_a=" << s.base::m_a << endl;
}
void test02()
{
	son s;
	s.func();//直接调用,调用的是子类中的成员函数
	s.base::func();
	//如果子类中出现和父类同名的成员函数,子类的同名函数会隐藏掉父类的所有同名成员
	//要加作用域
	s.base::func(100);
}
int main()
{
	test02();
	test01();
	system("pause");
	return 0;
}

6.6 继承同名静态成员处理方式

与非静态处理一样

#include<iostream>
using namespace std;
//静态成员  类内声明,类外初始化
class base
{
public:
	
	static int m_a;
	static void func()
	{
		cout << "base-static-void func()" << endl;
	}
	static void func(int a)
	{
		cout << "base-static-void func(int a)" << 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 test01()
{
	//1.通过对象访问
	cout << "通过对象访问:" << endl;
	son s;
	cout << "son m_a=" << s.m_a << endl;
	cout << "base m_a=" << s.base::m_a << endl;
	//2.通过类名访问
	cout << "通过类名访问:" << endl;
	cout<< "son m_a=" << son::m_a << endl;
	//第一个::代表通过类名方式访问 第二个::表示父类作用域下
	cout << "base m_a=" << son::base::m_a << endl;
}
//同名静态成员函数
void test02()
{
	//1.通过对象
	cout << "通过对象访问:" << endl;
	son s;
	s.func();
	s.base::func();
	//2.通过类名
	cout << "通过类名访问:" << endl;
	son::func();
	son::base::func();
	son::base::func(100);
}
int main()
{
	test02();
	test01();
	system("pause");
	return 0;
}

6.7 多继承语法

一个类继承多个类

class 子类:继承方式 父类1,继承方式 父类2.。。。

可能会引发父类中有同名成员出现,需要加作用于区分

不建议多继承

#include<iostream>
using namespace std;

class base1
{
public:
	base1()
	{
		m_a = 100;
	}
	int m_a;
};
class base2
{
public:
	base2()
	{
		m_a = 200;
	}
	int m_a;
};
class son :public base1, public base2
{
public:
	son()
	{
		m_c = 300;
		m_d = 400;
	}
	int m_c;
	int m_d;
};
void test01()
{
	son s;
	cout << "sizeof son=" << sizeof(son) << endl;
	//当父类中出现童名成员,需要加作用域
	cout << "base1 m_a=" << s.base1::m_a<< endl;
	cout << "base2 m_a=" << s.base2::m_a << endl;

}

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

6.8 菱形继承

两个派生类继承同一个基类

又有某个类同时继承两个派生类

#include<iostream>
using namespace std;
//动物类
class animal
{
public:
	int m_age;
};
//利用虚继承可以解决菱形继承的问题,双重继承
//在继承之前加virtual
//羊类
class sheep:virtual public animal{};
//驼类
class tuo :virtual public animal {};
//杨驼类
class sheeptuo:public sheep,public tuo{};
void test01()
{
	sheeptuo st;
	st.sheep::m_age = 18;
	st.tuo::m_age = 28;
	//菱形继承时,当两个父类拥有同名,需要加作用域区分
	cout << "st.sheep::m_age = " << st.sheep::m_age << endl;
	cout << "st.tuo::m_age = " << st.tuo::m_age << endl;
	cout << st.m_age << endl;//结果只有28
}
int main()
{
	test01();
	system("pause");
	return 0;
}

7. 多态

7.1 基本概念

静态多态:函数和运算符重载  函数地址早绑定、编译阶段确定函数地址

动态多态:派生类、虚函数实现  函数地址晚绑定、运行阶段确定函数地址

#include<iostream>
using namespace std;
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 dospeak(animal &animl)
{
	animl.speak();
}
void test01()
{
	Cat cat;
	dospeak(cat);
	Dog dog;
	dospeak(dog);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

7.2 案例:计算器类

组织结构清晰、可读性强、对于后期和前期扩展和维护性高

#include<iostream>
using namespace std;
#include<string>
//普通写法
class calculator
{
public:
	int getresult(string oper)
	{
		if (oper == "+")
		{
			return m_num1 + m_num2;
		}
		else if (oper == "-")
		{
			return m_num1 - m_num2;
		}
		else if (oper == "*")
		{
			return m_num1 * m_num2;
		}
		//如果想扩展新的功能,得对源代码修改
		//在真实开发中,提倡开闭原则:对扩展进行开发,对修改进行关闭
	}
	int m_num1;
	int m_num2;
};
void test01()
{
	calculator c;
	c.m_num1 = 10;
	c.m_num2 = 10;
	cout << c.m_num1 << "+" << c.m_num2 <<"="<< c.getresult("+") << endl;
	cout << c.m_num1 << "-" << c.m_num2 << "="<<c.getresult("-") << endl;
	cout << c.m_num1 << "*" << c.m_num2 <<"=" <<c.getresult("*") << endl;
}
//利用多态实现
//实现计算器抽象类
class abstractcalculator
{
public:
	virtual int getresult()
	{
		return 0;
	}
	int m_num1;
	int m_num2;
};
//加法计算器
class addcalculator :public abstractcalculator
{
	int getresult()
	{
		return m_num1+m_num2;
	}
};
class subcalculator :public abstractcalculator
{
	int getresult()
	{
		return m_num1 - m_num2;
	}
};
class mulcalculator :public abstractcalculator
{
	int getresult()
	{
		return m_num1 * m_num2;
	}
};
void test02()
{
	abstractcalculator *abc = new addcalculator;
	abc->m_num1 = 100;
	abc->m_num2 = 100;
	cout << abc->m_num1 << "+" << abc->m_num2 << "=" << abc->getresult() << endl;
	//用完后销毁
	delete abc;
	//减法
     abc=new subcalculator;
	 abc->m_num1 = 100;
	 abc->m_num2 = 100;
	 cout << abc->m_num1 << "-" << abc->m_num2 << "=" << abc->getresult() << endl;
	 delete abc;
	 //乘法
	 abc = new mulcalculator;
	 abc->m_num1 = 100;
	 abc->m_num2 = 100;
	 cout << abc->m_num1 << "*" << abc->m_num2 << "=" << abc->getresult() << endl;
	 delete abc;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

7.3 纯虚函数和抽象类

virtual 返回值类型 函数名(参数列表)=0

有纯虚函数的类成为抽象类

抽象类:无法实例化对象,子类必须重写纯虚函数

#include<iostream>
using namespace std;
#include<string>
class base
{
public:
	virtual void func() = 0;//纯虚函数

};
class son :public base
{
public:
	void func()
	{
		cout << "func函数调用" << endl;
	}
};
void test01()
{
	//base b;不可实例化
	//new base;//不可实例化
	//son s;//子类必须重写
	base *bs = new son;
	bs->func();
}
int main()
{
	test01();	
	system("pause");
	return 0;
}

7.3 案例 制作饮品

#include<iostream>
using namespace std;
#include<string>
class abstractdrinking 
{
public:
	virtual void boil() = 0;//煮水
	virtual void brew() = 0;//冲泡
	virtual void pourincup() = 0;//倒入杯中
	virtual void putsomething() = 0;//加入辅料
	void makedrink()//制作饮品
	{
		boil();
		brew();
		pourincup();
		putsomething();
	}
};
//咖啡
class coffee :public abstractdrinking
{
public:
	void boil()
	{
		cout << "煮农夫山泉" << endl;
	 }
	void brew()
	{
		cout << "冲泡咖啡" << endl;
	}
	void pourincup()
	{
		cout << "倒入杯中" << endl;
	}
	void putsomething()
	{
		cout << "加入糖和牛奶" << endl;
	}
};
//茶叶
class Tea :public abstractdrinking
{
public:
	void boil()
	{
		cout << "煮开水" << endl;
	}
	void brew()
	{
		cout << "冲泡茶叶" << endl;
	}
	void pourincup()
	{
		cout << "倒入杯中" << endl;
	}
	void putsomething()
	{
		cout << "加入枸杞" << endl;
	}
};
//制作函数
void dowork(abstractdrinking *abs)
{
	abs->makedrink();
	delete abs;
}
void test01()
{
	dowork(new coffee);
	cout << "--------------------------" << endl;
	dowork(new Tea);
}
int main()
{
	test01();	
	system("pause");
	return 0;
}

7.5 虚析构和纯虚析构

如果子类中有属性开辟到堆区,那么父亲指针在释放时无法调用到子类的析构代码

解决方式:将父类中的析构函数改为虚析构或者纯虚析构

可以解决父类指针释放子类对象;都需要有具体的函数实现

虚析构:virtual ~类名(){}

纯虚析构:virtual ~类名()=0

如果子类中没有堆区数据,可以不写为虚析构、纯虚析构

#include<iostream>
using namespace std;
#include<string>
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 test01()
{
	Animal *animal = new Cat("Tom");
	animal->speak();
	//父类指针在析构时,不会调用子类的析构函数,导致如果子类有堆区属性,内存会泄露
	delete animal;
}
int main()
{
	test01();	
	system("pause");
	return 0;
}

7.6案例:电脑组装

#include<iostream>
using namespace std;
#include<string>
//抽象不同零件类
//cpu
class CPU
{
public:
	virtual void calculator() = 0;//计算
};
class Videocard
{
public:
	virtual void display() = 0;//显示
};
class Memory
{
public:
	virtual void storage() = 0;//存储
};
//电脑类
class Computer
{
public:
	Computer(CPU *cpu, Videocard *vc, Memory *mem)
	{
		m_cpu = cpu;
		m_vc = vc;
		m_mem = mem;
	}
	//工作函数
	void work()
	{
		m_cpu->calculator();
		m_vc->display();
		m_mem->storage();
	}
	//释放三个零件
	~Computer()
	{
		if (m_cpu != NULL)
		{
			delete m_cpu;
			m_cpu = NULL;
		}
		if (m_vc != NULL)
		{
			delete m_vc;
			m_vc = NULL;
		}
		if (m_mem != NULL)
		{
			delete m_mem;
			m_mem = NULL;
		}
	}
private:
	CPU *m_cpu;//指针
	Videocard *m_vc;
	Memory *m_mem;
};
//具体厂商
class IntelCPU :public CPU
{
public:
	virtual void calculator()
	{
		cout << "Intel的CPU开始计算了!" << endl;
	}
};
class IntelVideocard :public Videocard
{
public:
	virtual void display()
	{
		cout << "Intel的Videocard开始显示了!" << endl;
	}
};
class IntelMemory :public Memory
{
public:
	virtual void storage()
	{
		cout << "Intel的Memory开始储存了!" << endl;
	}
};
//lenovo厂商
class lenovoCPU :public CPU
{
public:
	virtual void calculator()
	{
		cout << "lenovo的CPU开始计算了!" << endl;
	}
};
class lenovoVideocard :public Videocard
{
public:
	virtual void display()
	{
		cout << "lenovo的显卡开始显示了!" << endl;
	}
};
class lenovoMemory :public Memory
{
public:
	virtual void storage()
	{
		cout << "lenovo的内存开始储存了!" << endl;
	}
};
void test01()
{
	 //第一台电脑零件
	CPU *intelcpu = new IntelCPU;
	Videocard *intelcard = new IntelVideocard;
	Memory *intelMem = new IntelMemory;
	cout << "第一台电脑开始工作" << endl;
	//创建第一台电脑
	Computer * computer1 = new Computer(intelcpu, intelcard, intelMem);
	computer1->work();
	delete computer1;
	cout << "--------------------------" << endl;
	cout << "第二台电脑开始工作" << endl;
	//第二台电脑组装
	Computer * computer2 = new Computer(new lenovoCPU, new lenovoVideocard,new lenovoMemory);
	computer2->work();
	delete computer2;
	cout << "--------------------------" << endl;
	cout << "第三台电脑开始工作" << endl;
	//第三台电脑组装
	Computer * computer3 = new Computer(new IntelCPU, new lenovoVideocard, new lenovoMemory);
	computer3->work();
	delete computer3;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

五、文件操作

对文件操作需要加头文件<fstream>

文件类型:文本文件:以文本的ASCII码存储

                   二进制文件:以文本的二进制形式,用户一般不能读懂

操作文件的三大类:ofstream 写操作ifstream  fstream

1.文本文件

1.1 写文件

(1)步骤

包含头文件:#include<fstream>

创建流对象:ofstream ofs;

打开文件:ofs.open("文件路径",打开方式)

写数据:ofs<<"写入的数据"

关闭文件:ofs.close();

(2)文件打开方式

ios::in   为读文件而打开文件

ios::out   为写文件而打开文件

ios::ate  初始位置:文件尾

ios::app   追加方式写文件

ios::trunc   如果文件存在,先删除,再创建

ios::binary   二进制方式

文件打开方式可以配合使用,利用| 操作符

#include<iostream>
using namespace std;
#include<fstream>//头文件

void test01()
{
	//创建流对象
	ofstream ofs;
    //指定打开方式
	ofs.open("test.txt", ios::out);
	//写内容
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;
	//关闭文件
	ofs.close();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

1.2 读文件

(1)步骤

包含头文件:#include<fstream>

创建流对象:ifstream ifs;

打开文件并判断文件是否成功打开:ifs.open("文件路径",打开方式)

读数据:4种方式

关闭文件:ifs.close();

#include<iostream>
using namespace std;
#include<fstream>//头文件
#include<string>
void test01()
{
	//创建流对象
	ifstream ifs;
    //打开文件并判断
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
		return;
	}
	//读数据
	//第一种
	/*
	char buf[1024] = { 0 };
	while (ifs >> buf)
	{
		cout << buf << endl;
	}
	*/
	//第二种
	/*
	char buf[1024] = { 0 };
	while (ifs.getline(buf,sizeof(buf)))
	{
		cout << buf << endl;
	}
	*/
	//第三种
	/*
	string buf;
	while (getline(ifs, buf))
	{
		cout << buf << endl;
	}
	*/
	//第四种  逐个读
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;
	}
	//关闭
	ifs.close();

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

2.二进制文件

打开方式要指定:ios::binary

2.1 写文件

流对象调用成员函数:write

ostream& write(const char * buffer,int len)  buffer指向内存中一段存储空间,len 读写的字节数

#include<iostream>
using namespace std;
#include<fstream>//头文件

//写文件
class person
{
public:
	char m_name[64];
	int m_age;
};
void test01()
{
	ofstream ofs;
	ofs.open("person.txt", ios::out | ios::binary);
	person p = { "张三" ,18};
	ofs.write((const char *)&p, sizeof(person));
	ofs.close();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.2读文件

read

istream& write(char * buffer,int len) 

#include<iostream>
using namespace std;
#include<fstream>//头文件

//读文件
class person
{
public:
	char m_name[64];
	int m_age;
};
void test01()
{
	ifstream ifs;//创建流对象
	ifs.open("person.txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
	}
	person p;
	ifs.read((char *)&p, sizeof(person));
	cout << "姓名:" << p.m_name << "年龄:" << p.m_age << endl;
	ifs.close();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

六、职工管理系统

1.管理系统需求

公司职工:普通员工、经理、老板   显示信息时,要显示职工编号、职工姓名、职工岗位、职责

普通员工职责:完成经理交给的任务

经理职责:完成老板交给的任务,并下发任务给员工

老板职责:管理公司所有事务

注意:添加员工时实现批量添加;删除时按编号删除

2.创建项目

3.创建管理类

内容:与用户的沟通菜单界面;对职工增删改查的操作;与文件的读写交互

4.菜单功能

5.退出功能

6.创建职工类

7.添加职工

8.文件交互-选文件

9.文件交互-读文件

10.显示员工

11.删除员工

12.修改员工

13.查找职工

14.排序

15.清空文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值