Introduction
Inheritance is one of the core principles of object-oriented programming (OOP). It allows us to create a new class that reuses, extends, or modifies the behavior defined in another class. Single inheritance is the simplest form of inheritance where a class inherits from only one base class. In this article, we will explore how to code a C++ Program to Implement Single Inheritance. We’ll walk through the necessary prerequisites, examine various examples, and provide detailed explanations for each approach.
Prerequisites
Before diving into the code, it’s essential to have a basic understanding of:
- C++ Programming Language: Familiarity with basic syntax, classes, and objects.
- Object-Oriented Programming (OOP): Understanding of fundamental OOP concepts like inheritance, encapsulation, and polymorphism.
- Standard Library Functions: Knowledge of C++ Standard Library functions and header files.
With these prerequisites in mind, let’s explore different ways to implement single inheritance using C++.
Example 1: Basic Single Inheritance in C++
Code Explanation
In this example, we will create a simple Person
class and a derived Student
class that inherits from Person
.
Code
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
void displayPersonInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Student : public Person {
public:
int studentID;
void displayStudentInfo() {
displayPersonInfo();
cout << "Student ID: " << studentID << endl;
}
};
int main() {
Student student;
student.name = "Alice";
student.age = 20;
student.studentID = 12345;
student.displayStudentInfo();
return 0;
}
Output
Name: Alice, Age: 20
Student ID: 12345
Explanation
In this example, the Student
class inherits from the Person
class. This allows the Student
class to access the properties and methods of the Person
class. The displayStudentInfo
method in the Student
class calls the displayPersonInfo
method to display the inherited properties.
Example 2: Extending Functionality in Derived Class
Code Explanation
In this example, we will extend the functionality of the base class by adding new methods in the derived class.
Code
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
void displayPersonInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Employee : public Person {
public:
int employeeID;
double salary;
void displayEmployeeInfo() {
displayPersonInfo();
cout << "Employee ID: " << employeeID << ", Salary: " << salary << endl;
}
void setSalary(double newSalary) {
salary = newSalary;
}
};
int main() {
Employee employee;
employee.name = "Bob";
employee.age = 35;
employee.employeeID = 67890;
employee.salary = 50000.0;
employee.displayEmployeeInfo();
employee.setSalary(55000.0);
cout << "Updated Salary: " << employee.salary << endl;
return 0;
}
Output
Name: Bob, Age: 35
Employee ID: 67890, Salary: 50000
Updated Salary: 55000
Explanation
In this example, the Employee
class inherits from the Person
class and adds new properties and methods. The setSalary
method allows the salary to be updated, showcasing how the derived class can extend the functionality of the base class.
Example 3: Overriding Methods in Derived Class
Code Explanation
In this example, we will demonstrate method overriding by redefining a method in the derived class.
Code
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
virtual void displayInfo() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Teacher : public Person {
public:
string subject;
void displayInfo() override {
cout << "Name: " << name << ", Age: " << age << ", Subject: " << subject << endl;
}
};
int main() {
Teacher teacher;
teacher.name = "Charlie";
teacher.age = 45;
teacher.subject = "Mathematics";
teacher.displayInfo();
return 0;
}
Output
Name: Charlie, Age: 45, Subject: Mathematics
Explanation
In this example, the Teacher
class inherits from the Person
class and overrides the displayInfo
method. The override
keyword is used to ensure that the method in the derived class is intended to override the base class method. This demonstrates polymorphism, where the same method name can have different implementations in different classes.
Conclusion
Single inheritance in C++ is a powerful tool for creating a class hierarchy where a derived class inherits the properties and methods of a base class. Through the examples provided, we’ve seen different ways to implement single inheritance, including basic inheritance, extending functionality, and method overriding.