The Triangle Class
Description
Design a class named Triangle that extends GeometricObject class.
The class contains:
Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with color = “blue”, filled = true.
A constructor that creates a triangle with the specified side1, side2, side3 and color = “blue”, filled = true.
The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3().
A function named getArea() that returns the area of this triangle.
A function named getPerimeter() that returns the perimeter of the triangle.
class GeometricObject
{
public:
GeometricObject(string color, bool filled)
{
this->color = color;
this->filled = filled;
}
string getColor()
{ return color; }
void setColor(string color)
{ this->color = color; }
bool isFilled()
{ return filled; }
void setFilled(bool filled)
{ this->filled = filled;}
string toString()
{
return "Geometric object color " + color +
" filled " + ((filled) ? "true" : "false");
}
private:
string color;
bool filled;
};
Hint
You may use "#include ".
Please submit the GeometricObject class and Triangle class.
Don’t submit the main() function.
framework.cpp
#include <iostream>
#include <string>
#include "source.h"
using namespace std;
int main()
{
Triangle shape1;
cout << shape1.getSide1() << endl;
cout << shape1.getSide2() << endl;
cout << shape1.getSide3() << endl;
cout << shape1.getArea() << endl;
cout << shape1.getPerimeter() << endl;
cout << shape1.getColor() << endl;
cout << shape1.isFilled() << endl;
Triangle shape(1, 1.5, 1);
cout << shape.getColor() << endl;
cout << shape.isFilled() << endl;
shape.setColor("yellow");
shape.setFilled(true);
cout << shape.getSide1() << endl;
cout << shape.getSide2() << endl;
cout << shape.getSide3() << endl;
cout << shape.getArea() << endl;
cout << shape.getPerimeter() << endl;
cout << shape.getColor() << endl;
cout << shape.isFilled() << endl;
return 0;
}
source.h
#include <iostream>
#include <cmath>
using namespace std;
class GeometricObject
{
public:
GeometricObject(string color, bool filled)
{
this->color = color;
this->filled = filled;
}
string getColor()
{
return color;
}
void setColor(string color)
{
this->color = color;
}
bool isFilled()
{
return filled;
}
void setFilled(bool filled)
{
this->filled = filled;
}
string toString()
{
return "Geometric object color " + color +
" filled " + ((filled) ? "true" : "false");
}
private:
string color;
bool filled;
};
class Triangle:public GeometricObject
{
public:
Triangle();
Triangle(double side1,double side2,double side3);
double getSide1();
double getSide2();
double getSide3();
double getArea();
double getPerimeter();
private:
double _side1,_side2,_side3;
};
Triangle::Triangle()
:GeometricObject("blue",true),
_side1(1.0),_side2(1.0),_side3(1.0){}
Triangle::Triangle(double side1,double side2,double side3)
:GeometricObject("blue",true),
_side1(side1),_side2(side2),_side3(side3){}
double Triangle::getSide1()
{
return _side1;
}
double Triangle::getSide2()
{
return _side2;
}
double Triangle::getSide3()
{
return _side3;
}
double Triangle::getArea()
{
double p = (_side1 + _side2 + _side3) / 2;
return sqrt(p * (p - _side1) * (p - _side2) * (p - _side3));
}
double Triangle::getPerimeter()
{
return _side1 + _side2 + _side3;
}