Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Tuesday, April 18, 2017

C++ code showing Hierarchical Inheritance to calculate Area

This is the solution for T.U.2068 Q.N.3

#include <iostream>

using namespace std;

class Shape
{
protected:
    int A;
public:
    Shape()
    {
        A=0;
    }
    void show();
};

class Triangle:public Shape
{
    int h, b;
public:
    void Geth();
    void Getb();
    void Showhb();
    void CalTriA();
};

class Rectangle:public Shape
{
    int l, b;
public:
    void Getl();
    void Getb();
    void Showlb();
    void CalRecA();
};

void Shape::show()
{
    cout << endl << "Area=" << A << endl ;
}

void Triangle::Geth()
{
    cout << endl << "Enter height of the triangle:" ;
    cin >> h ;
}

void Triangle::Getb()
{
    cout << endl << "Enter base of the triangle:" ;
    cin >> b ;
}

void Triangle::Showhb()
{
    cout << endl << "Triangle:" << endl ;
    cout << "Height=" << h << endl << "Base=" << b << endl ;
}

void Triangle::CalTriA()
{
    A = (h*b)/2;
}

void Rectangle::Getl()
{
    cout << endl << "Enter length of Rectangle:" ;
    cin >> l ;
}

void Rectangle::Getb()
{
    cout << endl << "Enter breadth of Rectangle:" ;
    cin >> b ;
}

void Rectangle::Showlb()
{
    cout << endl << "Rectangle" << endl ;
    cout << "Length=" << l << endl << "Breadth=" << b << endl ;
}

void Rectangle::CalRecA()
{
    A = l*b ;
}


int main()
{
    Triangle T;
    T.Geth();
    T.Getb();
    Rectangle R;
    R.Getl();
    R.Getb();
    T.Showhb();
    T.CalTriA();
    T.show();
    R.Showlb();
    R.CalRecA();
    R.show();
    return 0;
}

C++ code showing Inheritance and Data Conversion

This is the solution for T.U. 2067 Q.N.3

#include <iostream>

using namespace std;

class clock
{
protected:
    int hr, min, sec ;
public:
    clock()
    {
        hr = min = sec = 0;
    }
    clock(int x)
    {
        hr = min = sec = x ;
    }
    void Show();
};

class wall_clock:public clock
{
    int TotalTime;
public:
    wall_clock():clock()
    {
        TotalTime = 0 ;
    }
    wall_clock(int x):clock(x)
    {

    }
    void Add(int, int, int);
    void ShowTime();
};

void clock::Show()
{
    cout << endl << "Initially" << endl ;
    cout << endl << hr << ":" << min << ":" << sec << endl ;
}

void wall_clock::Add(int a, int b, int c)
{
    hr+=a;
    min+=b;
    sec+=c;
}

void wall_clock::ShowTime()
{
    if(sec>60)
    {
        min += sec/60;
        sec = sec%60;
    }
    if(min>60)
    {
        hr += min/60 ;
        min = min%60;
    }
    cout << endl << hr << ":" << min << ":" << sec << endl ;
}

int main()
{
    wall_clock WC1,WC2;
    WC1 = 0 ;
    WC2 = 0 ;
    WC1.Show();
    WC2.Show();
    WC1.Add(1,17,65);
    WC2.Add(4,45,49);
    WC1.ShowTime();
    WC2.ShowTime();
    return 0;
}

C++ code showing Hierarchical Inheritance

This is the solution for T.U. 2066 Q.N. 3

#include <iostream>

using namespace std;

class Student
{
protected:
    float avg;
public:
    void Show();
};

class ComputerScience:public Student
{
    float OOPL, OS, NM ;
public:
    void GetMarks1();
    void ShowMarks1();
    void CalAvg1();
};

class Mathematics:public Student
{
    float Calculus, LA, Geometry ;
public:
    void GetMarks2();
    void ShowMarks2();
    void CalAvg2();
};

void Student::Show()
{
    cout << endl << "Average=" << avg << endl ;
}

void ComputerScience::GetMarks1()
{
    cout << endl << "Enter marks of:" << endl ;
    cout << endl << "OOPL:"  ;
    cin >> OOPL ;
    cout << endl << "NM:" ;
    cin >>  NM ;
    cout << endl << "OS:" ;
    cin >> OS ;
}

void ComputerScience::ShowMarks1()
{
    cout << endl << "Computer Science:" << endl ;
    cout << endl << "OOPL=" << OOPL << endl << "NM=" << NM << endl << "OS=" << OS << endl ;
}

void ComputerScience::CalAvg1()
{
    avg = (OOPL + NM + OS)/3 ;
}

void Mathematics::GetMarks2()
{
    cout << endl << "Enter Marks of:" << endl ;
    cout << endl << "Calculus:" ;
    cin >> Calculus ;
    cout << endl << "LA:" ;
    cin >> LA ;
    cout << endl << "Geometry:" ;
    cin >> Geometry ;
}

void Mathematics::ShowMarks2()
{
    cout << endl << "Mathematics:" << endl ;
    cout << endl << "Calculus=" << Calculus << endl << "LA=" << LA << endl << "Geometry=" << 
    Geometry << endl ;
}

void Mathematics::CalAvg2()
{
    avg = (Calculus + LA + Geometry)/3 ;
}

int main()
{
    ComputerScience CS;
    CS.GetMarks1();
    CS.CalAvg1();
    Mathematics M;
    M.GetMarks2();
    M.CalAvg2();
    CS.ShowMarks1();
    CS.Show();
    M.ShowMarks2();
    M.Show();
    return 0;
}