User Tools

Site Tools


cpp:copy_constructors

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:copy_constructors [2019/04/19 09:45] gthanoscpp:copy_constructors [2019/04/19 09:59] – [Μια πιο σύνθετη περίπτωση] gthanos
Line 125: Line 125:
 Στο παρακάτω παράδειγμα ορίζουμε την κλάση //Point// η οποία αντιπροσωπεύει ένα σημείο στο διδιάστατο χώρο. Στο παρακάτω παράδειγμα ορίζουμε την κλάση //Point// η οποία αντιπροσωπεύει ένα σημείο στο διδιάστατο χώρο.
  
-<code cpp Point.cpp>+<code cpp Point.hpp>
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 132: Line 132:
     int x, y;     int x, y;
   public:   public:
-    Point(int vx,int vy) { x = vx; y = vy; cout << "Point regular constructor!\n";+    Point(int vx,int vy) {  
-    Point(const Point &p) { x = p.x; y = p.y; cout << "Point copy constructor!\n";      +      x = vx; y = vy;  
-    Point() { cout << "Point default constructor!\n";+      cout << "Point regular constructor!\n"; 
-    ~Point() { cout << "xP Point destructor!\n"; }+    } 
 +     
 +    Point(const Point &p) {  
 +      x = p.x; y = p.y;  
 +      cout << "Point copy constructor!\n"; 
 +    } 
 +     
 +    Point() {  
 +      cout << "Point default constructor!\n"; 
 +    
 +    ~Point() {  
 +      cout << "xP Point destructor!\n";  
 +    } 
 +    
     void setX(int vx) { x = vx; }     void setX(int vx) { x = vx; }
     void setY(int vy) { y = vy; }     void setY(int vy) { y = vy; }
Line 145: Line 158:
 Η κλάση //Rectangle// που ακολουθεί ορίζει ένα πεδίο δείκτη σε αντικείμενα τύπου //Point//. Η δημιουργία ενός αντικειμένου τύπου //Rectangle// συνεπάγεται τη δυναμική δέσμευση μνήμης για το αντικείμενο τύπου //Point// που αυτή περιέχει. Δείτε το παράδειγμα που ακολουθεί. Η κλάση //Rectangle// που ακολουθεί ορίζει ένα πεδίο δείκτη σε αντικείμενα τύπου //Point//. Η δημιουργία ενός αντικειμένου τύπου //Rectangle// συνεπάγεται τη δυναμική δέσμευση μνήμης για το αντικείμενο τύπου //Point// που αυτή περιέχει. Δείτε το παράδειγμα που ακολουθεί.
  
-<code cpp Rectangle.cpp>+<code cpp Rectangle.hpp>
 #include <iostream> #include <iostream>
 #include <cstdlib> #include <cstdlib>
Line 151: Line 164:
 using namespace std; using namespace std;
  
-#include "Point.cpp"+#include "Point.hpp"
  
 class Rectangle { class Rectangle {
Line 162: Line 175:
     Rectangle();     Rectangle();
     ~Rectangle();     ~Rectangle();
 +    Rectangle(Rectangle &r);
     void setWidth(int w);     void setWidth(int w);
     void setHeight(int h);     void setHeight(int h);
Line 177: Line 191:
     exit(-1);     exit(-1);
   }   }
 +  cout << "Calling 2 args constructor" << endl;
 } }
  
-Rectangle::Rectangle(int s, Point p) +Rectangle::Rectangle(int s, Point p) : Rectangle(s,s,p) { 
-  width = s; height = s+  cout << "Calling 1 args constructor<< endl;
-  origin = new (nothrow) Point( p.getX(), p.getY(); +
-  if(origin == NULL) { +
-    cerr << "Memory allocation failure!\n"; +
-    exit(-1); +
-  }+
 } }
  
-Rectangle::Rectangle() +Rectangle::Rectangle() : Rectangle(0,Point()) { 
-  srand(time(NULL))+  cout << "Calling 0 args constructor<< endl;
-  width = rand() % 10 + 1; height = rand() % 10 + 1; +
-  origin = new (nothrow) Point{}; +
-  if(origin == NULL) { +
-    cerr << "Memory allocation failure!\n"; +
-    exit(-1); +
-  }+
 } }
  
Line 211: Line 215:
  
 <code cpp MoveOrigin.cpp> <code cpp MoveOrigin.cpp>
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 int moveOrigin(Rectangle &r, int dx, int dy) { int moveOrigin(Rectangle &r, int dx, int dy) {
Line 246: Line 250:
   width = r.width;   width = r.width;
   height = r.height;   height = r.height;
-  origin = new (nothrow) Point(r.getOrigin()->getX(), r.getOrigin()->getY()); +  if(r.origin == nullptr) 
-  if(origin == NULL) { +    origin = nullptr; 
-    cerr << "Memory allocation failure!"; +  else { 
-    exit(-1);+    origin = new (nothrow) Point(r.getOrigin()->getX(), r.getOrigin()->getY()); 
 +    if(origin == NULL) { 
 +      cerr << "Memory allocation failure!"; 
 +      exit(-1); 
 +    }
   }   }
-} 
 </code>  </code> 
  
cpp/copy_constructors.txt · Last modified: 2022/05/12 19:41 by gthanos