纯虚函数之饮品制作,三种方式实现函数的调用:
代码实现:
#include<iostream>
#include<vector>
#include <fstream>
#include<algorithm>
#include<string>
using namespace std;
using namespace cv;
class AbstructMakedrink {
public:
virtual void Boil() = 0;
virtual void Brew() = 0;
virtual void PutIncup() = 0;
virtual void Putsomething() = 0;
void Makedrinking() {
Boil();
Brew();
PutIncup();
Putsomething();
}
};
class MakeCoffe:public AbstructMakedrink {
public:
void Boil() {
cout << "煮农夫山泉" << endl;
}
void Brew() {
cout << "冲泡咖啡" << endl;
}
void PutIncup() {
cout << "倒入瓷器杯中" << endl;
}
void Putsomething() {
cout << "加入糖和牛奶" << endl;
}
};
class MakeTea :public AbstructMakedrink {
public:
void Boil() {
cout << "煮矿泉水" << endl;
}
void Brew() {
cout << "冲泡茶叶" << endl;
}
void PutIncup() {
cout << "倒入玻璃杯中" << endl;
}
void Putsomething() {
cout << "加入柠檬" << endl;
}
};
//传入指针的第一种方式实现饮品制作
//void Dowork(AbstructMakedrink* drink) {
// drink->Makedrinking();
// delete drink;
//}
//使用引用的方式制作饮品
void Dowork(AbstructMakedrink&drink) {
drink.Makedrinking();
}
void test02() {
//传入指针的第一种方式实现饮品制作
/*Dowork(new MakeCoffe);
Dowork(new MakeTea);*/
//使用堆区开辟指针的第二种方式实现饮品制作
//AbstructMakedrink* drink = new MakeCoffe;
//drink->Makedrinking();
//delete drink;
//drink = new MakeTea;
//drink->Makedrinking();
//delete drink;
//使用引用的方式制作饮品
MakeCoffe Coffe;
Dowork(Coffe);
MakeTea tea;
Dowork(tea);
}
int main() {
test02();
system("pause");
return 0;
}
2.电脑组装:
#include<iostream>
#include<vector>
#include <fstream>
#include<algorithm>
#include<string>
using namespace std;
using namespace cv;
class Cpu {
public:
virtual void Caculate() = 0;
};
class VideoCard {
public:
virtual void Display() = 0;
};
class Mermeoy {
public:
virtual void Strorage() = 0;
};
class IntelCpu :public Cpu {
public:
virtual void Caculate() {
cout << "Intel的Cpu开始计算了" << endl;
}
};
class IntelVideoCard :public VideoCard {
public:
virtual void Display() {
cout << "Intel的显卡开始显示了" << endl;
}
};
class IntelMermeoy :public Mermeoy {
public:
virtual void Strorage() {
cout << "Intel的内存开始存储了" << endl;
}
};
class HuiPCpu :public Cpu {
public:
virtual void Caculate() {
cout << "HuiP的Cpu开始计算了" << endl;
}
};
class HuiPVideoCard :public VideoCard {
public:
virtual void Display() {
cout << "HuiP的显卡开始显示了" << endl;
}
};
class HuiPMermeoy :public Mermeoy {
public:
virtual void Strorage() {
cout << "HuiP的内存开始存储了" << endl;
}
};
class Computer {
public:
Computer(Cpu*cpu, VideoCard*vd, Mermeoy*mem) {
m_cpu = cpu;
m_vd = vd;
m_mem = mem;
}
void Dowork() {
m_cpu->Caculate();
m_mem->Strorage();
m_vd->Display();
}
~Computer() {
if (m_cpu != NULL) {
delete m_cpu;
m_cpu = NULL;
}
if (m_vd != NULL) {
delete m_vd;
m_vd = NULL;
}
if (m_mem != NULL) {
delete m_mem;
m_mem = NULL;
}
}
private :
Cpu* m_cpu;//创建的类指针,并非实例化对象!
VideoCard* m_vd;
Mermeoy* m_mem;
};
void test02() {
Cpu* pu = new IntelCpu;
VideoCard* vd = new IntelVideoCard;
Mermeoy* me = new HuiPMermeoy;
//第一种电脑组装方法
Computer cp1(pu,vd,me);
cp1.Dowork();
delete pu;
delete vd;
delete me;
//第二、第三种电脑组装方法
//Computer* compu2 = new Computer(new IntelCpu,new HuiPVideoCard,new IntelMermeoy);
Cpu* pu3 = new IntelCpu;
VideoCard* vd3 = new IntelVideoCard;
Mermeoy* me3 = new HuiPMermeoy;
Computer* compu3 = new Computer(pu3,vd3,me3);//传指针的构造函数,有new,表示在堆区开辟空间返回指针指向构造函数。
compu3->Dowork();
delete compu3;
}
int main() {
test02();
system("pause");
return 0;
}