Lesson 4
Inheritance in C++ Classes
Introduction

Hello again! In this part of our C++ Class Basics Revision, we delve into inheritance in object-oriented programming (OOP) with C++. Inheritance allows us to share code across classes, thus improving readability and efficiency.

In this lesson, we'll clarify attribute and method inheritance in C++ using practical examples. Our lesson's blueprint includes defining inheritance, examining attribute inheritance, exploring method inheritance, and decoding the base class constructor's usage in C++. Ready? Let's get started!

Defining Inheritance

Inheritance involves creating a derived class that inherits details from a base class. In C++, we often find scenarios where classes share common attributes or methods, which makes inheritance highly useful.

Here's an example featuring a base class named Vehicle and a derived class named Car:

C++
1#include <iostream> 2#include <string> 3 4// Define the base class 'Vehicle' 5class Vehicle { 6public: 7 // Constructor to initialize 'color' and 'brand' attributes 8 Vehicle(std::string color, std::string brand) : color(color), brand(brand) {} 9 10protected: 11 std::string color; // color of the vehicle 12 std::string brand; // brand of the vehicle 13}; 14 15// Define the derived class 'Car' 16class Car : public Vehicle { // Car inherits from Vehicle 17public: 18 // Constructor to initialize 'color', 'brand', and 'doors' 19 Car(std::string color, std::string brand, int doors) : Vehicle(color, brand), doors(doors) {} 20 21private: 22 int doors; // number of doors specific to 'Car' 23};

Inheritance types, such as Single, Multiple, Multilevel, and Hierarchical, in C++, cater to different needs. However, our focus in this lesson is primarily on single inheritance, where one base class feeds one derived class.

Attribute Inheritance

Attribute inheritance allows a derived class to inherit the attributes of a base class.

Consider this example featuring a base class named Artist, and a derived class named Musician:

C++
1#include <iostream> 2#include <string> 3 4// Define the base class 'Artist' 5class Artist { 6public: 7 // Constructor to initialize 'name' attribute 8 Artist(std::string name) : name(name) {} 9 10protected: 11 std::string name; // name of the artist 12}; 13 14// Define the derived class 'Musician' 15class Musician : public Artist { // Musician inherits from Artist 16public: 17 // Constructor to initialize 'name' and 'instrument' attributes 18 Musician(std::string name, std::string instrument) : Artist(name), instrument(instrument) {} 19 20 // Method to display the musician's details 21 void display() { 22 std::cout << "Name: " << name << "\nInstrument: " << instrument << std::endl; 23 } 24 25private: 26 std::string instrument; // instrument played by the musician 27}; 28 29int main() { 30 // Create an instance of 'Musician' and display its details 31 Musician john("John Lennon", "Guitar"); 32 john.display(); 33 return 0; 34}

The Musician class inherits the name attribute from the Artist class and also has its own unique attribute, instrument.

Method Inheritance

Similar to attributes, method or function inheritance allows a derived class to inherit the methods of a base class.

In the example below, the Car class can invoke the start method from the Vehicle class:

C++
1#include <iostream> 2#include <string> 3 4// Define the base class 'Vehicle' 5class Vehicle { 6public: 7 // Constructor to initialize 'brand' attribute 8 Vehicle(std::string brand) : brand(brand) {} 9 10 // Method to simulate starting the vehicle 11 void start() { 12 std::cout << "The " << brand << " is starting." << std::endl; 13 } 14 15private: 16 std::string brand; // brand of the vehicle 17}; 18 19// Define the derived class 'Car' 20class Car : public Vehicle { // Car inherits from Vehicle 21public: 22 // Constructor to initialize 'brand' attribute 23 Car(std::string brand) : Vehicle(brand) {} 24}; 25 26int main() { 27 // Create an instance of 'Car' and call the inherited 'start' method 28 Car my_car("BMW"); 29 my_car.start(); 30 return 0; 31}
Understanding the Base Class Constructor

In C++, the base class constructor is explicitly called within the derived class constructor using an initializer list. This approach allows a derived class to extend or utilize the functionality of a base class without directly modifying it.

For instance, when overriding a method to add or alter its behavior, calling the base class method enables integrating its functionality with new enhancements:

C++
1#include <iostream> 2#include <string> 3 4// Define the base class 'Vehicle' 5class Vehicle { 6public: 7 // Virtual method to start the vehicle (can be overridden) 8 virtual std::string start() { 9 return "Vehicle is starting..."; 10 } 11}; 12 13// Define the derived class 'Car' 14class Car : public Vehicle { // Car inherits from Vehicle 15public: 16 // Override the start method to add custom behavior 17 std::string start() override { 18 return Vehicle::start() + " Beep! Beep!"; 19 } 20}; 21 22int main() { 23 // Create an instance of 'Car' and call the overridden 'start' method 24 Car my_car; 25 std::cout << my_car.start() << std::endl; 26 return 0; 27}

Similarly, during initialization, the derived class constructor calls the base class constructor, ensuring that the base class is properly initialized, thereby allowing the derived class to add its specific attributes seamlessly:

C++
1#include <iostream> 2#include <string> 3 4// Define the base class 'ParentClass' 5class ParentClass { 6public: 7 // Constructor to initialize 'value' attribute 8 ParentClass(std::string value) : value(value) {} 9 10protected: 11 std::string value; // some value to be inherited 12}; 13 14// Define the derived class 'ChildClass' 15class ChildClass : public ParentClass { // ChildClass inherits from ParentClass 16public: 17 // Constructor to initialize 'value' and 'additional_value' 18 ChildClass(std::string value, std::string additional_value) 19 : ParentClass(value), additional_value(additional_value) {} 20 21 // Method to display the values 22 void display() { 23 std::cout << "Value: " << value << "\nAdditional Value: " << additional_value << std::endl; 24 } 25 26private: 27 std::string additional_value; // additional value specific to 'ChildClass' 28}; 29 30int main() { 31 // Create an instance of 'ChildClass' and display its values 32 ChildClass child_class("value", "additional_value"); 33 child_class.display(); 34 return 0; 35}

In these ways, calling the base class constructor facilitates a coherent and modular approach to inheritance by allowing derived classes to build upon or adapt the functionality of their base classes efficiently and cleanly.

Lesson Summary

We've successfully explored attribute and method inheritance in C++ and practiced using several examples. Mastering these concepts in real-life programming can enhance both efficiency and readability. Remember, practice is essential for proficiency!

On that note, are you ready for some practice exercises? They will solidify your understanding and prepare you for more complex programming tasks. Programming is all about experimenting, learning, and problem-solving. Enjoy the journey!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.