C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
Inheritance in C++ is a way of creating a new class (derived class) from an existing class (base class). The derived class inherits the properties and methods of the base class, allowing for code reuse and the creation of hierarchical class structures.
In this tutorial, we'll cover the basics of inheritance and derivation in C++.
To use the iostream
library, include the iostream
header.
#include <iostream>
A base class is the class from which the derived class will inherit properties and methods. To create a base class, define it as you would any other class.
Example:
class Vehicle { public: void startEngine() { std::cout << "Starting the engine." << std::endl; } };
A derived class is a class that inherits properties and methods from a base class. To create a derived class, use the :
symbol followed by the access specifier and the name of the base class.
Example:
class Car : public Vehicle { public: void honk() { std::cout << "Honking the horn." << std::endl; } };
In this example, the Car
class inherits from the Vehicle
class using the public
access specifier. The derived class Car
can access the startEngine()
method from the base class.
When deriving a class, the access specifier determines how the inherited members will be accessed in the derived class:
public
: Inherited members keep their original access specifier in the derived class.protected
: Inherited public
and protected
members become protected
in the derived class.private
: Inherited public
and protected
members become private
in the derived class.In the derived class, you can access inherited members as if they were members of the derived class itself.
Example:
#include <iostream> class Vehicle { public: void startEngine() { std::cout << "Starting the engine." << std::endl; } }; class Car : public Vehicle { public: void honk() { std::cout << "Honking the horn." << std::endl; } void drive() { startEngine(); // Inherited from Vehicle honk(); } }; int main() { Car myCar; myCar.drive(); return 0; }
Output:
Starting the engine. Honking the horn.
In this example, the Car
class calls the startEngine()
method inherited from the Vehicle
class.
Inheritance and derivation are essential concepts in object-oriented programming, as they enable code reuse and the creation of complex class structures. Understanding these concepts will help you write modular and organized code in C++.
How to use inheritance in C++:
#include <iostream> // Base class class Animal { public: void eat() { std::cout << "Animal is eating." << std::endl; } }; // Derived class inheriting from Animal class Dog : public Animal { public: void bark() { std::cout << "Dog is barking." << std::endl; } }; int main() { Dog myDog; myDog.eat(); // Inherited from Animal myDog.bark(); // Specific to Dog return 0; }
Abstract classes and pure virtual functions in C++:
#include <iostream> // Abstract base class class Shape { public: // Pure virtual function virtual void draw() const = 0; }; // Concrete derived class class Circle : public Shape { public: // Implementation of pure virtual function void draw() const override { std::cout << "Drawing a circle." << std::endl; } }; int main() { Circle myCircle; myCircle.draw(); return 0; }