C++ Tutorial – 17 – Inheritance
Articles Blog

C++ Tutorial – 17 – Inheritance

Inheritance enables a class to reuse code
from other classes. In this example we would like the class Square to inherit from Rectangle.
This is specified after the class name with a colon followed by the public keyword and
the name of the class we wish to inherit from. Rectangle now 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. Because Square is now a kind of Rectangle
it can be used anywhere a Rectangle would be expected. For example, we can pass a Square
as an argument to a function that expects a Rectangle since the Square class contains
everything in the Rectangle class. Inside the function the Square is viewed as a Rectangle
so we can here only access the Rectangle members.

One thought on “C++ Tutorial – 17 – Inheritance

  1. @DrummaBoyFB Apreciate him for his teachings! I like his teachings a lot! Thnx Programmingvideos for the awesome videos 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top