37类—多态(多态的基本概念)
#include<iostream>
using namespace std;
#include<string>
class Animal
{
public:
virtual void speak()
{
cout << "动物会叫" << endl;
}
};
class Cat:public Animal
{
public:
void speak()
{
cout << "小猫喵喵叫" << endl;
}
};
void doSpeak(Animal & animal)
{
animal.speak();
}
void Test01()
{
Cat cat01;
doSpeak(cat01);
}
int main()
{
Test01();
system("pause");
return 0;
}
38类—多态(多态的原理剖析)
#include<iostream>
using namespace std;
#include<string>
class Animal01{
public:
void speak(){
cout << "动物会叫" << endl;
}
};
class Animal02{
public:
virtual void speak(){
cout << "动物会叫" << endl;
}
};
class Cat :public Animal02{
public:
virtual void speak(){
cout << "小猫喵喵叫" << endl;
}
};
void Test01(){
cout << "不带虚关键字的Animal类占用字节数:"<<sizeof(Animal01) << endl;
cout << "带虚关键字的Animal类占用字节数:" << sizeof(Animal02) << endl;
}
int main()
{
Test01();
system("pause");
return 0;
}
39类—多态(体验多态带来的便捷)
#include<iostream>
using namespace std;
#include<string>
class Calculater01{
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;
}
}
public:
int m_Num1;
int m_Num2;
};
void Test01(){
Calculater01 c1;
c1.m_Num1 = 10;
c1.m_Num2 = 10;
cout << c1.m_Num1 << "+" << c1.m_Num2 << "=" << c1.getResult("+") << endl;
cout << c1.m_Num1 << "-" << c1.m_Num2 << "=" << c1.getResult("-") << endl;
cout << c1.m_Num1 << "*" << c1.m_Num2 << "=" << c1.getResult("*") << endl;
}
class Calculater02{
public:
virtual int getResult(){
return 0;
}
public:
int m_Num1;
int m_Num2;
};
class Add:public Calculater02{
virtual int getResult()
{
return m_Num1 + m_Num2;
}
};
class Subtract:public Calculater02{
virtual int getResult()
{
return m_Num1 - m_Num2;
}
};
class Multiply :public Calculater02{
virtual int getResult()
{
return m_Num1 * m_Num2;
}
};
void Test02(){
Calculater02 *c2 = new Add;
c2->m_Num1 = 10;
c2->m_Num2 = 10;
cout << c2->m_Num1 << "+" << c2->m_Num2 << "=" << c2->getResult() << endl;
delete c2;
c2 = new Subtract;
c2->m_Num1 = 10;
c2->m_Num2 = 10;
cout << c2->m_Num1 << "-" << c2->m_Num2 << "=" << c2->getResult() << endl;
delete c2;
c2 = new Multiply;
c2->m_Num1 = 10;
c2->m_Num2 = 10;
cout << c2->m_Num1 << "*" << c2->m_Num2 << "=" << c2->getResult() << endl;
delete c2;
}
int main(){
Test01();
Test02();
system("pause");
return 0;
}
40类—多态(纯虚函数和抽象类)
#include<iostream>
using namespace std;
#include<string>
class Base
{
public:
virtual void func() = 0;
};
class Son :public Base
{
public:
virtual void func()
{
cout << "func调用" << endl;
};
};
void Test01()
{
Base * base = NULL;
base = new Son;
base->func();
delete base;
}
int main(){
Test01();
system("pause");
return 0;
}
41类—多态(虚析构和纯析构)
#include<iostream>
using namespace std;
#include<string>
class Animal{
public:
Animal(){
cout << "动物的构造函数执行了" << endl;
}
virtual ~Animal()= 0;
virtual void speak() = 0;
};
Animal::~Animal(){cout << "动物的析构函数执行了" << endl;}
class Cat :public Animal
{
public:
Cat(string name)
{
cout << "小猫的构造函数执行了" << endl;
m_Name=new string(name);
}
~Cat()
{
cout << "小猫的析构函数执行了"<<endl;
if (m_Name!=NULL)
{
delete m_Name;
m_Name = NULL;
}
}
virtual void speak()
{
cout <<*m_Name << "小猫喵喵叫" << endl;
}
string *m_Name;
};
void Test01()
{
Animal *animal = new Cat("Tom");
animal->speak();
delete animal;
}
int main(){
Test01();
system("pause");
return 0;
}
42类—多态(案例)
#include<iostream>
#include<string>
using namespace std;
class CPU
{
public:
virtual void calculate() = 0;
};
class VideoCard
{
public:
virtual void display() = 0;
};
class Memory
{
public:
virtual void storage() = 0;
};
class IntelCPU :public CPU
{
public:
virtual void calculate()
{
cout << "IntelCPU开始工作了" << endl;
}
};
class IntelVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "IntelVideoCard开始工作了" << endl;
}
};
class IntelMemory :public Memory
{
public:
virtual void storage()
{
cout << "IntelMemory开始工作了" << endl;
}
};
class AISMALLCPU :public CPU
{
public:
virtual void calculate()
{
cout << "AISMALLCPU开始工作了" << endl;
}
};
class AISMALLVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "AISMALLVideoCard开始工作了" << endl;
}
};
class AISMALLMemory :public Memory
{
public:
virtual void storage()
{
cout << "AISMALLMemory开始工作了" << endl;
}
};
class Computer{
public:
Computer(CPU *cpu, VideoCard * videoCard, Memory *memory)
{
m_cpu = cpu;
m_videoCard = videoCard;
m_memory = memory;
}
void doWork()
{
m_cpu->calculate();
m_videoCard->display();
m_memory->storage();
}
~Computer()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_videoCard != NULL)
{
delete m_videoCard;
m_videoCard = NULL;
}
if (m_memory != NULL)
{
delete m_memory;
m_memory = NULL;
}
}
private:
CPU *m_cpu;
VideoCard *m_videoCard;
Memory *m_memory;
};
void Test01()
{
CPU *intelCPU = new IntelCPU;
VideoCard *intelVideoCard = new IntelVideoCard;
Memory *intelmemory = new IntelMemory;
CPU *aismallCPU = new AISMALLCPU;
VideoCard *aismallVideoCard = new AISMALLVideoCard;
Memory *aismallmemory = new AISMALLMemory;
Computer *computer1 = new Computer(intelCPU, intelVideoCard, intelmemory);
computer1->doWork();
cout << "----------------------" << endl;
Computer *computer2 = new Computer(aismallCPU, aismallVideoCard, aismallmemory);
computer2->doWork();
cout << "----------------------" << endl;
Computer *computer3 = new Computer(intelCPU, aismallVideoCard, intelmemory);
computer3->doWork();
}
int main(){
Test01();
system("pause");
return 0;
}