Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Design and code a class for triangles. Your class should store the three

ID: 3783553 • Letter: I

Question

In C++

Design and code a class for triangles. Your class should store the three endpoints as an array of x coordinates and an array of y coordinates. You should implement functions shown below: class Triangle { private: double x[3],y[3]; public: Triangle ( ); Triangle(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y); // specifies the three points string toString ( ); // creates the string in the form “(x,y), (x,y), (x,y)” double get (int point, char c); // return the coordinate for the index, // c should be ‘x’ or ‘y’ void put (int point, char c, double v); // Store value v in the point specified as above double perimeter ( ); // return the perimeter of the triangle double area ( ); // return the area of the triangle }; ostream& operator<< (ostream& os, Triangle t) { }

Explanation / Answer

CODE:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

// class Triangle
class Triangle{
private:
// x[3] & y[3] declared as private members
double x[3], y[3];

// sideA, sideB, sideC declared as private for internal use.
double sideA, sideB, sideC;

public:
// default constructor.
Triangle(){
for(int i=0;i<3;i++){
x[i] = 0;
y[i] = 0;
}
}

// parameterised constructor
Triangle(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y){
x[0] = p1x;
y[0] = p1y;

x[1] = p2x;
y[1] = p2y;

x[2] = p3x;
y[2] = p3y;
}

// print co-ordinates in the required format.
string toString(){
cout << "(" << x[0] << "," << y[0] << ")" << "," << "(" << x[1] << "," << y[1] << ")" << "," << "(" << x[2] << "," << y[2] << ")" << "," << endl;
}

// get the co-ordinate value.
double get (int point, char c){
if (c == 'x')
return x[point];
if (c == 'y')
return y[point];
}

// insert value to specific co-ordinate.
void put (int point, char c, double v){
if (c == 'x')
x[point] = v;
if (c == 'y')
y[point] = v;
}

// calculateDistance finds distance between two co-ordinates using the distance formula.
double calculateDistance(double x0, double y0, double x1, double y1){
return sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
}

// calculate the perimeter.
double perimeter (){
// find the 3 sides of the Triangle and add them.
sideA = calculateDistance(x[0],y[0],x[1],y[1]);
sideB = calculateDistance(x[0],y[0],x[2],y[2]);
sideC = calculateDistance(x[2],y[2],x[1],y[1]);

  
return sideA + sideB + sideC;
}

// calculate the area of the Triangle.
double area (){
// area is calculated using the below Heron's formula.
// Given 3 sides a,b and c of Triangle, we can calculate area as
// Area   = sqrt(p (pa)(pb)(pc))
// where p is half of the perimeter.

// hP is half perimeter.
double hP = perimeter()/2;

double area = sqrt(hP * (hP - sideA) * (hP - sideB) * (hP - sideC));

return area;
}
};

int main(){
double p1x,p1y,p2x,p2y,p3x,p3y;

// enter the co-ordinates.
cout << "Enter the co-ordinates of the Traingle" << endl;
cout << "Enter x[0]" << endl;
cin >> p1x;
cout << "Enter y[0]" << endl;
cin >> p1y;
cout << "Enter x[1]" << endl;
cin >> p2x;
cout << "Enter y[1]" << endl;
cin >> p2y;
cout << "Enter x[2]" << endl;
cin >> p3x;
cout << "Enter y[2]" << endl;
cin >> p3y;

// create an instance of Traingle class.
Triangle tr(p1x, p1y, p2x,p2y,p3x,p3y);

cout << "Perimeter is " << tr.perimeter() << endl;
}

OUTPUT:

$ g++ traingle.cpp

$./a.out

Enter x[0]
1
Enter y[0]
2
Enter x[1]
3
Enter y[1]
4
Enter x[2]
5
Enter y[2]
6

Perimeter is 11.3137