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.
3 Antworten
+ 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;
}
0
#include<iostream>
#include<cmath>
class area_c1 {
public:
double height;
double width;
};
class Rectangle : public area_c1 {
public:
// Parameterized constructor
Rectangle(double h, double w) {
height = h;
width = w;
}
// Function to calculate the area of a rectangle
double area() {
return height * width;
}
};
class Isosceles : public area_c1 {
public:
// Parameterized constructor
Isosceles(double h, double w) {
height = h;
width = w;
}
// Function to calculate the area of an isosceles triangle
double area() {
return 0.5 * width * sqrt(pow(height, 2) - pow(0.5 * width, 2));
}
};
int main() {
// Example usage of Rectangle class
Rectangle rect(5.0, 10.0);
std::cout << "Area of Rectangle: " << rect.area() << std::endl;
// Example usage of Isosceles class
Isosceles iso(5.0, 6.0);
std::cout << "Area of Isosceles Triangle: " << iso.area() << std::endl;
return 0;
}