Inheritance in C++
Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP), allowing a new class (derived class) to inherit properties and behaviors (methods) from an existing class (base class). It helps to model hierarchical relationships between objects and promotes code reusability, which is a key benefit of OOP.
Dog is an Animal, or a Car is a Vehicle.The syntax to define inheritance in C++ is as follows:
class DerivedClass : access_specifier BaseClass {
// Derived class members
};
public: All public members of the base class are accessible as public members in the derived class.protected: All protected members of the base class are accessible as protected members in the derived class.private: All members of the base class are inaccessible in the derived class (not typically used for inheritance).Let's consider a simple example where we have a base class Animal, and a derived class Dog.
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited function from Animal class
myDog.bark(); // Function of Dog class
return 0;
}
Dog class is derived from the Animal class using the public access specifier.Dog class inherits the eat() method from the Animal class.Dog class has its own method bark().main() function creates an object myDog of type Dog and calls both the inherited eat() method and the bark() method.class Base {
public:
int x;
void show() {
cout << "x = " << x << endl;
}
};
class Derived : public Base {
public:
void set(int val) {
x = val; // x is accessible because it's public in Base
}
};
class Base {
public:
int x;
void show() {
cout << "x = " << x << endl;
}
};
class Derived : protected Base {
public:
void set(int val) {
x = val; // x is accessible but is protected in Derived
}
};
class Base {
public:
int x;
void show() {
cout << "x = " << x << endl;
}
};
class Derived : private Base {
public:
void set(int val) {
x = val; // x is now private in Derived
}
};
#include <iostream>
using namespace std;
class Animal {
public:
Animal() {
cout << "Animal Constructor Called" << endl;
}
};
class Dog : public Animal {
public:
Dog() {
cout << "Dog Constructor Called" << endl;
}
};
int main() {
Dog myDog;
// Output:
// Animal Constructor Called
// Dog Constructor Called
return 0;
}
Animal constructor is called first, followed by the derived class Dog constructor.In inheritance, a derived class can override methods of the base class to provide its own implementation. This is called method overriding. You use the same function name and parameters, but you provide a new implementation in the derived class.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Output: Dog barks
return 0;
}
sound() function in the Animal class is overridden in the Dog class.virtual keyword allows the derived class method to override the base class method, and override helps ensure that the method is correctly overriding a base class function.animal->sound() is called, the derived class (Dog) version of the function is executed, even though the pointer is of type Animal. This is polymorphism in action.In C++, a derived class can inherit from more than one base class. This is known as multiple inheritance.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food" << endl;
}
};
class Mammal {
public:
void breathe() {
cout << "Breathing air" << endl;
}
};
class Dog : public Animal, public Mammal {
public:
void bark() {
cout << "Barking" << endl;
}
};
int main() {
Dog dog;
dog.eat(); // From Animal class
dog.breathe(); // From Mammal class
dog.bark(); // From Dog class
return 0;
}
Dog class inherits from both Animal and Mammal.eat() from Animal and breathe() from Mammal), as well as its own method bark().Dog is a type of Animal).public, protected, private) control the visibility of the base class members in the derived class.Inheritance helps organize code and allows for the creation of more complex systems by building on existing functionality.
Open this section to load past papers