Q:

C++ Program to illustrates the use of Constructors in Single Inheritance

0

Write a C++ Program to illustrates the use of Constructors in Single Inheritance. Here’s simple Program to illustrates the use of Constructors in Single Inheritance in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What are Constructors in C++?


A class constructor is a special member function of a class that is executed whenever we create new objects of that class.

 
 

The Compiler calls the Constructor whenever an object is created. Constructors iitialize values to object members after storage is allocated to the object.

class A
{
int x;
public:
A(); //Constructor
};

While defining a contructor you must remeber that the name of constructor will be same as the name of the class, and contructors never have return type.

Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution :: operator.

 

Below is the source code for C++ Program to illustrates the use of Constructors in Single Inheritance 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 Constructors in Single Inheritance  */

#include<iostream>
using namespace std;

class A  // Base class
{
         private:
            int x ;
       public:
            A( )  // Constructor without any argument
            {
                x = 0 ;
                cout << "\n The constructor of the class A without any argument is  invoked*** " ;
            }
           A(int X) // Constructor with one argument
            {
                 x = X ;
                   cout << "\n The constructor of the class A with one argument is  invoked***" ;
            }
           void Enter_x(void)
           {
                cout << "\n\n\t Enter the value of x: ";  cin >> x ;
             }
           void Display_x(void)
           {     cout << "\n\t x = " << x ;   }
};

// ***************Derived Class******************
class B : public A
{
           private:
        int y ;
            public:
                   B( ) : A ( ) // Constructor of the derived class B without any argument
                  {
                        y = 0 ;
                        cout << "\n The constructor of the class B without any argument is  invoked**" ;
                }
                // Constructor of the derived class B with two arguments
                B(int X,   // Argument for constructor A
                        int Y)   // Argument for constructor B
                                 : A(X)   // Call for the constructor A
                {
                        y = Y ;
                          cout << "\n The constructor of the class B with two arguments is invoked***" ;
                }
                void Enter_y(void)
                {     cout << "\t Enter the value of y: " ; cin >> y ;     }
                void Display_y(void)
                {     cout << "\n\t y = " << y ;    }
};


int main()
{
                cout << "\n\n The first object b1 is in use********\n " ;
                B b1 ;      // Invokes the constructor with zero arguments
                b1.Enter_x( );
                b1.Enter_y( );
                b1.Display_x( );
                b1.Display_y( );
                cout << "\n\n The second object b2 is in use********\n " ;
                B b2(5, 6);  // Invokes the constructor with two arguments
                b2.Display_x( );
                b2.Display_y( );
                cout<<"\n";

return 0;
}

OUTPUT : :


/*  C++ Program to illustrates the use of Constructors in single inheritance   */

 The first object b1 is in use********

 The constructor of the class A without any argument is  invoked***
 The constructor of the class B without any argument is  invoked**

         Enter the value of x: 1
         Enter the value of y: 2

         x = 1
         y = 2

 The second object b2 is in use********

 The constructor of the class A with one argument is  invoked***
 The constructor of the class B with two arguments is invoked***
         x = 5
         y = 6

Process returned 0

Above is the source code and output for C++ Program for use of Constructors in single inheritance which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to illustrates the use of Constructors... >>
<< C++ Program for Constructor with Parameters(Parame...