C++ Program to illustrates the use of Virtual base class

By | 03.01.2017

Illustrates use of Virtual base class


Write a C++ Program to illustrates the use of Virtual base class. Here’s a Simple C++ Program to illustrates the use of Virtual base class in C++ Programming Language.


What is Polymorphism in C++ ?


The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


Virtual Function : :


A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don’t want static linkage for this function.


Below is the source code for C++ Program to illustrates the use of Virtual base class which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to illustrates the use of Virtual base class  */

#include<iostream>
using namespace std;
// This program illustrates the use of the virtual base clas
class B  // Base class
{
                protected:
                        int base_data ;
                public:
                        void get_b_data(int b)
                        {
                                cout << "\n\n Accessing the data from the base class B------------>" ;
                                base_data = b ;
                        }
                        void display_b_data(void)
                        {
                                cout << "\n base_data = " << base_data ;
                        }
};
class D1 : virtual public B  // First virtual base class.
{                                        // This class is derived from the class B.
                protected:
                        int der1_data ;
                public:
                        void get_d1_data(int d1)
                        {
                                cout << "\n\n Accessing the data from the derived class D1---------->" ;
                                der1_data = d1 ;
                        }
                        void display_d1_data(void)
                        {
                                cout << "\n der1_data = " << der1_data ;
                        }
};

class D2 : virtual public B // Second virtual base class.
{                                       // This class is also derived from the class B.
                protected:
                        int der2_data ;
                public:
                        void get_d2_data(int d2)
                        {
                                cout << "\n\n Accessing the data from the derived class D2---------->" ;
                                der2_data = d2 ;
                        }
                        void display_d2_data(void)
                        {
                                cout << "\n der2_data = " << der2_data ;
                        }
};
class D3 : public D1, public D2 // This class inherits the properties of the
{                                                   // classes B, D1 and D2.
                public:
                        int der3_data ;
                public:
                        void get_d3_data(int d3)
                        {
                                cout << "\n\n Accessing the data from the derived class D3---------->" ;
                                der3_data = d3 ;
                        }
                        void display_d3_data(void)
                        {
                                cout << "\n der3_data = " << der3_data ;
                        }
};

int main()
{
        D3 der3 ;  // Object of the class D3.
        der3.get_b_data(7);    // Accessing the member functions of the base class B.
        der3.display_b_data( ) ;
        der3.get_d1_data(8) ; // Accessing the member functions of the class D1.
        der3.display_d1_data( );
        der3.get_d2_data(18) ; // Accessing the member functions of the class D2.
        der3.display_d2_data( );
        der3.get_d3_data(15); // Accessing the member functions from its own class D3.
        der3.display_d3_data( );
return 0;
}

OUTPUT : :


/*  C++ Program to illustrates the use of Virtual base class  */

 Accessing the data from the base class B------------>
 base_data = 7

 Accessing the data from the derived class D1---------->
 der1_data = 8

 Accessing the data from the derived class D2---------->
 der2_data = 18

 Accessing the data from the derived class D3---------->
 der3_data = 15

Process returned 0

Above is the source code and output for C++ Program to illustrates the use of Virtual base class which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments