User Tools

Site Tools


cpp:object_creation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
cpp:object_creation [2019/04/19 08:34] – [Κύκλος ζωής των αντικειμένων - Δημιουργία και ανάθεση αντικειμένων στο heap] gthanoscpp:object_creation [2019/04/19 09:14] – [Παράδειγμα αρχικοποίησης δεικτών] gthanos
Line 157: Line 157:
   - πριν την ολοκλήρωση του προγράμματος είμαστε υποχρεωμένοι να ελευθερώσουμε τη μνήμη που δεσμεύτηκε στο //heap// κατά τη δημιουργία των αντικειμένων στα οποία δείχνουν οι δείκτες //r2, r3//    - πριν την ολοκλήρωση του προγράμματος είμαστε υποχρεωμένοι να ελευθερώσουμε τη μνήμη που δεσμεύτηκε στο //heap// κατά τη δημιουργία των αντικειμένων στα οποία δείχνουν οι δείκτες //r2, r3// 
  
-<code cpp Rectangle.cpp>+<code cpp Rectangle.hpp>
 #include <iostream> #include <iostream>
 #include <cstdlib> #include <cstdlib>
Line 164: Line 164:
 class Rectangle { class Rectangle {
   private:   private:
-    int *width, *height;+    int *width_ptr, *height_ptr;
   public:   public:
 +    Rectangle();
     Rectangle(int w, int h);     Rectangle(int w, int h);
 +    Rectangle(int s);
     ~Rectangle();     ~Rectangle();
     void setWidth(int w);     void setWidth(int w);
Line 175: Line 177:
 }; };
  
-Rectangle::Rectangle(int w, int h) { +Rectangle::Rectangle() { 
-  width = new (nothrow) int;     +  width_ptr = new (nothrow) int;     
-  height = new (nothrow) int; +  height_ptr = new (nothrow) int; 
-  if(width == NULL || height == NULL) {+  if(width_ptr == NULL || height_ptr == NULL) {
     cerr << "Memory allocation failure!\n";     cerr << "Memory allocation failure!\n";
     exit(-1);     exit(-1);
   }   }
-  *width w; *height h+  *width_ptr = *height_ptr 0
-  cout << "Constructing rectangle (w:"<< *width <<", h:"<<*height<<")\n";+  cout << "Calling 0 args constructor" << endl; 
 +
 + 
 +Rectangle::Rectangle(int w, int h) : Rectangle() { 
 +  *width_ptr = w; 
 +  *height_ptr = h; 
 +  cout << "Calling 2 args constructor" << endl; 
 +
 + 
 +Rectangle::Rectangle(int s) : Rectangle(s,s) { 
 +  cout << "Calling 1 args constructor<< endl;
 } }
  
 Rectangle::~Rectangle() { Rectangle::~Rectangle() {
-  cout << "Destructing rectangle (w:"<< *width <<", h:"<<*height<<")\n"; +  cout << "Destructing rectangle (w:"<< *width_ptr <<", h:"<<*height_ptr<<")\n"; 
-  delete width+  delete width_ptr
-  delete height;+  delete height_ptr;
 } }
  
-void Rectangle::setWidth(int w) { *width = w; } +void Rectangle::setWidth(int w) { *width_ptr = w; } 
-void Rectangle::setHeight(int h) { *height = h; } +void Rectangle::setHeight(int h) { *height_ptr = h; } 
-int Rectangle::getWidth() { return *width; } +int Rectangle::getWidth() { return *width_ptr; } 
-int Rectangle::getHeight() { return *height; } +int Rectangle::getHeight() { return *height_ptr; } 
-int Rectangle::getArea() { return *width * *height; }+int Rectangle::getArea() { return *width_ptr * *height_ptr; }
 </code> </code>
  
Line 202: Line 214:
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 int main() { int main() {
-  Rectangle rect {34};+  Rectangle rect {12};
   Rectangle *r1, *r2, *r3;   Rectangle *r1, *r2, *r3;
   r1 = &rect;   r1 = &rect;
-  r2 = new Rectangle {5, 6}; +  r2 = new Rectangle {2}; 
-  r3 = new Rectangle[2] { {4,8}, {7,3} }; +  r3 = new Rectangle[2] { {3,4}, {5} }
-  cout << "rect's getArea: " << rect.getArea() << endl; +   
-  cout << "*r1's getArea: " << r1->getArea() << endl; +  cout << "---------------" << endl; 
-  cout << "*r2's getArea: " << r2->getArea() << endl; +  Rectangle **r4; 
-  cout << "r3[0]'s getArea:" << r3[0].getArea() << endl; +  r4 = new Rectangle*[2]; 
-  cout << "r3[1]'s getArea:" << r3[1].getArea() << endl;       +  r4[0] = new Rectangle {6}; 
 +  r4[1] = new Rectangle (2,6); 
 +   
 +  cout << "---------------" << endl
 +  cout << "rect' getArea: " << rect.getArea() << endl; 
 +  cout << "*r1'  getArea: " << r1->getArea() << endl; 
 +  cout << "*r2'  getArea: " << r2->getArea() << endl; 
 +  cout << "r3[0]'s getArea: " << r3[0].getArea() << endl; 
 +  cout << "r3[1]'s getArea: " << r3[1].getArea() << endl;        
 +  cout << "r4[0]'s getArea: " << r4[0]->getArea() << endl; 
 +  cout << "r4[1]'s getArea: " << r4[1]->getArea() << endl; 
 +   
 +  cout << "---------------" << endl;
   delete r2;   delete r2;
   delete[] r3;   delete[] r3;
 +  
 +  cout << "---------------" << endl;
 +  delete r4[0];
 +  delete r4[1];
 +  delete []r4;
   return 0;   return 0;
 }  }
cpp/object_creation.txt · Last modified: 2021/05/07 06:22 (external edit)