Home Uncategorized Write a Program to show how constructors are invoked in derived class.

Write a Program to show how constructors are invoked in derived class.

SHARE

AIM: Write a C++ Program constructors invoked in derived class.

THEORY:

For each color a separate class with constructor is declared. The classes red, blue, yellow are base classes. The class orange is derived from red and yellow, green from blue and yellow, violet from red and blue.

The classes reddish brown derived from orange and violet, bluish brown from violet and green and finally yellowish brown from green and orange. In function main() objects of the classes reddish brown , bluish brown , yellowish brown are declared .

When objects are declared constructors are executed from base to derived class.

SOURCE CODE:

#include<iostream>
using namespace std;
class red
{
    public:
    red()
    {
        cout<<“Red\n”;
    }
};
class yellow
{
    public:
    yellow()
    {
        cout<<“Yellow\n”;
    }
};
class blue
{
    public:
    blue()
    {
        cout<<“Blue\n”;
    }
};
class orange: public red,yellow
{
    public:
    orange()
    {
        cout<<“Orange\n”;
    }
};
class green:public blue,yellow
{
    public:
    green()
    {
        cout<<“Green\n”;
    }
};
class violet:public red,blue
{
    public:
    violet()
    {
        cout<<“Violet\n”;
    }
};
class reddish_brown:public orange,violet
{
    public:
    reddish_brown()
    {
        cout<<“Reddish brown\n”;
    }
};
class yellowish_brown:public green,orange
{
    public:
    yellowish_brown()
    {
        cout<<“Yellowish brown\n”;
    }
};
class bluish_brown:public violet,green
{
    public:
    bluish_brown()
    {
        cout<<“Bluish brown\n”;
    }
};
int main()
{
    reddish_brown r;
    cout<<endl;
    bluish_brown b;
    cout<<endl;
    yellowish_brown y;
    cout<<endl;
}

OUTPUT:

Red
Yellow
Orange
Red
Blue
Violet
Reddish brown

Red
Blue
Violet
Blue
Yellow
Green
Bluish brown

Blue
Yellow
Green
Red
Yellow
Orange
Yellowish brown

Back to Program.