Abstract class in C++
Level 4221 keystrokes
A pure virtual function makes Shape an abstract class; Circle implements area, and the area of a circle of radius 5 is printed as 78.5397.
C++ code
1class Shape {2public: // = 0 makes this pure virtual: Shape cannot be instantiated, and derived classes must implement area3 virtual double area() = 0;4};5 // Concrete class derived from Shape6class Circle : public Shape { // Private radius7 double r;8public: // r(r) initializes the member r with the parameter r9 Circle(double r) : r(r) {} // Implements area as πr² (π approximated by 3.14159)10 double area() override { return 3.14159 * r * r; }11};12 // The int 5 is converted to double 5.013Circle c(5);// 78.53975 is printed with cout's default 6 significant digits: 78.539714cout << c.area() << endl;Output
78.5397
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.
;× 7(× 6)× 6:× 4{× 4}× 4<× 4.× 2*× 2=× 1