c++类和对象---多态

1、多态的基本概念

多态是c++面向对象的三大特性之一

多态分为两类:

1、静态多态:函数重载和运算符重载属于静态多态,复用函数名

2、动态多态:派生类和虚函数实现运行时多态

静态多态和动态多态的区别

1、静态多态的函数地址早绑定 - 编译阶段确定函数地址

2、动态多态的函数地址晚绑定 - 运行阶段确定函数地址

动态多态满足条件:

1、有继承关系

2、子类重写父类虚函数(完全相同)

动态多态使用

父类指针或者引用,指向子类对象

class animal
{
public:
	virtual void speak()
	{
		cout << "动物在说话" << endl;
	}
	int a_age;	
};

class cat : public animal
{
	void speak()
	{
		cout << "喵喵喵喵" << endl;
	}
	
};
class dog : public animal
{
	void speak()
	{
		cout << "汪汪汪汪" << endl;
	}

};
//地址早绑定,在编译阶段确定函数地址
//如果想执行让猫说话,那么就不能让函数地址早绑定,做法,在父类的函数前加virtual,变成虚函数
void doSpeak(animal & animal)
{
	animal.speak();
}

int main()
{
	cat cat1;
	doSpeak(cat1);
	dog dog1;
	doSpeak(dog1);
	
	system("pause");
	return 0;
}
喵喵喵喵
汪汪汪汪
请按任意键继续. . .


案例:
分别利用普通写法和多态技术,实现两个操作数进行运算的计算器类

多态的优点:
1、代码组织结构清晰,可读性强

2、有利于前期和后期的扩展和维护

using namespace std;
//普通写法
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;
		}
		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 = 2;
	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;
	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
{
public:
	int getResult()
	{
		return m_num1 + m_num2;
	}
};
class jianCalculator : public AbstractCalculator
{
public:
	int getResult()
	{
		return m_num1 - m_num2;
	}
};
class chengCalculator : public AbstractCalculator
{
public:
	int getResult()
	{
		return m_num1 * m_num2;
	}
};

void test02()
{
	AbstractCalculator * abc = new addCalculator;
	abc->m_num1 = 10;
	abc->m_num2 = 2;
	cout << abc->m_num1 << "+" << abc->m_num2 << "=" << abc->getResult() << endl;
	delete abc;

	abc = new jianCalculator;
	abc->m_num1 = 10;
	abc->m_num2 = 2;
	cout << abc->m_num1 << "-" << abc->m_num2 << "=" << abc->getResult() << endl;
	delete abc;

	abc = new chengCalculator;
	abc->m_num1 = 10;
	abc->m_num2 = 2;
	cout << abc->m_num1 << "*" << abc->m_num2 << "=" << abc->getResult() << endl;
	delete abc;
}
int main()
{
	//test01();
	test02();
	
	system("pause");
	return 0;
}

2、纯虚函数和抽象类

在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容

因此可以将虚函数改为纯虚函数

纯虚函数语法:virtual 返回值类型 函数名 (参数列表) = 0;

当类中有了纯虚函数,这个类也称为抽象类

抽象类特点:

1、无法实例化对象

2、子类必须重写抽象类中的纯虚函数,否则也属于抽象类

//纯虚函数
class AbstractCalculator
{
public:
	virtual int getResult() = 0;
	
	int m_num1;
	int m_num2;
};
class addCalculator : public AbstractCalculator
{
public:
	
};
class jianCalculator : public AbstractCalculator
{
public:
	virtual int getResult()
	{
		return m_num1 - m_num2;
	}
};
class chengCalculator : public AbstractCalculator
{
public:
	int getResult()
	{
		return m_num1 * m_num2;
	}
};

void test01()
{
	/*
	报错,纯虚函数属于抽象类,无法实例化对象
	AbstractCalculator a;
	new AbstractCalculator b;
	*/

	//addCalculator c;报错,子类没有重写父类中的纯虚函数,也不能实例化对象

	

	jianCalculator abc;
	abc.m_num1 = 10;
	abc.m_num2 = 2;
	cout << abc.m_num1 << "-" << abc.m_num2 << "=" << abc.getResult() << endl;
	
}
int main()
{
	test01();
	
	
	system("pause");
	return 0;
}

