C++ Inheritance
C++ Inheritance
In this tutorial, we will learn about inheritance in C++ with the help of examples.
The derived class inherits the features from the base class and can have
additional features of its own. For example,
class Animal {
// eat() function
// sleep() function
};
Here, the Dog class is derived from the Animal class. Since Dog is derived from
Animal , members of Animal are accessible to Dog .
Inheritance in C++
Notice the use of the keyword public while inheriting Dog from Animal.
We can also use the keywords private and protected instead of public . We will
learn about the differences between using private , public and protected later
in this tutorial.
is-a relationship
Inheritance is an is-a relationship. We use inheritance only if an is-a relationship
is present between the two classes.
A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
};
// derived class
class Dog : public Animal {
public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
return 0;
}
Run Code
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
Here, dog1 (the object of derived class Dog ) can access members of the base
class Animal . It's because Dog is inherited from Animal .
We need protected members if we want to hide the data of a class, but still want
that data to be inherited by its derived classes.
#include <iostream>
#include <string>
using namespace std;
// base class
class Animal {
private:
string color;
protected:
string type;
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
}
string getColor() {
return color;
}
};
// derived class
class Dog : public Animal {
public:
void setType(string tp) {
type = tp;
}
void displayInfo(string c) {
cout << "I am a " << type << endl;
cout << "My color is " << c << endl;
}
void bark() {
cout << "I can bark! Woof woof!!" << endl;
}
};
int main() {
// Create object of the Dog class
Dog dog1;
return 0;
}
Run Code
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
I am a mammal
My color is black
Here, the variable type is protected and is thus accessible from the derived
class Dog . We can see this as we have initialized type in the Dog class using the
function setType() .
On the other hand, the private variable color cannot be initialized in Dog .
public:
void setColor(string clr) {
// Error: member "Animal::color" is inaccessible
color = clr;
}
};
Also, since the protected keyword hides data, we cannot access type directly
from an object of Dog or Animal class.
So far, we have used the public keyword in order to inherit a class from a
previously-existing base class. However, we can also use the private and
protected keywords to inherit classes. For example,
class Animal {
// code
};
The various ways we can derive classes are known as access modes. These
access modes have the following effect:
. public: If a derived class is declared in public mode, then the members of the
base class are inherited by the derived class just as they are.
. private: In this case, all the members of the base class become private
The private members of the base class are always private in the derived class.
If we create an object of the derived class and try to access that member
function, the member function in the derived class is invoked instead of the one
in the base class.
The member function of derived class overrides the member function of base
class.