×

Inheritance Program in C++

What is Inheritance?

Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance.

The ability or process of inheritance allows using properties of one class to other class. The class whose properties are utilized is referred to as the "base class" or "parent class," and the class that uses them is known as the "derived class" or "child class." The derived class is now referred to as having inherited properties from the base class.

When we say that a derived class inherits the base class, we mean that it does so while maintaining all of the base class's properties and having the option to add new features of its own. There will be no impact on the base class from these new features in the derived class. The specialized class for the base class is the derived class.

Subclass or Derived Class: A class that receives properties from another class is referred to as a subclass or derived class.

Super Class: A subclass's base class or superclass is the class from which its properties are inherited.

Inheritance Programs example in C++

Simple Inheritance Programs

1. Program:

#include <iostream>
using namespace std;
class Fine
{
public: void display()
{
cout<<"This display is located inside the first-class section." << endl;
}
};
class good: public Fine
{
public: void show()
{
cout<<"This programme belongs to the second class, which is derived from the parent class." << endl;
}
};
int main()
{
Fine x;
x.display();
//x.show();
good y;
y.display();
y.show();
}

Explanation:

  • In above code, we created two classes named Fine and Good. The good class is what we deduced from the fine.
  • We have two functions: one in the base class and one in the derived class.
  • We have declared our objects in the main method for both the parent class and the child class.
  • We attempted to access both the base and derived class methods with the child class object, and both attempts were completely successful.
  • However, as seen in the second result, if we attempt to access a variable or method from a derived class through an object from the base class, we encounter an error.
  • It is clear because derived class methods and variables cannot be accessed by base class objects but vice versa.

Program Output:

Inheritance Program in C++

Multiple inheritance example.

2. Program:

#include <iostream>


using namespace std;


// base class 1


class data_science


{


public:


    void data_science_Marks()


    {


        cout << "The score for data science is 93."<<endl;


    }


}; 


// base class 2


class Programming


{


public: 


    void Programming_Marks()


    {


        cout << "The score for Programming is 87."<<endl;


    }


}; 


// derived class


class output: public data_science, public Programming


{


public:


    void show_Marks()


    {


        // accessing the member functions 


        // of the base classes


       data_science_Marks();


        Programming_Marks();


    }


}; 


int main()


{


    // create an object of the derived class


    output result;
    
    // call displayMarks() function of the derived class 


    result.show_Marks();  


    return 0;


}

Explanation:

  • First, we created 1st Base class named data_science.
  • 1st Base class consists of a public function, "data_science_Marks," which has void data type.
  • After that, we created the 2nd Base class, which name is Programming.
  • 2nd Base class consists of a public function, "Programming_Marks," which has void data type.
  • Then we created a Derived class whose name is "output," and this class inherits "data_science” and “Programming” classes in Public mode.
  • This "output" class consists of the Public member function “show_Marks," which has a void data type.
  • The member functions "data_science_Marks" and "Programming_Marks" of the base classes "data_science" and "Programming," respectively, are accessed by the function "show Marks."
  • In the main() function, the Object "result" is created of the "output" Class.
  • The function “show_Marks" is called by the object "result".

Program Output:

Inheritance Program in C++

Multilevel Inheritance example.

3. Program:

#include <iostream>
using namespace std;
class Animal
{
public:
Animal ()
{
cout << "This is an Animal" << endl;
}
};
class Reptiles: public Animal
{
public:
Reptiles()
{
cout<<"Reptiles are a class of vertebrates"<<endl;
}
};
class Snake: public Reptiles{
public:
Snake()
{
cout<<"In reptiles, snakes are common."<<endl;
}
};
int main ()
{
Snake obj;
return 0;
}

Explanation:

  • First, we created a Base class whose name is Animal.
  • The base class consists of a Public function Animal(). This function will print a message on the screen "This is an animal".
  • Then we created a derived class named Reptiles, and this class inherits the "Animal” class in public mode.
  • This “Reptiles" class consists of the public member function “Reptiles”. This function will print a message on the screen "Reptiles are a class of vertebrates”.
  • Then we created 2nd derived class, which name is Snake, and this class inherits the "Reptiles" class in public mode.
  • This “Snake" class consists of the public member function "Snake”. This function will print a message on the screen "In reptiles, snakes are common.”.

