week 8 Lab/ Hierarchical Inheritance

PHOTO EMBED

Wed May 08 2024 20:20:56 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #c++

#include <iostream>

using namespace std;
//Hierarchical inheritance
class Shape{
    
    public:
    double area(){
    return 0.0;
    }
    
    void displayShape(){
        cout<<"Generic Shape: ";
    }
};
//Derived REctangle Class From Shape Class
class Rectangle:public Shape{
    double width;
    double length;
    
    public:
    //You can write like this also:
    Rectangle(double w, double l){
        width = w;
        length = l;
    }
    //Both of them are same
    //Rectangle(double w, double l):width(w),length(l){
        
    //}
    double AreaRectangle(){
        return width * length;
    }
    void displayRectangleInfo(){
        displayShape();
        cout<<"Rectangle: \nLength = "<<length<<"\nwidth = "<<width<<"\nArea of Rectangle = "<<AreaRectangle()<<endl;
    }
};
//Second Derived Circle class from Shape class
class Circle:public Shape{
    
    double radius;
    
    public:
    Circle(double r):radius(r){
        
    }
    double AreaCircle(){
        return 3.14 * radius * radius;
    }
    void displayCircleInfo(){
        displayShape();
        cout<<"Circle: \nRadius = "<<radius<<"\nArea of Circle = "<<AreaCircle()<<endl;
    }
};
//Third Derived class from Shape
class Triangle:public Shape {
    
    double base;
    double heigth;
    
    public:
    Triangle(double b, double h):base(b),heigth(h){
        
    }
    double AreaTriangle(){
        return 0.5 * base * heigth;
    }
    void displayTriangleInfo(){
        displayShape();
        cout<<"Triangle: \nBase = "<<base<<"\nHeigth = "<<heigth<<"\nArea of Triangle = "<<AreaTriangle()<<endl;
    }
};

int main() {
    Rectangle rect(10.2, 20.2);
    rect.displayRectangleInfo();
    cout<<endl;
    Circle c(10);
    c.displayCircleInfo();
    cout<<endl;
    Triangle tri(10.2, 10.1);
    tri.displayTriangleInfo();

    return 0;
}
//OUTPUT:
Generic Shape: Rectangle: 
Length = 20.2
width = 10.2
Area of Rectangle = 206.04

Generic Shape: Circle: 
Radius = 10
Area of Circle = 314

Generic Shape: Triangle: 
Base = 10.2
Heigth = 10.1
Area of Triangle = 51.51
content_copyCOPY