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)].
#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);
}
Comments
Leave a comment