cpp:copy_constructors
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:copy_constructors [2017/04/20 10:13] – created gthanos | cpp:copy_constructors [2017/04/20 11:44] – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Κατασκευαστές Αντιγραφείς ====== | ====== Κατασκευαστές Αντιγραφείς ====== | ||
- | Στην ενότητα των συναρτήσεων είδαμε [[cpp: | + | Στην ενότητα των συναρτήσεων είδαμε [[cpp: |
+ | <code cpp foo.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | #include " | ||
+ | void printArea(Rectangle r) { | ||
+ | cout << " area: " << r.getArea() << endl; | ||
+ | } | ||
+ | int main() { | ||
+ | Rectangle rect(5,6); | ||
+ | printArea(rect); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Παρακάτω δίνεται το σχηματικό διάγραμμα της στοίβας της διεργασίας πριν και κατά τη διάρκεια της κλήσης της μεθόδου // | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Ορισμός ενός κατασκευαστή αντιγραφέα ===== | ||
+ | |||
+ | Ένας κατασκευαστής αντιγραφέας για την κλάση Rectangle ορίζεται ως εξής: | ||
+ | |||
+ | <code cpp> | ||
+ | Rectangle:: | ||
+ | width = r.width; height = r.height; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Συνολικά η κλάση // | ||
+ | |||
+ | <code cpp Rectangle.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | class Rectangle { | ||
+ | private: | ||
+ | int width, height; | ||
+ | public: | ||
+ | Rectangle(int w, int h); | ||
+ | Rectangle(int s); | ||
+ | Rectangle(); | ||
+ | void setWidth(int w); | ||
+ | void setHeight(int h); | ||
+ | int getWidth(); | ||
+ | int getHeight(); | ||
+ | }; | ||
+ | |||
+ | Rectangle:: | ||
+ | width = w; height = h; | ||
+ | } | ||
+ | |||
+ | Rectangle:: | ||
+ | width = s; height = s; | ||
+ | } | ||
+ | |||
+ | Rectangle:: | ||
+ | srand(time(NULL)); | ||
+ | width = rand() % 10 + 1; height = rand() % 10 + 1; | ||
+ | } | ||
+ | |||
+ | // copy constructor | ||
+ | Rectangle:: | ||
+ | width = r.width; height = r.height; | ||
+ | } | ||
+ | |||
+ | void Rectangle:: | ||
+ | void Rectangle:: | ||
+ | int Rectangle:: | ||
+ | int Rectangle:: | ||
+ | </ |
cpp/copy_constructors.txt · Last modified: 2022/05/12 19:41 by gthanos