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