Posts

Showing posts with the label inheritance

Multiple, Multilevel, Hybrid and Hierarchical inheritance

Image
Multiple inheritance:                          Multiple inheritance is a type of inheritance in c++ in which a class can be inherited from more than one classes. In this case the inherited class has more than one parent class. Multiple inheritance example:         #include <iostream> using namespace   std; class Animal { public:    Animal(){ cout<<" Animal's constructor "<<endl;} }; class Mammal { public:    Mammal(){cout<<" Mammal's constructor "<<endl;} }; class Bat : public Animal,public Mammal { public:   Bat(){ cout<<" Bat's constructor "<<endl;} }; int main(){     Bat bat1;    return 0; } Output: Animal's constructor Mammal's constructor Bat's constructor Multilevel inheritance:            In multilevel inheritance a class can be derived from another class who is already def...

Inheritance in C++ part 1

Image
Inheritance: A programming technique that is used to reuse an existing class to create a new class is called inheritance . The new class inherits all the behaviour of the original class. The existing class that is raised to create a new class is called super class, base class or parent class. The new class that inherits the properties and functions of the existing class is known as subclass, derived class or child class. The basic principle of inheritance is that each child class shares common properties with the class from which it is derived. The child class inherits all properties of the parent class and can add its own capabilities. Example: Suppose we have a class named Vehicle. The derived classes of this class may share similar properties such as wheels and motor etc. Additionally, derived classes may have their own particular characteristics. For example, a derived class bus may have seats for people but, another derived class truck may have space to carry goods. Advantages of ...