C++ Inheritance Introduction
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to call the base class constructor with arguments in the initializer list, so the base's default constructor is required.
- Writing
class B : Awithoutpublicand getting private inheritance. - Not making the base destructor
virtualwhen deleting derived objects through a base pointer.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: