Write a program that lets you work withtriangles. The program should let a user
ID: 3684539 • Letter: W
Question
Write a program that lets you work withtriangles. The program should let a user create a triangle with a default base of 1 and a height of 1. The program should provide a way to increase or decrease the above mentioned dimensions by a value of 1
A function to validate user input 3. A function to display the menu 4. The function main() should only be a collection of function calls. 5. Comment your code and use meaningful or mnemonic variable names.
MUST USE given .h files
#ifndef Triangle_H
#define Triangle_H
#include <iostream>
using namespace std;
class Triangle;//forward declaration
//some compilers require to prototype the << and >> operator functions
ostream &operator <<(ostream & someStream, const Triangle & someTriangle); // Overloaded << operator
class Triangle
{
public:
Triangle();//default value of base = 1 and height = 1;
Triangle(double base, double height);
//mutators
void setBase(double b);//used to set the value of the base
void setHeigth(double h);//used to set the value of the height
//Accessors
double getBase() const;
double getHeight()const;
double getArea()const;
// Overloaded operators
Triangle operator ++(); // Prefix ++ increase the size of the base by 1 abd height by 1
Triangle operator ++(int); // Postfix ++ increase the size of the base by 1 abd height by 1
Triangle operator --(); // Prefix -- decrease the size of the base by 1 abd height by 1
Triangle operator --(int); // Postfix -- decrease the size of the base by 1 abd height by 1
friend ostream &operator <<(ostream &, const Triangle &); // Overloaded << operator
/*Use this operator to display on the screen the value of the base, the height and the area of the triangle
*/
private:
double base;
double height;
};
#endif
Explanation / Answer
Here is the complete program
#include <iostream>
using namespace std;
class Triangle;//forward declaration
//some compilers require to prototype the << and >> operator functions
ostream &operator <<(ostream & someStream, const Triangle & someTriangle); // Overloaded << operator
class Triangle
{
public:
Triangle();//default value of base = 1 and height = 1;
Triangle(double base, double height);
//mutators
void setBase(double b);//used to set the value of the base
void setHeigth(double h);//used to set the value of the height
//Accessors
double getBase() const;
double getHeight()const;
double getArea()const;
// Overloaded operators
Triangle operator ++(); // Prefix ++ increase the size of the base by 1 abd height by 1
Triangle operator ++(int); // Postfix ++ increase the size of the base by 1 abd height by 1
Triangle operator --(); // Prefix -- decrease the size of the base by 1 abd height by 1
Triangle operator --(int); // Postfix -- decrease the size of the base by 1 abd height by 1
friend ostream &operator <<(ostream &, const Triangle &); // Overloaded << operator
/*Use this operator to display on the screen the value of the base, the height and the area of the triangle
*/
private:
double base;
double height;
};
Triangle::Triangle()
{
this->base=1;
this->height=1;
}
Triangle::Triangle(double base, double height)
{
this->base=base;
this->height=height;
}
void Triangle::setBase(double base)
{
this->base=base;
}
void Triangle::setHeigth(double height)
{
this->height=height;
}
double Triangle::getHeight() const
{
return height;
}
double Triangle::getBase() const
{
return base;
}
double Triangle::getArea() const
{
return (height*base)/2;
}
Triangle Triangle:: operator ++(int a)
{
Triangle t2(base+1, height+1);
cout << t2.base;
return t2;
}
Triangle Triangle:: operator --(int a)
{
Triangle t(base-1, height-1);
return t;
}
ostream &operator <<(ostream &out, const Triangle &t)
{
out <<"Base:"<<t.base<<" Height:"<< t.height<<" Area:"<<t.getArea()<<endl;
return out;
}
void menu(Triangle &t)
{
cout << "Triangle created "<<t;
int c;
while(true)
{
cout << "Please enter 1 increase dims 2 to decrease dims 3 to display triangle 4 to exit ";
cin >>c;
if(c==0)
break;
else if(c==1)
{
t=t++;
cout << t;
}
else if(c==2)
{
t=t--;
cout <<t;
}
else if(c==3)
{
cout <<t;
}
}
}
void main()
{
Triangle t(2,1);
menu(t);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.