Answer to Question #326255 in C++ for gayathri

Question #326255

: 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-09T17:54:08-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