← Back to C++ Course | Chapter 9: Inheritance | Lesson 3 of 8

C++ Single Inheritance

Single inheritance is when a class has exactly one parent, like a child copying one parent's skills and adding its own. It reuses the parent's code.
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to access private members of the base class directly in the derived class.
  2. Expecting the derived constructor to run first, when the base constructor runs first.
  3. Forgetting public in the inheritance list, which makes the base members private in the derived class.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.