本帖最后由 Eden00 于 2023-4-20 22:44 编辑
#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <ctime>
using namespace std;
class CPU
{
public:
string cname;
virtual void calculate() = 0;
~CPU()
{
cout << cname << " CPU is destructed" << endl;
}
};
class VideoCard
{
public:
string vname;
virtual void display() = 0;
~VideoCard()
{
cout << vname << " video card is destructed" << endl;
}
};
class Memory
{
public:
string mname;
virtual void store() = 0;
~Memory()
{
cout << mname << " memory is destructed" << endl;
}
};
class Supplier : public CPU, public VideoCard, public Memory
{
string name;
void calculate()
{
cout << name << " CPU is calculating" << endl;
}
void store()
{
cout << name << " memory is stoing" << endl;
}
void display()
{
cout << name << " video card is displaying" << endl;
}
public:
Supplier()
{
cout << "Supplier is structed without argument" << endl;
}
Supplier(string n) : name(n)
{
cout << "Supplier is structed with name as " << n << endl;
}
string SetName(string n)
{
cout << "Supplier name is set by Class Supplier as " << n << endl;
name = n;
return name;
}
string GetName()
{
return name;
}
~Supplier()
{
cout << name << " supplier is destructed" << endl;
}
};
class Computer
{
CPU *c0;
VideoCard *v0;
Memory *m0;
public:
Computer(Supplier *c, Supplier *v, Supplier *m)
{
c0 = c;
v0 = v;
m0 = m;
c0->cname = c->GetName();
v0->vname = v->GetName();
m0->mname = m->GetName();
cout << "This computer is assembled with " << c0->cname << " CPU, " << v0->vname
<< " video card, " << m0->mname << " memory" << endl;
}
~Computer()
{
if (c0 != NULL)
{
cout << c0->cname << endl
<< c0 << endl;
delete c0;
c0 = NULL;
}
if (v0 != NULL)
{
cout << v0->vname << endl
<< v0 << endl;
delete v0;
v0 = NULL;
}
if (m0 != NULL)
{
delete m0;
m0 = NULL;
}
}
void work()
{
c0->calculate();
v0->display();
m0->store();
}
};
void fun0()
{
Supplier *q0=new Supplier("Qualcom");
Supplier *i0=new Supplier("Intel");
Supplier *a0=new Supplier("AMD");
Computer c0(q0, i0, a0);
c0.work();
}
int main()
{
fun0();
system("pause");
return 1;
}
|