Answer to Question #326745 in C++ for Aditya

Question #326745

Write a program to perform class to basic conversion [such as : class is : Triangle with data members base and height (float), and basic type is float(area of triangle)].

1
Expert's answer
2022-04-10T12:36:44-0400
#include<iostream>

using namespace std;

class Triangle
{
	float h;
	float base;
public:
	Triangle(float _h,	float _base) :h(_h), base(_base) {}
	float GetHeight() const { return h; }
	float GetBase()const { return base; }
};


class Area
{
public:
	float operator() (const Triangle& t)
	{
		return 0.5*t.GetBase()*t.GetHeight();
	}
};


int main()
{
	Triangle tr(10, 20);
	Area a;
	cout<<"Area of triangle is "<< a(tr);
}

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