Answer to Question #324970 in C++ for S.M.Masum

Question #324970

H=11.3cm is the height of the triangle.

B=8.7cm is the base of the triangle.

Create a class for the above triangle.

1.Create one default constructor.

2.Create four parameterized constructors .They would look

like-

·Constructor_name(double, double)

.Constructor_name(double, int)

.Constructor_name(int, double)

·Constructor_name(int, int)

3.Create one copy constructor.

4.Create a function which would show the area of the triangle.

5.Create a destructor.

Call each of the functions from main functions. Call Constructor

name(do.

int) using pass by value and Constructor name(int, double) by

pass by

reference.


1
Expert's answer
2022-04-09T17:55:40-0400
using namespace std;


class Triangle
{
	float h;
	float base;
public:
	Triangle() :h(11.3), base(8.7) { cout << "Default constructor: "; }
	Triangle(double _h, double _base) :h(_h), base(_base) { cout << "\ndouble-double: "; }
	Triangle(double _h, int _base) :h(_h), base(_base) { cout << "\ndouble-int: "; }
	Triangle(int &_h, double &_base) :h(_h), base(_base) { cout << "\nint-double: "; }
	Triangle(int _h, int _base) :h(_h), base(_base) { cout << "\nint-int: "; }
	//Copy constructor
	Triangle(const Triangle& T) :h(T.h), base(T.base) { cout << "\nCopy constructor"; }
	float Area()
	{
		return 0.5*h*base;
	}
	~Triangle() { cout << "\nDestructor"; }
};


int main()
{
	Triangle a;
	cout << a.Area();
	Triangle b(5.6, 7);
	cout << " pass by value " << b.Area();
	int x = 5;
	double y = 8.8;
	Triangle c(x, y);
	cout << " pass by reference " << c.Area();
	Triangle d(2.4, 5.5);
	cout << d.Area();
	Triangle e(2, 5);
	cout << e.Area();
}

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