Inheritance
Inheritance is the ability to create a new class based on an existing class, starting out with the same existing properties and methods. It is generally used when it’s necessary to implement a number of different objects that are all of the same type and share several common characteristics.
Syntax
The class that inherits these properties and methods is called the derived class or sub-class. The class that the sub-class inherits from is called the base class.
class BaseClass {
accessModifier:
dataType property1;
dataType property2;
};
class SubClass: public BaseClass {
accessModifier:
dataType subProp1;
dataType subProp2;
};
Access Specifiers
public
members of the base class arepublic
in the derived class.protected
members of the base class are accessible by the derived class, but not outside the class.private
members of the base class aren’t accessible to the derived class.
Example
A good example is a Shape
class that might include:
- A
center
property with (x, y) coordinates. - A
weight
property that defines a line width. - A
color
property that would define a fill color. - A
translate
method that relocates the center property.
Some possible sub-classes derived from this base class might be:
- A
Rectangle
class which would haveheight
andwidth
properties. It could also have its ownperimeter
method. - A
Circle
class that would have aradius
property. It could also have its owncircumference
method.
Both the Circle
and Rectangle
classes would share the properties and methods of their base class, Shape
, in addition to properties and methods unique to their own class definition.
// Create the base Shape classclass Shape {public:int center[2];int weight;int color;void translate(int deltaX, int deltaY) {center[0] += deltaX;center[1] += deltaY;}};// Create sub-class Rectangleclass Rectangle: public Shape {public:int height;int width;int perimeter() {return (height + width) * 2;}};// Create sub-class Circleclass Circle: public Shape {public:int radius;float circumference() {return 2.0 * 3.1416 * float (radius);}};
All contributors
- garanews222 total contributions
- StevenSwiniarski474 total contributions
- Christine_Yang271 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.