Program Output:

Inheritance Program in C++

Multiple inheritance example to get the sum of numbers.

4. Program:

#include<iostream>
using namespace std;


	class A // 1st base class
	{
protected:
    int Aint;


public:
    void set_Aint(int x)
    {
        Aint = x;
    }
};


class B // 2nd base class
{
protected:
    int Bint;


public:
    void set_Bint(int x)
    {
        Bint = x;
    }
};


class C // 3rd base class
{
protected:
    int Cint;


public:
    void set_Cint(int x)
    {
        Cint = x;
    }
};
class Derived : public A, public B, public C
{
    public: 
        void show(){
            cout << "The value of 1st Base is " << Aint<<endl;
            cout << "The value of 2nd Base is " << Bint<<endl;
            cout << "The value of 3rd Base is " << Cint<<endl;
            cout << "The sum of these values is " << Aint + Bint + Cint << endl;
        }
};
int main()
{
    Derived david;
    david.set_Aint(55);
    david.set_Bint(50);
    david.set_Cint(75);
    david.show();
    
    return 0;
}

Explanation:

  • First, we developed a class called "A" that contains the protected data member integer "Aint".
  • Afterward, a public function called "set Aint" is part of the "A" class. The value of the data member "Aint" will be set by this function.
  • Then, a "B" class was made, which is composed of the protected data member integer "Bint".
  • "set Bint" is a public function that is part of the "B" class. The "Bint" data member will have its value set by this function.
  • A protected data member integer, "Cint," was then established as part of the "C" class.
  • Afterward, a public function called "set Cint" is part of the "C" class. The "Cint" data member will have its value set by this function.
  • Then, in public mode, we built a class called "Derived" that inherits from classes "A," "B," and "C."
  • Then the "Derived" class consists of the public member function "show".
  • Then "show" will first print the values of "Aint," "Bint," and "Cint" separately before printing the sum of all three values.
  • It is obvious that class "Derived" inherits from classes "A," "B," and "C." This is an illustration of multiple inheritances.
  • The "Derived" data type object "david" is then constructed.
  • Then the object "david" calls the function "set Aint" and passes the value "55" to it.
  • The object "david" then calls the function "set Bint," passing the value "50" along with it.
  • After that, the object "david" calls the function "set Cint" and passes the parameter "75" to it.
  • The object "David" then makes a call to the function "show."

Program Output:

Inheritance Program in C++

C++ Program of Hierarchal inheritance.

5. Program:

#include <iostream>


using namespace std;


// base class


class measurement


{


public:


    int length = 9;


    int breadth = 8;


};


// derived class 1


class circumference: public measurement


{


public:


    // print_circumference() function of the derived class 1


    // to print the circumference


    void print_circumference()


    {


        // calculating the perimeter using the 


        // dimensions in the base class.


        cout << "The calculated circumference is: " << 2 * (length + breadth) << endl;


    }


};


// derived class 2


class area: public measurement


{


public:


    // print_area() function of the derived class 2


    // to print the circumference


    void print_area()


    {


        // calculating the area using the 


        // measurement in the base class.


        cout << "The calculated area is: " << (length * breadth) << endl;


    }


};


int main()


{


    // object of the derived class 1


    circumference output1;


    // object of the derived class 2


    area output2;


    // call print_circumference() function of the derived class 1


    output1.print_circumference();


    // call print_area() function of the derived class 2


    output2.print_area();


    return 0;


}