案例2:制做饮品的大致流程:煮水-冲泡-倒入杯中-加入辅料

class abstractDrink
{
public:
	virtual void boil() = 0;
	virtual void brew() = 0;
	virtual void pourInCup() = 0;
	virtual void putSome() = 0;
	void makeDrink()
	{
		boil();
		brew();
		pourInCup();
		putSome();
	}
};
class coffee : public abstractDrink
{
	virtual void boil()
	{
		cout << "煮水" << endl;
	}
	virtual void brew()
	{
		cout << "冲咖啡" << endl;
	}
	virtual void pourInCup()
	{
		cout << "倒入杯中" << endl;
	}
	virtual void putSome()
	{
		cout << "加入咖啡伴侣" << endl;
	}

};

class tea : public abstractDrink
{
	virtual void boil()
	{
		cout << "煮水" << endl;
	}
	virtual void brew()
	{
		cout << "冲茶" << endl;
	}
	virtual void pourInCup()
	{
		cout << "倒入杯中" << endl;
	}
	virtual void putSome()
	{
		cout << "加入白糖" << endl;
	}

};

void doWork(abstractDrink * abs)
{
	abs->makeDrink();
	delete abs;
}
void test01()
{
	doWork(new coffee);
	cout << "--------" << endl;
	doWork(new tea);
	
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3、虚析构和纯虚析构

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

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

虚析构和纯虚析构共同点:

1、可以解决父类指针释放子类对象

2、都需要具体的函数实现

区别:
1、如果是纯虚析构,该类属于抽象类,无返实例化对象

虚析构语法:

virtual ~类名(){}

纯虚析构语法:(需要声明也需要函数实现)!!!
类内声明:virtual ~类名() = 0;

类外函数实现:类名::~类名(){}

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);
	}
	virtual 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();//父类指针在析构时候,不会调用子类中析构函数,导致子类中如果有堆区属性,出现内存泄露
					//解决方法,将父类析构函数变为虚析构,析构函数前面加“virtual”
	delete animal;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
animal构造函数调用
cat构造函数调用
tom喵喵喵
cat析构函数调用
animal纯虚析构函数调用
请按任意键继续. . .

 总结:

  • 虚析构或纯虚析构就是用来解决通过父类指针释放子类对象
  • 如果子类中没有堆区数据,可以不写为虚析构或纯虚析构
  • 拥有纯虚析构函数的类也属于抽象类

案例3

class CPU
{
public:
	virtual void caculate() = 0;
};
class vidaoCard 
{
public:
	virtual void display() = 0;

};
class memory
{
public:
	virtual void storage() = 0;
};
class computer
{
public:
	computer(CPU * cpu, vidaoCard * vc, memory * mem)
	{
		m_cpu = cpu;
		m_vc = vc;
		m_mem = mem;
	}
	void work()
	{
		m_cpu->caculate();
		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;
	vidaoCard * m_vc;
	memory * m_mem;

};

class intercpu : public CPU
{
	virtual void caculate()
	{
		cout << "inter的cpu开始工作" << endl;
	}
};
class intervidao : public vidaoCard
{
	virtual void display()
	{
		cout << "inter的vidaocard开始工作" << endl;
	}
};
class intermemory : public memory
{
	virtual void storage()
	{
		cout << "inter的memory开始工作" << endl;
	}
};
class lenovocpu : public CPU
{
	virtual void caculate()
	{
		cout << "lenovo的cpu开始工作" << endl;
	}
};
class lenovovidao : public vidaoCard
{
	virtual void display()
	{
		cout << "lenovo的vidaocard开始工作" << endl;
	}
};
class lenovomemory : public memory
{
	virtual void storage()
	{
		cout << "lenovo的memory开始工作" << endl;
	}
};

void test01()
{
	CPU * interCPU = new intercpu;
	vidaoCard * intercard = new intervidao;
	memory * intermem = new intermemory;

	computer * computer1 = new computer(interCPU, intercard, intermem);
	computer1->work();
	delete computer1;
	
}
int main()
{
	test01();
	system("pause");
	return 0;
}

你可能感兴趣的:(c++学习笔记,c++)