Answer to Question #173488 in C++ for praveenkumar.b

Question #173488

Create a class by name Area and initialize the data members. Then, find areas of square [Area(a)], area of rectangle [Area(l, b)], area of triangle [Area(a, b, c)] using constructor overloading. Also define a destructor to destroy the object.


1
Expert's answer
2021-03-27T04:06:44-0400
#include <iostream>
#include <cmath>
 
using namespace std;
 
class Area
{
public:
	Area(double a_);
	Area(double a_, double b_);
	Area(double a_, double b_, double c_);
	void Display();
private:
	double a;
	double b;
	double c;
	double area;
};
 
Area::Area(double a_)
{
	a = a_;
	b = 0;
	c = 0;
	area = a * a;
}
 
Area::Area(double a_, double b_)
{
	a = a_;
	b = b_;
	c = 0;
	area = a * b;
}
 
Area::Area(double a_, double b_, double c_)
{
	a = a_;
	b = b_;
	c = c_;
	double p = (a + b + c) / 2;
	area = sqrt(p * (p - a) * (p - b) * (p - c));
}
 
void Area::Display()
{
	if (b == 0 && c == 0) cout << "Area of square is: " << area << endl;
	if (b != 0 && c == 0) cout << "Area of rectangle is: " << area << endl;
	if (c != 0) cout << "Area of triangle is: " << area << endl;
}
 
 
int main()
{
	Area sq(5);
	sq.Display();
	Area rec(5, 4);
	rec.Display();
	Area tria(5, 4, 3);
	tria.Display();
	cout << endl;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS