Constructor in C++
Level 4190 keystrokes
Defines a Rectangle class whose constructor sets private members with an initializer list, then prints the area 15.
C++ code
1class Rectangle {// Members below are hidden from code outside the class2private: // Two int members declared in one line3 int width, height;4public: // Constructor: the initializer list sets width and height before the (empty) body runs5 Rectangle(int w, int h) : width(w), height(h) {}6 // Return the area computed from the private members7 int area() { return width * height; }8};9 // Call the constructor with w = 5 and h = 310Rectangle r(5, 3);// Print 5 * 3 = 1511cout << r.area() << endl;Output
15
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
(× 6)× 6;× 5,× 4<× 4:× 3{× 3}× 3.× 1*× 1