Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. 1.
  2. class Samolot { public:  float paliwo;  float dystans;  float x, y, z;  float wysokosc; };
  3.  
  4. int main(int argc, char *argv[]) {
  5.  
  6.  
  7.  Samolot s1;  s1.dystans = 10;  zad5(s1);  Samolot s2 = s1;  zad5(s2);  Samolot s3(s2);  zad5(s3);  Samolot *s4 = new Samolot;  (*s4).dystans = 15;  zad5(*s4); (*s4).~Samolot();  return 0; }
  8.  
  9. Z powyższego kodu wynika:
  10. - konstruktor bezargumentowy utworzyl sie sam - konstruktor wieloargumentowy nie utworzyl sie sam - konstruktor kopiujacy utworzyl sie sam - operator przypisania utworzyl sie sam - destruktor utworzyl sie sam
  11.  
  12. 2.  
  13. Samolot(float paliwo, float dystans, float x, float y, float z, float wysokosc) {   this->paliwo = paliwo;   this->dystans = dystans;   this->x = x;   this->y = y;   this->z = z;   this->wysokosc = wysokosc;  }
  14. - konstruktur bezargumentowy nie utworzyl sie sam - tworzy sie wtedy, gdy nie jest zdefiniowany zaden konstuktor
  15.  
  16.  
  17.  
  18.  
  19.  
  20. 3.
  21. Samolot() = default
  22. Reszta zadania została wykonana już wcześniej (poprzednie punkty)
  23. - konstruktor kopiujacy utworzyl sie sam - konstruktor przenoszacy nie utworzyl sie sam - operator przypisania utworzyl sie sam - destruktor utworzyl sie sam
  24.  
  25. 4.
  26. Już w punkcie 1 zostało pokazane istnienie destruktora wygenerowanego automatycznie
  27. 5.  
  28.  
  29. void zad5(Samolot samolot) {  std::cout << "Dystans: " << samolot.dystans << std::endl;  std::cout << "Paliwo: " << samolot.paliwo << std::endl;  std::cout << "wysokosc: " << samolot.wysokosc << std::endl;  std::cout << std::endl; }
  30.  
  31. Zaimplementowanie funkcji nie zmieniło stanu z poprzednich zadań.
  32. 6.  
  33. Samolot zad6(Samolot samolot, float korekta) {  Samolot s = samolot;  s.wysokosc -= korekta;  return s; }
  34. Zaimplementowanie tej funkcji również nie zmieniło stanu z poprzednich zadań
  35. 7.
  36. int main(int argc, char *argv[]) {
  37.  
  38.  
  39.  Samolot s1(10, 20, 30, 40, 50, 60);  Samolot s2;  s1 = s2;  zad5(s1);  zad5(s2);
  40.  
  41.  return 0; }
  42. kod ten wyrzuca błąd – uninitialized local variable ‘s2’ used. Natomiast po zaimplementoaniu (przeciążeniu domyślnego) operatora przypisania:
  43.  
  44.  
  45.  
  46. Samolot &operator=(const Samolot &s) {   if (this != &s) {
  47.    this->paliwo = s.paliwo;    this->dystans = s.dystans;    this->x = s.x;    this->y = s.y;    this->z = s.z;    this->wysokosc = s.wysokosc;   }   else     return *this; }
  48.  
  49. Atrybuty obiektu s1 przyjmuja śmieciowe wartości (takie same jak obiektu s2)
  50.  
  51. 8.
  52. class Samolot { public:  float paliwo;  float dystans;  float wysokosc; private:  float x;  float y;  float z; public:  inline float getX() {   return x;  }  inline float getY() {   return y;  }  inline float getZ() {   return z;  }  Samolot(float paliwo, float dystans, float x, float y, float z, float wysokosc);  Samolot() = default;  Samolot &operator=(const Samolot &s); };
  53.  
  54. int main(int argc, char *argv[]) {
  55.  
  56.  
  57.  Samolot s1(10, 20, 30, 40, 50, 60);  Samolot s2 = s1;  std::cout << s2.getX() << ", " << s2.getY() << ", " << s2.getZ() << std::endl;
  58.  
  59.  return 0; }
  60. Atrybuty x,y,z obiektu s2 przyjely wartości odpowiednio 30, 40 i 50.  
  61.  
  62.  
  63.  
  64.  
  65. 9.
  66. class Samolot { public:  float paliwo;  float dystans;  float wysokosc;  float x;  float y;  float z;  char *sygnatura;
  67.  
  68.  Samolot(float paliwo, float dystans, float x, float y, float z, float wysokosc, char *syg);  Samolot() = default; };
  69. Samolot::Samolot(float paliwo, float dystans, float x, float y, float z, float wysokosc, char *syg) {  this->paliwo = paliwo;  this->dystans = dystans;  this->x = x;  this->y = y;  this->z = z;  this->wysokosc = wysokosc;  sygnatura = new char[strlen(syg) + 1];  strcpy_s(sygnatura, strlen(syg) + 1, syg); }
  70.  
  71. Przetestowane dla:
  72. int main(int argc, char *argv[]) {
  73.  
  74.  
  75.  Samolot s1(10, 20, 30, 40, 50, 60, "test");  Samolot s2;  s2 = s1;  Samolot s3(s2);  Samolot *s4 = new Samolot;  (*s4).~Samolot();
  76.  
  77.  return 0; }
  78. Widzimy, że nic się nie zmieniło względem punktu 1.
  79.  
  80. 10.
  81. Po dodaniu stałego pola w klasie, nic się nie zmienia poza tym, że nie generuje się domyślny operator przypisania.
  82.  
  83.  
  84.  
  85.  
  86.  
  87. 11.
  88. class Silnik { public:  bool stan;  int szybkosc;  int identyfikator; private:  Silnik &operator=(const Silnik &sil); };
  89.  
  90. Silnik &Silnik::operator=(const Silnik &sil) {  if (this != &sil) {   this->stan = sil.stan;   this->szybkosc = sil.szybkosc;   this->identyfikator = sil.identyfikator;  }  else   return *this; }
  91. Konstruktor bezargumentowy generuje się automatycznie, kopiujący tak samo. Destruktor również wygenerował się automatycznie, natomiast jako, że przeciążyliśmy domyślny operator przypisania, ale jest on prywatny, nie mamy możliwości jego użycia (poza klasa).
  92. 12.
  93. class Samolot { public:  float paliwo;  float dystans;  float wysokosc;  float x;  float y;  float z;  char *sygnatura;  const int masa = 100;  Silnik psilnik; };
  94.  
  95. Po dodaniu obiektu klasy Silnik do klasy Samolot, konstruktor kopiujący w dalszym ciągu generuje się automatycznie, podobnie konstruktor bezargumentowy, oraz destruktor.
  96. W dalszym ciągu konstruktor wieloargumentowy nie generuje się automatycznie, tak jak operator przypisania
  97.  
  98. 13.
  99. class Radio { public:  bool stan;  int kanal; };
  100.  
  101.  
  102.  
  103. 14.
  104. class Samolot { public:  float paliwo;  float dystans;  float wysokosc;  float x;  float y;  float z;  char *sygnatura;  Radio &radio; };
  105. Po dodaniu Radio &radio przestając być generowane wszelkie konstruktory, oraz operator przypisania. Przekazując przez referencje, przekazywany obiekt musi być zainizjalizowany!
  106.  
  107.  
  108.  
  109. 15.
  110. Operator przeniesienia, oraz konstruktor przenoszący są generowane automatycznie, do czasu, gdy nie zostanie zaimplementowane jedno z: operator przeniesienia, konstruktor przenoszący, konstruktor kopiujący, destruktor lub operator przypisania.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement