C++ Single Inheritance
In this page:
class Base {
public:
// shared members
};
class Derived : public Base {
// uses Base members
};
Basic Single Inheritance
Single inheritance is the simplest inheritance relationship in C++: exactly one derived class inherits from exactly one base class, forming a straightforward parent-child pair rather than a more complex hierarchy.
Example: Basic Single Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
void drive() { std::cout << "Driving" << std::endl; }
};
// Define the type Car
class Car : public Vehicle {};
// Program execution starts in main()
int main() {
Car c;
c.drive();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Base Class Members
A single derived class can freely read and write any base class member that was declared public or protected, since inheritance carries that accessibility down into the child — private base members remain off-limits even here.
Example: Accessing Base Class Members
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
int wheels = 4;
};
// Define the type Car
class Car : public Vehicle {};
// Program execution starts in main()
int main() {
Car c;
// Print to the console with cout
std::cout << c.wheels << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Order of Execution
Base class constructors always execute first to establish the inherited portion of the object, and only afterward does the derived class's own constructor run to finish initializing the properties it introduces itself.
Example: Constructor Order of Execution
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
Vehicle() { std::cout << "Vehicle first" << std::endl; }
};
// Define the type Car
class Car : public Vehicle {
// Members below can be accessed from outside the class
public:
Car() { std::cout << "Car second" << std::endl; }
};
// Program execution starts in main()
int main() {
Car c;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Private Inheritance
Private inheritance converts every public and protected member of the base class into a private member inside the derived class, meaning outside code loses all access to those inherited members through a derived object — even though the derived class itself can still use them internally.
Example: Private Inheritance
#include <iostream>
class Vehicle {
public:
int wheels = 4;
};
class Car : private Vehicle {
public:
void showWheels() { std::cout << wheels << std::endl; }
};
int main() {
Car c;
// c.wheels would fail to compile: private inheritance
c.showWheels();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Protected Inheritance
Protected inheritance is a middle ground: it converts all inherited public and protected base members into protected members in the derived class, keeping them hidden from main() or outside code while still letting any further subclasses of the derived class access them.
Example: Protected Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
int wheels = 4;
};
// Define the type Car
class Car : protected Vehicle {
// Members below can be accessed from outside the class
public:
void showWheels() { std::cout << wheels << std::endl; }
};
// Program execution starts in main()
int main() {
Car c;
c.showWheels();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Trying to access private members of the base class directly in the derived class.
- Expecting the derived constructor to run first, when the base constructor runs first.
- Forgetting
publicin the inheritance list, which makes the base members private in the derived class.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: