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 [2020/04/08 14:30] – ρε gthanoscpp:object_creation [2020/04/08 14:34] gthanos
Line 20: Line 20:
 }; };
  
-Rectangle::Rectangle(int w, int h): width(w), height(h) {} +Rectangle::Rectangle(int w, int h): width(w), height(h) { 
-Rectangle::Rectangle(int s) : width(s), height(s) {} +  std::cout << "Constructor with 2 parameters!\n"; 
-Rectangle::Rectangle(): width(0), height(0) {}+
 +Rectangle::Rectangle(int s) : width(s), height(s) { 
 +  std::cout << "Constructor with 1 parameter!\n"; 
 +
 +Rectangle::Rectangle(): width(0), height(0) { 
 +  std::cout << "Constructor with 0 parameters!\n"; 
 +}
  
 Rectangle::~Rectangle() { Rectangle::~Rectangle() {
Line 32: Line 38:
 void Rectangle::setWidth(int w) { width = w; } void Rectangle::setWidth(int w) { width = w; }
 void Rectangle::setHeight(int h) { height = h; } void Rectangle::setHeight(int h) { height = h; }
- 
 </code> </code>
  
Line 107: Line 112:
 Τα παραδείγματα που είδαμε μέχρι τώρα δημιουργούν αντικείμενα μέσα στο //stack// της διεργασίας. Εάν θέλουμε να δημιουργήσουμε αντικείμενα τα οποία είναι αποθηκευμένα στο //heap//, δηλαδή δεσμεύεται δυναμικά μνήμη για αυτά, θα πρέπει να το κάνουμε μέσω του τελεστή [[cpp:dynamic_memory|**new** που είδαμε προηγούμενα]] ως εξής: Τα παραδείγματα που είδαμε μέχρι τώρα δημιουργούν αντικείμενα μέσα στο //stack// της διεργασίας. Εάν θέλουμε να δημιουργήσουμε αντικείμενα τα οποία είναι αποθηκευμένα στο //heap//, δηλαδή δεσμεύεται δυναμικά μνήμη για αυτά, θα πρέπει να το κάνουμε μέσω του τελεστή [[cpp:dynamic_memory|**new** που είδαμε προηγούμενα]] ως εξής:
  
-<code cpp RectangleInitialization.cpp>+<code cpp RectangleInHeap.cpp> 
 +#include "Rectangle.hpp" 
 int main () { int main () {
   Rectangle *rect;         Rectangle *rect;      
cpp/object_creation.txt · Last modified: 2021/05/07 06:22 (external edit)