How to Use Inheritance in C++: Syntax and Examples
In C++,
inheritance allows a class (called derived) to acquire properties and behaviors from another class (called base) using the syntax class Derived : access_specifier Base. This helps reuse code and create relationships between classes.Syntax
Inheritance in C++ is declared by specifying a derived class that inherits from a base class using a colon : followed by an access specifier and the base class name.
- Derived: The new class that inherits features.
- Base: The existing class whose features are inherited.
- Access specifier: Controls access to base class members (
public,protected, orprivate).
cpp
class Base { public: void show() { std::cout << "Base class method" << std::endl; } }; class Derived : public Base { // Derived inherits Base's public members };
Example
This example shows a base class Animal with a method sound(). The derived class Dog inherits from Animal and can use the sound() method directly.
cpp
#include <iostream> class Animal { public: void sound() { std::cout << "Animal makes a sound" << std::endl; } }; class Dog : public Animal { public: void bark() { std::cout << "Dog barks" << std::endl; } }; int main() { Dog myDog; myDog.sound(); // inherited from Animal myDog.bark(); // own method return 0; }
Output
Animal makes a sound
Dog barks
Common Pitfalls
Common mistakes when using inheritance include:
- Forgetting to specify
publicinheritance, which makes base class membersprivateby default inclassinheritance. - Trying to access
privatemembers of the base class directly in the derived class. - Not using virtual destructors in base classes when using polymorphism, which can cause resource leaks.
cpp
class Base { private: int secret = 42; public: void show() { std::cout << "Base show" << std::endl; } }; class Derived : Base { // private inheritance by default public: void test() { // std::cout << secret; // Error: private member // show(); // Error: show() is private in Derived } }; // Correct way: class Base2 { public: virtual ~Base2() {} // virtual destructor }; class Derived2 : public Base2 { // safe polymorphic deletion };
Quick Reference
Remember these key points about inheritance in C++:
- Public inheritance means "is-a" relationship and keeps base class public members public in derived class.
- Protected inheritance makes base public and protected members protected in derived class.
- Private inheritance makes base public and protected members private in derived class.
- Use
virtualkeyword for polymorphism and virtual destructors.
| Inheritance Type | Effect on Base Members in Derived |
|---|---|
| public | public and protected members stay public and protected |
| protected | public and protected members become protected |
| private | public and protected members become private |
Key Takeaways
Use public inheritance to express an "is-a" relationship between classes.
Specify the access level (public, protected, private) when inheriting to control member visibility.
Derived classes inherit accessible members from base classes and can add new features.
Avoid accessing private base members directly in derived classes; use protected or public members.
Always use virtual destructors in base classes when polymorphism is involved to prevent resource leaks.