access data members of the base class in the derived classes
In this, we are going to see how to access data members of the base class in the derived classes using Hierarchical Inheritance  in C++ Programming Language.

 


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

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

// base class
class dimension
{
    public:
    int length;
    int breadth;

    void getdata()
    {
        cout << "Enter the Length: ";
        cin >> length;
        cout << "Enter the Breadth: ";
        cin >> breadth;
    }
};

// derived class 1
class perimeter : public dimension
{
    public:
    // printPerimeter() function of the derived class 1
    // to print the perimeter
    void printPerimeter()
    {
        // calculating the perimeter using the
        // dimensions in the base class.
        cout << "The calculated Perimeter is: " << 2 * (length + breadth) << "\n\n";
    }
};

// derived class 2
class area : public dimension
{
    public:
    // printArea() function of the derived class 2
    // to print the perimeter
    void printArea()
    {
        // calculating the area using the
        // dimensions in the base class.
        cout << "The calculated Area is: " << (length * breadth) << "\n\n";
    }
};

void main()
{
    clrscr();
    // object of the derived class 1
    perimeter obj1;
    obj1.getdata();
    obj1.printPerimeter();

    // object of the derived class 2
    area obj2;
    obj2.getdata();
    obj2.printArea();

    getch();;
}

//.............Coded by RISHAB NAIR

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

#include <iostream>

using namespace std;

// base class
class dimension
{
    public:
    int length;
    int breadth;

    void getdata()
    {
        cout << "Enter the Length: ";
        cin >> length;
        cout << "Enter the Breadth: ";
        cin >> breadth;
    }
};

// derived class 1
class perimeter : public dimension
{
    public:
    // printPerimeter() function of the derived class 1
    // to print the perimeter
    void printPerimeter()
    {
        // calculating the perimeter using the
        // dimensions in the base class.
        cout << "The calculated Perimeter is: " << 2 * (length + breadth) << "\n\n";
    }
};

// derived class 2
class area : public dimension
{
    public:
    // printArea() function of the derived class 2
    // to print the perimeter
    void printArea()
    {
        // calculating the area using the
        // dimensions in the base class.
        cout << "The calculated Area is: " << (length * breadth) << "\n\n";
    }
};

int main()
{
    // object of the derived class 1
    perimeter obj1;
    obj1.getdata();
    obj1.printPerimeter();

    // object of the derived class 2
    area obj2;
    obj2.getdata();
    obj2.printArea();

    return 0;
}

//.............Coded by RISHAB NAIR

#ENJOY CODING

Post a Comment

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

Previous Post Next Post