fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Karta
  4. {
  5. private:
  6. string nazwa;
  7. int pamiec;
  8. public:
  9. Karta() :nazwa("Nvidia"),pamiec(2000){
  10. //cout << "Karta " << endl;
  11. #ifdef TESTPR
  12. cout << "Dziala konstruktor dynamiczny Karta()" << endl;
  13. #endif
  14. }
  15. ~Karta(){
  16. #ifdef TESTPR
  17. cout << "Dziala destruktor ~Karta()" << endl;
  18. #endif
  19. }
  20. void wyswietlinfo(){
  21. cout << "nazwa karty" << nazwa << endl;
  22. cout << " pamiec karty" << pamiec << endl;
  23. }
  24.  
  25. };
  26.  
  27. class Procesor{
  28. private:
  29. string marka;
  30. int czestotliwosc;
  31. public:
  32. Procesor():marka("Radeon"),czestotliwosc(1000){
  33. #ifdef TESTPR
  34. cout << "Dziala konstruktor domyslny Procesor()" << endl;
  35. #endif
  36. }
  37.  
  38. ~Procesor(){
  39. #ifdef TESTPR
  40. cout << "Dziala destruktor ~Procesor()" << endl;
  41. #endif
  42. }
  43. void infoprocesor(){
  44. cout << "marka procesora " << marka << endl;
  45. cout << " czestotliwosc procesora : " << czestotliwosc << endl;
  46. }
  47. };
  48.  
  49. class Komputer{
  50. private:
  51. string wykonawca;
  52. string producent;
  53. Procesor procesor;
  54. static int licznik;
  55. int dysk;
  56. Karta *karta;
  57. int ram;
  58.  
  59.  
  60. public:
  61. friend ostream& operator<< (ostream&,Komputer const&);
  62. Komputer(int x, int y){
  63. this->x = x; this-> y = y;
  64. }
  65. static int zwrocLicznik() { return licznik; }
  66. int x,y;
  67. Komputer () {
  68. karta = NULL;
  69.  
  70. //cout << "konstruktor" << endl;
  71. x=3;
  72. y=5;
  73. dysk=500;
  74. ram=2000;
  75. ++licznik;
  76. #ifdef TESTPR
  77. cout << "Dziala konstruktor domyslny Komputer()" << endl;
  78. #endif
  79. }
  80. void dodajkarta() {
  81. if( karta==NULL)
  82. karta= new Karta();
  83. cout << "dodana karta" << endl;
  84. }
  85.  
  86. void usunkarta() {
  87. if(karta)
  88. delete karta;
  89. cout << "usunieto karta"<< endl;
  90. }
  91.  
  92. void infokomp(){
  93. cout<< "dysk ma pojemnosc " << dysk<< endl;
  94. cout << "pamiec ram wynosci " << ram << endl;
  95. if (karta)
  96. karta->wyswietlinfo();
  97. procesor.infoprocesor();
  98. }
  99.  
  100. Komputer (const Komputer &o) {
  101. #ifdef TESTPR
  102. cout << "Dziala konstruktor kopiujacy Komputer()" << endl;
  103. #endif
  104.  
  105. producent=o.producent;
  106. wykonawca=o.wykonawca;
  107. procesor=o.procesor;
  108. dysk=o.dysk;
  109. if (o.karta != NULL)
  110. karta = new Karta(*o.karta);
  111. cout << "konstruktor kopiujacy sie wywolal " << endl;
  112. cout << "pojemnosc dysku komputera wynosi " << dysk << endl;
  113.  
  114. }
  115.  
  116. ~Komputer () {
  117. --licznik;
  118. #ifdef TESTPR
  119. cout << "Dziala desturktor ~Komputer()" << endl;
  120. #endif
  121.  
  122. }
  123.  
  124. bool operator==(const Komputer &k){ return producent == k.producent; }
  125.  
  126. void setProducent(string producent){ this->producent = producent; }
  127.  
  128. Komputer& operator=(const Komputer &p){
  129. cout << "operator przypisania" << endl;
  130. dysk=p.dysk;
  131. ram=p.ram;
  132. if (p.karta != NULL)
  133. {
  134. if (karta == NULL) karta = new Karta;
  135. *karta = *p.karta;
  136. }
  137. return *this;
  138. }
  139.  
  140. Komputer& operator+(Komputer &m){
  141. cout << "operator + " << endl;
  142. Komputer temp;
  143. temp.x = x+m.x;
  144. temp.y = y+m.y;
  145. return temp;
  146. }
  147.  
  148.  
  149.  
  150. };
  151. int Komputer::licznik = 0;
  152.  
  153.  
  154. int main() {
  155. cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
  156. Komputer k1;
  157. cout << "Jest " << Komputer::zwrocLicznik() << " komputer" << endl;
  158. Komputer k2;
  159. cout << "Sa " << Komputer::zwrocLicznik() << " komputery" << endl;
  160. Komputer *k3;
  161. cout << "Jestesmy po deklaracjach/definicjach" << endl;
  162. k3 = new Komputer();
  163. cout << "Sa " << Komputer::zwrocLicznik() << " komputery" << endl;
  164. cout << (k1 == k2) << endl;
  165. cout << "procesory sa takie same wiec zwrocilo 1" << endl;
  166. k2.setProducent("Intel");
  167. cout << (k1.operator==(k2)) << endl;
  168. cout << "procesory nie sa takie same wiec zwrocilo 0" << endl;
  169. cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
  170. delete k3;
  171. cout << "Jest " << Komputer::zwrocLicznik() << " komputerow bo usunelam jeden hah" << endl;
  172. Komputer k5;
  173. cout << "Jest " << Komputer::zwrocLicznik() << " komputerow" << endl;
  174.  
  175. Komputer k6(k5); // konstruktor kopiujacy
  176. Komputer k7;
  177. Komputer k8;
  178. k7 = k8;
  179. k7=k2+k1;
  180. k2.dodajkarta();
  181. k2.usunkarta();
  182. k1.infokomp();
  183.  
  184.  
  185. return 0;
  186. return 0;
  187. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Jest 0 komputerow
Jest 1 komputer
Sa 2 komputery
Jestesmy po deklaracjach/definicjach
Sa 3 komputery
1
procesory sa takie same wiec zwrocilo 1
0
procesory nie sa takie same wiec zwrocilo 0
Jest 3 komputerow
Jest 2 komputerow bo usunelam jeden hah
Jest 3 komputerow
konstruktor kopiujacy sie wywolal 
pojemnosc dysku komputera wynosi 500
operator przypisania
operator + 
operator przypisania
dodana karta
usunieto karta
dysk ma pojemnosc  500
pamiec ram wynosci 2000
marka procesora Radeon
 czestotliwosc procesora : 1000