#include <iostream>
#include <string>
using namespace std;
class Cpu
{public:
void virtual dogo() = 0;
};
class Vcard
{
public:
void virtual show() = 0;
};
class Memry
{
public:
virtual void store() = 0;
};
//电脑得类 属性是上面三个类
class computer
{public:
computer(Cpu* a, Vcard* b, Memry* c)
{
cpu = a;
vc = b;
mem = c;
}
//提供接口调用部件得函数
void dowork()
{
cpu->dogo();
vc->show();
mem->store();
}
private:
Cpu *cpu;
Vcard* vc;
Memry* mem;
};
class intelCpu :public Cpu
{
void virtual dogo()
{
cout << "intel的cpu搞事情了" << endl;
}
};
class intelVcard :public Vcard
{
public:
void virtual show()
{
cout << "intel的vc搞事情了" << endl;
}
};
class intelMemry :public Memry
{
public:
void virtual store()
{
cout << "intel的mem搞事情了" << endl;
}
};
class lenovoCpu :public Cpu
{
void virtual dogo()
{
cout << "lenovo的cpu搞事情了" << endl;
}
};
class lenovoVcard :public Vcard
{
public:
void virtual show()
{
cout << "lenovo的vc搞事情了" << endl;
}
};
class lenovoMemry :public Memry
{
public:
void virtual store()
{
cout << "lenovo的mem搞事情了" << endl;
}
};
void test()
{//intel
cout << "开始吗intel" << endl;
cout << "---------------------------------------------------------------" << endl;
Cpu* intelcpu = new intelCpu; //父类的指针可以指向 子类 相当于new了一个子类的对象 由父类接受
Vcard* intelvc = new intelVcard;
Memry* intelmem = new intelMemry;
computer* p1 = new computer(intelcpu, intelvc, intelmem);//电脑中有这3个属性 用一个指针来接受它们即可
p1->dowork();
//lenovo
cout << "开始吗lenovo" << endl;;
cout << "---------------------------------------------------------------" << endl;
computer* p2 = new computer(new lenovoCpu, new lenovoVcard, new lenovoMemry);
p2->dowork();
//混合起来
cout << "开始吗混合" << endl;;
cout << "---------------------------------------------------------------" << endl;
computer* p3 = new computer(new lenovoCpu, new lenovoVcard, new intelMemry);
p3->dowork();
}
int main()
{
test();
system("pause");
return 0;
}
多态 组装电脑
最新推荐文章于 2023-03-03 17:19:38 发布