Write a c++ program Given the following | SoloLearn: Learn to code for FREE!

0

Write a c++ program Given the following

Write a c++ program Given the following base class Class area-c1{ Public: double height; double width; }; Create two derived classes called rectangle and isosceles that inherit area-c1. Have each class include a function called area ( ) that returns the area ofa rectangle or isosceles triangle, as appropriate. Use parameterized constructor to initialize height and width.

1/15/2017 3:34:49 PM

Mohammad Touhidul Islam

2 Answers

New Answer

+3

#include <iostream> using namespace std; class Area{ public: double length; double breadth; double height; double base; }; class Rectangle:public Area{ public: void setlength(double l){ length=l; } void setbreadth(double b){ breadth=b; } double area(){ return length*breadth; } }; class Triangle:public Area{ public: void setbase(double b) { base=b; } void setheight(double h){ height=h; } double area(){ return (base*height)/2; } }; int main() { Rectangle rec; Triangle isc; rec.setlength(4.63); rec.setbreadth(5.82); isc.setheight(5.72); isc.setbase(6.73); cout<<"the area of Rectangle is : "<<rec.area()<<endl; cout<<"the area of Triangle is : "<<isc.area(); return 0; } modify the parameters as you want... *****Happy Coding..*****😀

+2

using parameterized constructor #include <iostream> using namespace std; class Area{ public: double length; double breadth; double height; double base; Area(double l,double b,double h,double ba){ cout<<"constructor called and assigning values"<<endl; length=l; breadth=b; height=h; base=ba; } ~Area(){ cout<<"destructor called"; } double areaR(){ return length*breadth;} double areaT(){ return (base*height)/2;} }; int main() { Area ar(4.53,5.52,5.987,4.321); cout<<"the area of Rectangle is : "<<ar.areaR()<<endl; cout<<"the area of Triangle is : "<<ar.areaT()<<endl; return 0; }