Welcome back! Now that you have a solid understanding of classes and objects in C++, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).
Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.
In this lesson, you'll understand how to use inheritance in C++. We'll cover:
Inheritance is a way to establish a relationship between a new class (derived class) and an existing class (base class). The derived class inherits properties and behaviors (methods) from the base class.
Here’s a simple example:
C++1#include <iostream> 2#include <string> 3 4// Define the base class Person with name and age attributes 5class Person { 6public: 7 // Constructor to initialize name and age 8 Person(const std::string& name, int age) : name(name), age(age) {} 9 10 // Display method to show name and age 11 void display() const { 12 std::cout << "Name: " << name << ", Age: " << age << std::endl; 13 } 14 15 // Greet method to display a greeting message 16 void greet() const { 17 std::cout << "Hello, there!" << std::endl; 18 } 19 20private: 21 std::string name; 22 int age; 23}; 24 25// Define the derived class Student, inheriting from Person 26class Student : public Person { 27public: 28 // Constructor to initialize name, age of the base class, and the major of the student 29 Student(const std::string& name, int age, const std::string& major) 30 : Person(name, age), major(major) {} 31 32 // Display method to show name, age using the base class display method, and major of the student 33 void display() const { 34 this->greet(); 35 Person::display(); 36 std::cout << "Major: " << major << std::endl; 37 } 38 39private: 40 std::string major; 41}; 42 43int main() { 44 // Create a Student object and display its details 45 Student student("Bob", 25, "Computer Science"); 46 student.display(); 47 48 return 0; 49}
In this snippet, the Student
class inherits from the Person
class. It reuses the name
and age
attributes and methods from the Person
class and adds a new attribute major
and a new display
method to show the student's major.
When you declare a derived class, you specify the base class it inherits from. This is done using the : public BaseClass
syntax. The derived class can then extend or override the functionality of the base class.
In our example:
Person
class is the base class.Student
class is the derived class, inheriting name
and age
from the Person
class.Student
class also adds a new member, major
, and overrides the display
method to include information about the major.
Student::display()
calls Person::display()
to reuse the base class functionality before adding its own details.greet
method is also called from the display
method to show how the derived class can access base class methods using the this
pointer.Notice how Student::display()
calls Person::display()
to reuse the base class functionality before adding its own details.
Inheritance is powerful for several reasons:
Inheritance is a cornerstone of OOP, and understanding it will enable you to design more flexible and scalable applications. It's an essential concept for mastering OOP.
Excited to start practicing? Let's move on and put this theory into action!