Advertisement
Loony04

[PSIO] Dziedziczenie złożone

Nov 30th, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class LUDZIE {
  6. protected:
  7.         int ilosc;
  8.         string *imiona;
  9. public:
  10.         LUDZIE(int _ilosc): ilosc(_ilosc) {
  11.                 imiona = new string[ilosc];
  12.  
  13.                 if(imiona == NULL) {
  14.                         cerr << "brak pamieci";
  15.                         exit(1);
  16.                 }
  17.         }
  18.  
  19.         void dopiszKlan(string nazwa) {
  20.                 for(int i=0; i<ilosc; i++) {
  21.                         imiona[i] += "(" + nazwa + ")";
  22.                 }
  23.         }
  24.  
  25.         ~LUDZIE() {
  26.                 delete[] imiona;
  27.         }
  28. };
  29.  
  30. class KUZNIA {
  31. protected:
  32.         int ilosc;
  33.         string *arsenal;
  34. public:
  35.         KUZNIA(int _ilosc): ilosc(_ilosc) {
  36.                 arsenal = new string[ilosc];
  37.  
  38.                 if(arsenal == NULL) {
  39.                         cerr << "brak pamieci";
  40.                         exit(1);
  41.                 }
  42.         }
  43.  
  44.         ~KUZNIA() {
  45.                 delete[] arsenal;
  46.         }
  47. };
  48.  
  49. class RYCERZE: public LUDZIE, public KUZNIA {
  50. private:
  51.         string nazwa_armii;
  52. public:
  53.         RYCERZE(int ludzie, int kuznie, string nazwa): LUDZIE(ludzie), KUZNIA(kuznie) {
  54.                 nazwa_armii = nazwa;
  55.         }
  56.         friend istream& operator >> (istream&, RYCERZE&);
  57.         void operator ~ () {
  58.                 cout << "Imiona = [ ";
  59.                 for(int i=0; i<LUDZIE::ilosc; i++) {
  60.                         cout << LUDZIE::imiona[i] << ", ";
  61.                 }
  62.                 cout << "]\n";
  63.  
  64.                 cout << "Arsenal = [ ";
  65.                 for(int i=0; i<KUZNIA::ilosc; i++) {
  66.                         cout << KUZNIA::arsenal[i] << ", ";
  67.                 }
  68.                 cout << "]\n";
  69.         }
  70.         void Przygotuj() {
  71.                 srand(time(NULL));
  72.  
  73.                 cout << "\nWynik metody Przygotuj():\n";
  74.                 for(int i=0; i<LUDZIE::ilosc; i++) {
  75.                         cout << LUDZIE::imiona[i] << " -> " << KUZNIA::arsenal[rand()%KUZNIA::ilosc] << endl;
  76.                 }
  77.         }
  78. };
  79.  
  80. int main()
  81. {
  82.         RYCERZE *rycerze = new RYCERZE(5, 3, "mala armia");
  83.         string nazwaKlanu;
  84.  
  85.         cout << "Podaj nazwe klanu: ";
  86.         cin >> nazwaKlanu;
  87.         cin >> *rycerze;
  88.  
  89.         rycerze->dopiszKlan(nazwaKlanu);
  90.         ~*rycerze;
  91.         rycerze->Przygotuj();
  92.  
  93.         //cin.get();
  94.         //cin.get();
  95.         system("PAUSE");
  96.         return 0;
  97. }
  98.  
  99. istream& operator >> (istream &str, RYCERZE &r) {
  100.         for(int i=0; i<r.LUDZIE::ilosc; i++) {
  101.                 cout << "Podaj imie[" << i << "]: ";
  102.                 cin >> r.LUDZIE::imiona[i];
  103.         }
  104.  
  105.         for(int i=0; i<r.KUZNIA::ilosc; i++) {
  106.                 cout << "Podaj nazwe broni[" << i << "]: ";
  107.                 cin >> r.KUZNIA::arsenal[i];
  108.         }
  109.         return str;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement