Base Class and Derived Class in C++
In the C++ object-oriented programming language (OOPS), inheritance is a unique concept in the class explanation of multiple base classes derived from a specific class. It allows a user-defined subclass (derived class) to adopt or inherit the default properties and behavior (e.g., attributes and class methods of the user-defined class), functions, and features of another main root class (base class). The inheritance concept helps C++ users reuse existing program source code multiple times and creates a natural hierarchy between classes derived from a base class. The user-defined root class typically represents a general root class concept, while the derived subclass demonstrates a more extended root class concept.

Base class, derived class, and special features of inheritance.
- Base Class – A user-defined main root or super base class in a C++ program that represents the common properties and behavior (class-declared parameters, members, and function methods) of the root class.
- Derived Class – A user-defined derived or subclass that inherits from the default functions and methods of the base or main root class and can add its own properties or behavior, or override base class methods if needed.
- Inheritance – Inheritance is a concept or mechanism in C++ programs that provides a derived class with access and control over the public and protected class members and data type parameters of the base class.
Syntax of BaseClass and DerivedClass in inheritance.
class BaseClass {
//User-defined variables or members of the base class
};
class DerivedClass : public BaseClass {
//User-defined members of the derived class parameter
};
In the above BaseClass and DerivedClass inheritance syntax,
- In this syntax, a base class is a user-defined root or superclass.
- Similarly, a derived class is a user-defined subclass.
- Here, public in the “DerivedClass” class means that the public class data type member variables of the user-defined root or BaseClass can be accessed and controlled in the DerivedClass or subclass. This can also be protected or private.
Types of Inheritance in C++ Programming.
- Single inheritance – In a C++ program, single inheritance extends or defines a class derived from a single root or base class.
- Multiple inheritance – In a C++ program, multiple inheritance occurs when a class is derived from and connected to more than one base or superclass.
- Multilevel inheritance – In a C++ program, multilevel inheritance occurs when one class is derived from another class, which in turn is derived from or connected to a third class.
- Hierarchical inheritance – In a C++ program, hierarchical inheritance occurs when multiple classes are derived and connected to a single base root class.
- Hybrid inheritance – In a C++ program, hybrid inheritance is a unique combination of two or more types of class inheritance.
Example of single inheritance in a C++ program.
So, let’s look at an example of single inheritance in a C++ program, representing a single inheritance, in which a derived class inherits from a base root class.
#include <iostream>
#include <string>
class Employee { // here we define an employee name (root or base class)
public:
std::string emp_name;
Employee (std::string employeeName) : emp_name (employeeName) {}
void work(){
std::cout << emp_name << ” Employee is working.” << std::endl;
}
void notwork() {
std::cout << emp_name << ” Employee is not working.” << std::endl;
}
};
class Person : public Employee { // here we Derived person class (inherits from Employee base class)
Public:
Person(std::string Personemp_name) : Employee (Personemp_name) {} // here we use Constructor initializes the base class
void leave() {
std::cout << emp_name << ” Employee is on leave.” << std::endl;
}
};
int main() {
Person testPerson(“Siddhi Deora”);
testPerson.work(); // here it Inherited from Employee base class properties
testPerson.notwork(); // here it Inherited from Employee base class properties
testPerson.leave(); // here it is defined in Person derived class
return 0;
}
Explanation of single inheritance in C++.
Base class (Employee).
- In this example, the Employee base class defines an emp_name class member variable attribute and two class methods (work() and notwork()).
- A constructor named Employee in the base class initializes the emp_name attribute.
Derived class (Person).
- The Person derived class inherits from the Employee base class using the public Employee method.
- The Person class creates its own custom method, leave(), in addition to the inherited class methods (work() and notwork()).
- In the constructor of the Person derived class, we call the Employee constructor in the existing class using the Employee(Personemp_name) initializer list.
Usage in main().
- We create testPerson as an object of a Person-derived class and call the work(), notwork(), and leave() class methods on it. The work() and notwork() methods are class properties inherited from the Employee base class, while leave() is a member or property specific to the Person class.

