多态简单运用,void函数里,Computer* computer1 = new Computer(..)
报错显示 没有与参数列表匹配的构造函数//************************多态案例三 电脑组装
#include <stdio.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;
class AbstractCpu
{
public:
virtual void calculate() = 0;
};
class AbstractVidocard
{
public:
virtual void display() = 0;
};
class AbstractMerory
{
public:
virtual void storage() = 0;
};
class Computer
{
public:
Computer(AbstractCpu* cpu, AbstractMerory* memory, AbstractVidocard* vidocard)
{
m_cpu = cpu;
m_merory = memory;
m_vidocard = vidocard;
}
//提供工作的函数
void work()
{
m_cpu->calculate();
m_merory->storage();
m_vidocard->display();
}
private:
AbstractCpu* m_cpu;
AbstractMerory* m_merory;
AbstractVidocard* m_vidocard;
};
//具体化厂商
//厂商1Intel
class IntelCUP :public AbstractCpu
{
public:
virtual void calculate()
{
cout << " Intel Cpu begin to work " << endl;
}
};
class IntelvedioCard :public AbstractVidocard
{
public:
virtual void display()
{
cout << " Intel vedioCard begin to work " << endl;
}
};
class IntelMemory :public AbstractMerory
{
public:
virtual void storage()
{
cout << " Intel storage begin to work " << endl;
}
};
//厂商2联想
class LevenoCUP :public AbstractCpu
{
public:
virtual void calculate()
{
cout << " Leveno Cpu begin to work " << endl;
}
};
class LevenovedioCard :public AbstractVidocard
{
public:
virtual void display()
{
cout << " Leveno vedioCard begin to work " << endl;
}
};
class LevenoMemory :public AbstractMerory
{
public:
virtual void storage()
{
cout << " Leveno storage begin to work " << endl;
}
};
void test()
{
//电脑零件
AbstractCpu* Intelc = new IntelCUP; //父类指针对象指向子类,多态的运用
AbstractVidocard* Intelv = new IntelvedioCard;
AbstractMerory* IntelM = new IntelMemory;
//创建第一台电脑
Computer* computer1 = new Computer(Intelc,Intelv,IntelM);
computer1->work();
delete computer1;
}
int main()
{
test();
return 0;
} //************************多态案例三 电脑组装
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class AbstractCpu {
public:
virtual void calculate() = 0;
virtual ~AbstractCpu() {}
};
class AbstractVidocard {
public:
virtual void display() = 0;
virtual ~AbstractVidocard() {}
};
class AbstractMerory {
public:
virtual void storage() = 0;
virtual ~AbstractMerory() {}
};
class Computer {
public:
Computer(AbstractCpu *cpu, AbstractMerory *memory,
AbstractVidocard *vidocard) {
m_cpu = cpu;
m_merory = memory;
m_vidocard = vidocard;
}
//提供工作的函数
void work() {
m_cpu->calculate();
m_merory->storage();
m_vidocard->display();
}
private:
AbstractCpu *m_cpu;
AbstractMerory *m_merory;
AbstractVidocard *m_vidocard;
};
//具体化厂商
//厂商1Intel
class IntelCUP : public AbstractCpu {
public:
virtual void calculate() { cout << " Intel Cpu begin to work " << endl; }
};
class IntelvedioCard : public AbstractVidocard {
public:
virtual void display() {
cout << " Intel vedioCard begin to work " << endl;
}
};
class IntelMemory : public AbstractMerory {
public:
virtual void storage() { cout << " Intel storage begin to work " << endl; }
};
//厂商2联想
class LevenoCUP : public AbstractCpu {
public:
virtual void calculate() { cout << " Leveno Cpu begin to work " << endl; }
};
class LevenovedioCard : public AbstractVidocard {
public:
virtual void display() {
cout << " Leveno vedioCard begin to work " << endl;
}
};
class LevenoMemory : public AbstractMerory {
public:
virtual void storage() { cout << " Leveno storage begin to work " << endl; }
};
void test() {
//电脑零件
AbstractCpu *Intelc = new IntelCUP; //父类指针对象指向子类,多态的运用
AbstractVidocard *Intelv = new IntelvedioCard;
AbstractMerory *IntelM = new IntelMemory;
//创建第一台电脑
//Computer *computer1 = new Computer(Intelc, Intelv, IntelM);
Computer *computer1 = new Computer(Intelc, IntelM, Intelv);
computer1->work();
delete computer1;
delete Intelc;
delete Intelv;
delete IntelM;
}
int main() {
test();
return 0;
}
页:
[1]