Facebook
From Coral Pelican, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 170
  1. problem rombu
  2. zaprojektować przykład dziedziczenia bazowego (+diagram)
  3.  
  4. #include<string>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. class radiofm
  10. {
  11. public:
  12.         int kompresja;
  13.         int crc;
  14.         string format;
  15.  
  16.         radiofm(int kompresja, int crc, string format) : kompresja(kompresja), crc(crc), format(format)
  17.         {
  18.                 cout << "Radio FM" << endl;
  19.         }
  20.  
  21.         virtual void dekoduj()
  22.         {
  23.                 cout << "Dekodowanie radiofm" << endl;
  24.         }
  25.  
  26.         virtual void wypisz_format()
  27.         {
  28.                 cout << format << endl;
  29.         }
  30. };
  31.  
  32. class MPG
  33. {
  34. public:
  35.         int kompresja;
  36.         int crc;
  37.         string format;
  38.  
  39.         MPG(int kompresja, int crc, string format) : kompresja(kompresja), crc(crc), format(format)
  40.         {
  41.                 cout << "MPG" << endl;
  42.         }
  43.  
  44.         virtual void dekoduj()
  45.         {
  46.                 cout << "Dekodowanie MPG" << endl;
  47.         }
  48.  
  49.         virtual void wypisz_format()
  50.         {
  51.                 cout << format << endl;
  52.         }
  53. };
  54.  
  55. class dab_mp3
  56. {
  57. public:
  58.         int kompresja;
  59.         int crc;
  60.         string format;
  61.  
  62.         dab_mp3(int kompresja, int crc, string format) : kompresja(kompresja), crc(crc), format(format)
  63.         {
  64.                 cout << "dab+ MP3" << endl;
  65.         }
  66.  
  67.         virtual void dekoduj()
  68.         {
  69.                 cout << "Dekodowanie dab+ MP3" << endl;
  70.         }
  71.  
  72.         virtual void wypisz_format()
  73.         {
  74.                 cout << format << endl;
  75.         }
  76. };
  77.  
  78. class dekoder : public radiofm, public MPG, public dab_mp3
  79. {
  80. public:
  81.         dekoder(int kompresja_r, int crc_r, string format_r,
  82.                         int kompresja_m, int crc_m, string format_m) : radiofm(kompresja_r, crc_r, format_r),
  83.                 MPG(kompresja_m, crc_m, format_m),
  84.                 dab_mp3(kompresja_r, crc_r, format_r)
  85.         {
  86.         }
  87. };
  88. /*
  89. int main()
  90. {
  91.         dekoder* pd = new dekoder(10, 16, "radio FM", 20, 32, "MPG");
  92.        
  93.         MPG* pMPG = pd;
  94.         pMPG->dekoduj(); // wypisuje "Dekodowanie MPG"
  95.  
  96.         radiofm* pRFM = pd;
  97.         pRFM->wypisz_format(); // wypisuje "radioFM"
  98.  
  99.         dab_mp3* pDab = pd;
  100.         cout << pDab->kompresja << endl; // wypisuje "10"
  101.  
  102.         int i;
  103.         cin >> i;
  104. }
  105. */
  106.  
  107.  
  108.  
  109.  
  110. #include<iostream>
  111.  
  112. using namespace std;
  113.  
  114. class MPG
  115. {
  116. public:
  117.         void wypisz_format()
  118.         {
  119.                 cout << "MPG" << endl;
  120.         }
  121. };
  122.  
  123. class MPG2 : public virtual MPG
  124. {
  125. public:
  126.         void wypisz_format()
  127.         {
  128.                 cout << "MPG2" << endl;
  129.         }
  130. };
  131.  
  132. class MPG4 : public virtual MPG, public virtual MPG2
  133. {
  134. public:
  135.         void wypisz_format()
  136.         {
  137.                 cout << "MPG4" << endl;
  138.         }
  139. };
  140.  
  141. class odtwarzacz : public virtual MPG, public virtual MPG2, public MPG4
  142. {
  143. public:
  144.         void f() {}
  145. };
  146.  
  147. int main()
  148. {
  149.         odtwarzacz* o = new odtwarzacz();
  150.  
  151.         MPG* p1 = o;
  152.         p1->wypisz_format(); // Wypisuje "MPG"
  153.  
  154.         MPG2* p2 = o;
  155.         p2->wypisz_format(); // Wypisuje "MPG2"
  156.  
  157.         MPG4* p3 = o;
  158.         p3->wypisz_format(); // Wypisuje "MPG4"
  159.  
  160.  
  161.         int i;
  162.         cin >> i;
  163. }