CHAPTER 14

image

Inheritance

Inheritance allows a class to acquire the members of another class. In the example below, Square inherits from Rectangle. This is specified after the class name by using a colon followed by the public keyword, and the name of the class to inherit from. Rectangle then becomes a base class of Square, which in turn becomes a derived class of Rectangle. In addition to its own members, Square gains all accessible members in Rectangle, except for its constructors and destructor.

class Rectangle{ public:  int x, y;  int getArea() { return x * y; }};class Square : public Rectangle {};

Upcasting

An object can be upcast to its ...

Get C++ Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.