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 revision
Previous revision
Next revisionBoth sides next revision
cpp:object_creation [2017/04/20 09:52] – [Χρόνος ζωής των αντικειμένων - Δημιουργία και ανάθεση αντικειμένων στο heap] gthanoscpp:object_creation [2017/05/10 04:49] – [Δημιουργία αντικειμένων] gthanos
Line 2: Line 2:
  
 Η δημιουργία αντικειμένων γίνεται με κλήση του αντίστοιχου κατασκευαστή ως εξής: Η δημιουργία αντικειμένων γίνεται με κλήση του αντίστοιχου κατασκευαστή ως εξής:
-<code c++> +<code c++ Rectangle.cpp
-Rectangle rect1(3,4); +#include <iostream> 
-Rectangle rect2(5); +using namespace std; 
-Rectangle rect3;+ 
 +class Rectangle { 
 +  private: 
 +    int width, height; 
 +  public: 
 +    Rectangle(int w, int h); 
 +    Rectangle(int s); 
 +    Rectangle(); 
 +    int getWidth() const; 
 +    int getHeight() const; 
 +    void setWidth(int w); 
 +    void setHeight(int h); 
 +}; 
 + 
 +Rectangle::Rectangle(int w, int h): width(w), height(h) {} 
 +Rectangle::Rectangle(int s) : width(s), height(s) {} 
 +Rectangle::Rectangle(): width(0), height(0) {} 
 + 
 +int Rectangle::getWidth() const { return width;} 
 +int Rectangle::getHeight() const { return height;} 
 +void Rectangle::setWidth(int w) { width = w; } 
 +void Rectangle::setHeight(int h) { height = h; } 
 + 
 +int main() { 
 +  Rectangle rect1(3,4); 
 +  Rectangle rect2(5); 
 +  Rectangle rect3; 
 +}
 </code> </code>
  
-Ο παραπάνω κώδικας δημιουργεί 3 αντικείμενα της κλάσης //Rectangle// το αντικείμενο //rect1// με πλευρές 3 και 4, το //rect2// με πλευρές κοινού μήκους 5 και το αντικείμενο //rect3// που λαμβάνει τυχαίες τιμές. +Ο παραπάνω κώδικας δημιουργεί 3 αντικείμενα της κλάσης //Rectangle// το αντικείμενο //rect1// με πλευρές 3 και 4, το //rect2// με πλευρές κοινού μήκους 5 και το αντικείμενο //rect3// που έχει μηδενικές τιμές. 
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
Line 151: Line 178:
 }  }
 </code> </code>
-===== Κατασκευαστές αντιγραφείς =====+
  
cpp/object_creation.txt · Last modified: 2021/05/07 06:22 (external edit)