[C++] 多態案例-計算機組成

鹹菜蘿蔔幹 2022-01-07 22:09:34 阅读数:457

c++ 案例

多態案例-計算機組成

#include <iostream>
using namespace std;

//CPU基類
class CPU
{
public:
//
//純虛函數
virtual void calculate() = 0;
};

//顯卡基類
class VideoCard
{
public:
virtual void display() = 0;
};

//內存基類
class Memory
{
public:
virtual void storage() = 0;
};

//電腦類
class computer
{
public:

computer( CPU * cpu, VideoCard * vc, Memory * mem)
{
cout << "電腦的構造調用" << endl;
this -> m_cpu = cpu;
this -> m_vc = vc;
this -> m_mem = mem;
}

void doWork()
{
this -> m_cpu -> calculate();
this -> m_vc -> display();
this -> m_mem -> storage();
}

~computer()
{
cout << "電腦的析構調用" << endl;
if ( this -> m_cpu)
{
delete this -> m_cpu;
this -> m_cpu = NULL;
}
if ( this -> m_vc)
{
delete this -> m_vc;
this -> m_vc = NULL;
}
if ( this -> m_mem)
{
delete this -> m_mem;
this -> m_mem = NULL;
}
}

CPU * m_cpu;
VideoCard * m_vc;
Memory * m_mem;
};

//廠商 inter
class interCPU : public CPU
{
public:
void calculate()
{
cout << "inter CPU 開始計算了" << endl;
}
};

class interVideoCard : public VideoCard
{
public:
void display()
{
cout << "inter 顯卡 開始顯示了" << endl;
}
};

class interMemory : public Memory
{
public:
void storage()
{
cout << "inter 內存 開始存儲了" << endl;
}
};

//Lenovo
class LenoveCPU : public CPU
{
public:
void calculate()
{
cout << "Lenove CPU 開始計算了" << endl;
}
};

class LenoveVideoCard : public VideoCard
{
public:
void display()
{
cout << "Lenove 顯卡 開始顯示了" << endl;
}
};

class LenoveMemory : public Memory
{
public:
void storage()
{
cout << "Lenove 內存 開始存儲了" << endl;
}
};


int main()
{
cout << "組裝一臺電腦" << endl;
CPU * intercpu = new interCPU; //多態:父類的指針指向子類的對象
VideoCard * lenovevc = new LenoveVideoCard;
Memory * lenovemem = new LenoveMemory;


computer c1( intercpu, lenovevc, lenovemem);
c1. doWork();

return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
版权声明:本文为[鹹菜蘿蔔幹]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201072209338712.html