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 revision
Previous revision
Next revisionBoth sides next revision
cpp:copy_constructors [2019/04/19 09:45] gthanoscpp:copy_constructors [2020/04/12 16:02] gthanos
Line 6: Line 6:
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 void printArea(const Rectangle r) { void printArea(const Rectangle r) {
Line 27: Line 27:
  
 <code cpp> <code cpp>
-Rectangle::Rectangle(const Rectangle &r) {+Rectangle::Rectangle(Rectangle &r) {
   width = r.width; height = r.height;   width = r.width; height = r.height;
 } }
Line 97: Line 97:
  
 <code cpp CopyRectangle.cpp> <code cpp CopyRectangle.cpp>
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 int main() { int main() {
Line 108: Line 108:
 Το παραπάνω είναι λειτουργικά ισοδύναμο με το παρακάτω. Το παραπάνω είναι λειτουργικά ισοδύναμο με το παρακάτω.
 <code cpp CopyRectangle.cpp> <code cpp CopyRectangle.cpp>
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 int main() { int main() {
Line 123: Line 123:
 Στις περιπτώσεις που υπάρχουν πεδία δείκτες που δείχνουν σε άλλα αντικείμενα (στατικά ή δυναμικά δεσμευμένα) αντιγράφονται οι διευθύνσεις αυτές, όπως θα αντιγράφονταν οποιοδήποτε άλλο πεδίο. Αυτό πρακτικά σημαίνει ότι δύο ή περισσότερα αντικείμενα δείχνουν σε μία κοινή περιοχή μνήμης. Το παραπάνω μπορεί να προκαλέσει δυσλειτουργίες, καθώς η μεταβολή του κοινού αντικειμένου επηρεάζει το σύνολο των αντικειμένων που το μοιράζονται. Στις περιπτώσεις που υπάρχουν πεδία δείκτες που δείχνουν σε άλλα αντικείμενα (στατικά ή δυναμικά δεσμευμένα) αντιγράφονται οι διευθύνσεις αυτές, όπως θα αντιγράφονταν οποιοδήποτε άλλο πεδίο. Αυτό πρακτικά σημαίνει ότι δύο ή περισσότερα αντικείμενα δείχνουν σε μία κοινή περιοχή μνήμης. Το παραπάνω μπορεί να προκαλέσει δυσλειτουργίες, καθώς η μεταβολή του κοινού αντικειμένου επηρεάζει το σύνολο των αντικειμένων που το μοιράζονται.
  
-Στο παρακάτω παράδειγμα ορίζουμε την κλάση //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 143: Line 156:
 </code> </code>
  
-Η κλάση //Rectangle// που ακολουθεί ορίζει ένα πεδίο δείκτη σε αντικείμενα τύπου //Point//. Η δημιουργία ενός αντικειμένου τύπου //Rectangle// συνεπάγεται τη δυναμική δέσμευση μνήμης για το αντικείμενο τύπου //Point// που αυτή περιέχει. Δείτε το παράδειγμα που ακολουθεί.+Η κλάση //Rectangle// που ακολουθεί ορίζει το πεδίο //origin// που είναι δείκτης σε ένα αντικείμενο τύπου //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); 
 +    }
   }   }
 } }
cpp/copy_constructors.txt · Last modified: 2022/05/12 19:41 by gthanos