// Intro. to Inheritance // * Probably the most powerful feature of OOP // * base class --> derived class // * The derived class inherits all the capablities of the base class // but can override received ones and // add other features. #include class Circle { private: double radius; public: Circle(double r) { radius = r; } double getR() { return radius; } double area(void) { return 3.14*radius*radius; } }; class Cylinder : public Circle { private: double len; public: Cylinder(double l, double r) : Circle(r) { len = l; } double getL(void) { return len; } double volume(void) { return area()*len; } }; void main() { Circle c1(1), c2(2); cout << "Circle 1: " << "r=" << c1.getR() << " Area=" << c1.area() << endl; cout << "Circle 2: " << "r=" << c2.getR() << " Area=" << c2.area() << endl; Cylinder cy1(1,1), cy2(2,2); cout << "Cylinder 1: " << "r=" << cy1.getR() << ",L=" << cy1.getL() << " Vol=" << cy1.volume() << endl; cout << "Cylinder 2: " << "r=" << cy2.getR() << ",L=" << cy2.getL() << " Vol=" << cy2.volume() << endl; }