Basic program for Multilevel Inheritance using Constructors
In this, we are going to see a basic program for Multilevel Inheritance with constructors in C++ Programming Language. 
 


The Code given below can be used in TURBO C++ Compilers: -

#include<iostream.h>
#include<conio.h>

class Hello
{
    public:
    char str1[50];
    Hello()
    {
        cout << "Input a word: ";
        cin >> str1;
    }
};

class HelloWorld : public Hello
{
    public:
    char str2[50];
    HelloWorld()
    {
        cout << "Input another word: ";
        cin >> str2;
    }
};

class Getstr : public HelloWorld
{
    public:
    void get()
    {
        cout << str1 << " " << str2;
    }
};

void main()
{
    clrscr();
    Getstr obj;
    obj.get();
    getch();
}

//...........Coded by Sahil Shaikh


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Hello
{
    public:
    string str1;
    Hello()
    {
        cout << "Input a word: ";
        cin >> str1;
    }
};

class HelloWorld : public Hello
{
    public:
    string str2;
    HelloWorld()
    {
        cout << "Input another word: ";
        cin >> str2;
    }
};

class Getstr : public HelloWorld
{
    public:
    void get()
    {
        cout << str1 << " " << str2;
    }
};

int main()
{
    Getstr obj;
    obj.get();
    return 0;
}

//...........Coded by Sahil Shaikh


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post