Explanation:

  • First, we created 1st Base class, with name measurement.
  • 1st Base class consists of public data members with integers "length" and "breadth".
  • Then, we created 1st derived class with name "circumference," which inherits the "measurement" class in Public mode.
  • This "circumference" class consists of the Public member function "print_circumference()," which has a void data type.
  • This function will print the calculated circumference of the data member “length” and “breadth”.
  • Then, we created 2nd derived class, with the name "area," and this class inherits the "measurement" class in Public mode.
  • This "area" class consists of the Public member function "print_area()," which has a void data type.
  • This function will print the calculated area of the data member “length” and “breadth”.
  • In the main() function, Object “output1” of the derived class 1st is created, which has a "circumference" data type.
  • Then Object “output2” of the derived class 2nd  is created, which has the "area" data type.
  • The function "print_circumference()" is called by the object "output1".
  • The function "print_area()" is called by the object "output2".

Program Output:

Inheritance Program in C++

C++ program of Hybrid inheritance.

6. Program:

#include <iostream>  


using namespace std;  


class Books // indicates class A  


{  


public:  


Books()  


    {  


cout<< "This is a Book"<<endl;  


    }  


};  


class Programming: public Books // indicates class B derived from class A


{  


public:  


Programming()  


    {  


cout<< "This is a Programming Book"<<endl;  


    }  


};  


class Data_science  // indicates class C


{  


public:  


Data_science()  


    {  


cout<< "This is a Data Science Book"<<endl;  


    }  


};  


class oops: public Programming, public Data_science


 // indicates class D derived from class B and class C


{  


public:  


oops()  


    {  


cout<< "oops is the part of programming"<<endl;  


    }    


};  


int main() {  


    oops x;  


    return 0;  


}  

Explanation:

  • First, we created 1st class, which name is Books.
  • 1st class consists of a public function, "Books". This function will print the message of the book.
  • Then, we created 2nd class, which name is Programming, and this class inherits the "Books" class in Public mode. This indicates that the class "Programming" is derived from the class "Books".
  • Class "Programming" consist of a Public function, "Programming()," and this function will print a message, "This is a Programming Book".
  • Then, we created 3rd class whose name is Data_science.
  • 3rd class consists of a public function, "Data_science()". This function will print the message "This is a Data Science Book".
  • Then we created a 4th class whose name is oops, and this class inherits the "Programming" and "Data_science" classes in Public mode. This indicates that class “oops” is derived from class “Programming” and “Data_science”.
  • Class "oops" consist of a Public function "oops()," and this function will print a message "oops is the part of programming".

Program Output:

Inheritance Program in C++

C++ Program to demonstrate the Ambiguity Resolution in Inheritance.

7. Program:

#include<iostream>
using namespace std;


class A // 1st Base class
{
    public:
        void saket(){
            cout<<"You are looking good"<<endl;
        }
};


class B // 2nd Base class
{
    public:
        void saket()
        {
            cout << "You are intelligent" << endl;
        }
};




class Derived : public A, public B
{
   int x;
   public:
    void saket(){
        B :: saket();
    }
};


int main()
{
  // Ambibuity 
  
     A Aobj;
     B Bobj;
     Aobj.saket();
     Bobj.saket();
     Derived y;
     y.saket();


    return 0;
}

Explanation:

  • We have created a “A” class which consists of public member function “saket”. The function “saket” will print “You are looking good”
  • We have created a “B” class which consists of public member function “saket”. The function “saket” will print “You are intelligent”
  • We have created a "Derived" class that inherits "A" and "B" classes. The "Derived" class consists of public member function "saket”.
  • As a result of the usage of a scope resolution operator to inform the compiler which function to execute in the absence of ambiguity, the function "saket" will execute the "saket" function of the "B" class.
  • The "A" data type object "Aobj" is created.
  • The "B" data type object "Bobj" is created.
  • The object "Aobj" calls the function "saket".
  • The object "Bobj" calls the function "saket".
  • The "Derived" data type is used to generate the object "y."
  • The object "y" calls the function "saket."
  • The most important thing to take away from this is that the "saket" function of the "B" class will be invoked when the object "y" calls the function "saket," as we stated it using the scope resolution operator "::" to eliminate ambiguity.

Program Output:

Inheritance Program in C++

Related Topics

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.