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 10:08] – [Κατασκευαστές αντιγραφείς] gthanoscpp:object_creation [2017/05/10 04:47] – [Δημιουργία αντικειμένων] 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: 
 +    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) {} 
 + 
 +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>
  
cpp/object_creation.txt · Last modified: 2021/05/07 06:22 (external edit)