Advertisement
MeehoweCK

Untitled

Jun 27th, 2022
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. #include "Bazowa.h"
  4. #include "Pochodna.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     Bazowa* tab[2];
  11.     tab[0] = new Bazowa(1, 2);
  12.     tab[1] = new Pochodna(3, "tekst");
  13.     for(int i = 0; i < 2; ++i)
  14.     {
  15.         tab[i]->opis();
  16.         delete tab[i];
  17.     }
  18.     return 0;
  19. }
  20.  
  21. // Bazowa.h
  22. #ifndef BAZOWA_H
  23. #define BAZOWA_H
  24. #include <iostream>
  25.  
  26. using namespace std;
  27.  
  28. class Bazowa
  29. {
  30.     public:
  31.         Bazowa(int a, int b) : wartosc1(a), wartosc2(b) {}
  32.         Bazowa(int a) : wartosc2(a) {}
  33.         virtual ~Bazowa() {}
  34.         virtual void opis() const;
  35.     private:
  36.         int wartosc1;
  37.     protected:
  38.         int wartosc2;
  39. };
  40.  
  41. void Bazowa::opis() const
  42. {
  43.     cout << "Obiekt klasy bazowa posiada dwie wartosci: " << wartosc1 << " i " << wartosc2 << endl;
  44. }
  45.  
  46. #endif // BAZOWA_H
  47.  
  48. // Pochodna.h
  49. #ifndef POCHODNA_H
  50. #define POCHODNA_H
  51. #include <iostream>
  52. #include "Bazowa.h"
  53.  
  54. using namespace std;
  55.  
  56. class Pochodna : public Bazowa
  57. {
  58.     public:
  59.         Pochodna(int a, string m_tekst) : Bazowa(a), tekst(m_tekst) {}
  60.         void opis() const;
  61.         ~Pochodna() {}
  62.     private:
  63.         string tekst;
  64. };
  65.  
  66. void Pochodna::opis() const
  67. {
  68.     cout << "Obiekt klasy pochodna posiada wartosc " << wartosc2 << " oraz tekst " << tekst << endl;
  69. }
  70.  
  71. #endif // POCHODNA_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement