×

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

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.