Calculate Percentage of each subject using Multilevel Inheritance
In this, we are going to see a program to calculate payable amount of Tenant to Owner using Multilevel Inheritance in C++ Programming Language. 


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

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

class Owner
{
    public:
    int flatNo;
    char OName[50];
    long int oAdhaarNo;
    void GetOwnerDetails()
    { //to get the owner and flat details
        cout << "Enter Owner details: \n";
        cout << "Enter Flat No: ";
        cin >> flatNo;
        cout << "Enter Owner Name: ";
        cin >> OName;   
    }
};

class Tenant : public Owner
{
    public:
    int lengthOfContract;
    char TName[50];
    char ch;
    void GetTenantDetails()
    { //Tenant will inherit the properties of Owner
        cout << "Enter Tenant details: \n";
        cout <<"Enter Tenant's Name: ";
        cin >> TName;
        cout << "Enter Tenant's length of Contract: ";
        cin >> lengthOfContract;
        ch = 'Y';
        cout << "Is tenant sharing the flat? [Y/N]: ";
        cin >> ch;
    }

    double GetRent()
    {

        return (ch == 'Y') ? 18000.00 : 25000.00;
    }
};

class Rent : public Tenant
{
    public:
    void rentdis()
    {
        cout << TName << " will pay Rs. " << GetRent() << " to " << OName << " for " << lengthOfContract << " years totalling " << GetRent() * lengthOfContract;
    }
};

void main()
{
    clrscr();
    Rent obj;
    obj.GetOwnerDetails();
    obj.GetTenantDetails();
    obj.rentdis();
    getch();
}

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


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

#include <iostream>
using namespace std;

class Owner
{
    public:
    int flatNo;
    string OName;
    long int oAdhaarNo;
    void GetOwnerDetails()
    { //to get the owner and flat details
        cout << "Enter Owner details: \n";
        cout << "Enter Flat No: ";
        cin >> flatNo;
        cout << "Enter Owner Name: ";
        cin >> OName;   
    }
};

class Tenant : public Owner
{
    public:
    int lengthOfContract;
    string TName;
    char ch;
    void GetTenantDetails()
    { //Tenant will inherit the properties of Owner
        cout << "Enter Tenant details: \n";
        cout << "Enter Tenant's Name: ";
        cin >> TName;
        cout << "Enter Tenant's length of Contract: ";
        cin >> lengthOfContract;
        ch = 'Y';
        cout << "Is tenant sharing the flat? [Y/N]: ";
        cin >> ch;
    }

    double GetRent()
    {

        return (ch == 'Y') ? 18000.00 : 25000.00;
    }
};

class Rent : public Tenant
{
    public:
    void rentdis()
    {
        cout << TName << " will pay Rs. " << GetRent() << " to " << OName << " for " << lengthOfContract << " years totalling " << GetRent() * lengthOfContract;
    }
};

int main()
{
    Rent obj;
    obj.GetOwnerDetails();
    obj.GetTenantDetails();
    obj.rentdis();
    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