Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cassert>
  4. #include <cstring>
  5.  
  6. class Utwor{
  7. int rok_powstania;
  8. public:
  9. Utwor(int);
  10. virtual ~Utwor();
  11. int rok();
  12. virtual void drukuj();
  13. };
  14.  
  15. class Utwor_muzyczny : public Utwor{
  16. char *tytul;
  17. public:
  18. Utwor_muzyczny(char*,int);
  19. ~Utwor_muzyczny();
  20. Utwor_muzyczny &operator=(Utwor &);
  21. void drukuj();
  22. };
  23.  
  24. Utwor::Utwor(int r)
  25. {
  26. rok_powstania=r;
  27. }
  28. Utwor::~Utwor()
  29. {
  30. std::cout<<"Wyowało destruktor klasy bazowej"<<std::endl;
  31. }
  32. int Utwor::rok()
  33. {
  34. return rok_powstania;
  35. }
  36. void Utwor::drukuj(){
  37. std::cout<<"Rok powstania to: "<<rok_powstania<<std::endl;
  38. }
  39.  
  40. Utwor_muzyczny::Utwor_muzyczny(char *tyt,int r):Utwor(r){
  41.  
  42. int dlugosc=strlen(tyt);
  43. tytul=new char[dlugosc+1];
  44. assert(tytul);
  45. strcpy(tytul,tyt);
  46. }
  47.  
  48. Utwor_muzyczny::~Utwor_muzyczny(){
  49. std::cout<<"Wyowlano destruktor klasy pochodnej "<<std::endl;
  50. delete [] tytul;
  51. }
  52. void Utwor_muzyczny::drukuj(){
  53. std::cout<<"Rok powstania to: "<<rok()<<" tytul to: "<<tytul<<std::endl;
  54. }
  55. Utwor_muzyczny & Utwor_muzyczny::operator=(Utwor &muzyczny)
  56. {
  57. if (this==&muzyczny)
  58. return *this;
  59. Utwor *wsk;
  60. wsk=&muzyczny;
  61. rok_powstania=wsk->rok();
  62. delete [] tytul;
  63. int dlugosc=strlen(muzyczny.tytul);
  64. tytul=new char[dlugosc+1];
  65. assert(tytul);
  66. strcpy(tytul,muzyczny.tytul);
  67.  
  68. }
  69. int main()
  70. {
  71. Utwor utwor(1998);
  72. Utwor_muzyczny muzyczny1("Fajny",2012);
  73. Utwor_muzyczny muzyczny2 ("Siemka",1992);
  74. Utwor *wsk;
  75. wsk=&utwor;
  76. wsk->drukuj();
  77. wsk=&muzyczny1;
  78. wsk->drukuj();
  79.  
  80.  
  81.  
  82.  
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement