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

C++ Inheritance Introduction

Inheritance lets a new class receive everything from an older class, like a child inheriting a parent's traits. The child can add or change things of its own.
Syntax
cpp
class Base {
  // members
};

class Derived : public Base {
  // extra members
};

What is Inheritance?

Inheritance lets a new class, called the derived class, automatically acquire the properties and methods of an existing class, called the base class.

This promotes code reuse by letting related classes share common behavior instead of each reimplementing it independently.

Example: What is Inheritance?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	void eat() { std::cout << "Eating" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Barking" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.eat();
	d.bark();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base and Derived Class Constructors

When you create an object of a derived class, the base class's constructor always runs first to set up the inherited portion of the object, and only after that finishes does the derived class's own constructor run to initialize the parts it added.

Example: Base and Derived Class Constructors

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	Animal() { std::cout << "Animal constructed" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	Dog() { std::cout << "Dog constructed" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Public Inheritance Access

With public inheritance, any member that was public in the base class remains public in the derived class, meaning external code can access it through a derived object exactly as freely as it could through a base object.

Example: Public Inheritance Access

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	int legs = 4;
};

// Define the type Dog
class Dog : public Animal {};

// Program execution starts in main()
int main() {
	Dog d;
	// Print to the console with cout
	std::cout << d.legs << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Protected Access Specifier

Protected members can never be accessed from outside the class hierarchy entirely, but unlike private members, they remain freely accessible inside any class that derives from the one that declared them — useful for internal state a subclass legitimately needs to build on.

Example: Protected Access Specifier

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
protected:
	int age = 2;
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void showAge() { std::cout << age << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.showAge();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overriding Base Class Methods

Method overriding lets a derived class supply its own specific implementation of a function that the base class already defines, replacing the inherited behavior with logic tailored to that particular subclass.

Example: Overriding Base Class Methods

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "..." << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.speak();
	// 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. Forgetting to call the base class constructor with arguments in the initializer list, so the base's default constructor is required.
  2. Writing class B : A without public and getting private inheritance.
  3. Not making the base destructor virtual when deleting derived objects through a base pointer.

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.