Question 1: Write a program that defines an outline class with a constructor that gives value to breadth and altitude. The define three sub-classes square, triangle, and rectangle, that calculate the area of an outline (). In the main, define three variables a square, a triangle and a rectangle and then call the outline () function in these three variables.
#include<iostream>
using namespace std;
class outline {
private:
public:
double breadth, altitude;
outline(){
}
outline(double b){
breadth = b;
}
outline(double b, double a){
breadth = b;
altitude = a;
}
double getB(){
return breadth;
}
double getA(){
return altitude;
}
void set_data (double a, double b)
{
breadth = a;
altitude = b;
}
};
class square: public outline{
public:
double area(){
return breadth * breadth;
}
};
class triangle: public outline{
public:
double area(){
return altitude * breadth * 0.5;
}
};
class rectangle: public outline{
public:
double area(){
return altitude * breadth;
}
};
int main(){
cout<<"Enter the breadth:\n";
double b;
cin>>b;
cout<<"Enter the altitude:\n";
double a;
cin>>a;
triangle t;
t.set_data(b,a);
rectangle r;
r.set_data(b,a);
square s;
s.set_data(b,a);
cout<<t.area()<<endl;
cout<<r.area()<<endl;
cout<<s.area()<<endl;
}
Comments
Thanks a lot , helps a lot
Leave a comment