cpp:classes
This is an old revision of the document!
Κλάσεις και Αντικείμενα
Όπως και στη Java οι κλάσεις στη C++ δημιουργούν νέους τύπους δεδομένων. Παρακάτω δίνεται το παράδειγμα της κλάσης Rectangle με παράλληλη δημιουργία ενός αντικειμένου της κλάσης αυτής.
- Rectangle.cpp
#include <iostream> using namespace std; class Rectangle { private: int width, height; public: void setWidth(int w); void setHeight(int h); int getWidth(); int getHeight(); }; void Rectangle::setWidth(int w) { width = w; } void Rectangle::setHeight(int h) { height = h; } int Rectangle::getWidth() { return width; } int Rectangle::getHeight() { return height; } int main () { Rectangle rect; rect.setWidth(5); rect.setHeight(6); cout << "area: " << rect.getWidth() * rect.getHeight() << endl; return 0; }
Η παραπάνω κλάση διαθέτει τα πεδία τύπου int width, height και τις μεθόδους
cpp/classes.1491983758.txt.gz · Last modified: 2017/04/12 06:55 (external edit)