Use C In this lab you will create classes needed to represen

Use C++.

In this lab you will create classes needed to represent a triangle w/ points in space. Your tasks is to write the code to implement both the PointClass and TriangleClass described below. Then in main() run a sequence of tests without reading input or writing output. Then, only if all tests pass, write \"ALL TESTS PASSED!\" with endline.

PointClass: class PointClass { public: void SetCoords(int x, int y); // Sets the value for member variables xCoord and yCoord

double GetDistance(PointClass ptB); // Returns the distance to ptB.

void Print(); // Print point as \"(xCoord, yCoord)\" with endline

int GetXcoord(); // returns xCoord

int GetYcoord(); // returns yCoord

PointClass operator+(PointClass B); // Overloads \'+\' operator and adds two points:

// (x1,y1)+(x2,y2) is (x1+x2, y1+y2) private: int xCoord; int yCoord; };

TriangleClass: class TriangleClass {

public:

void SetPoints(PointClass A, PointClass B, PointClass C);

double GetArea();

void Print();

private:

PointClass A;

PointClass B;

PointClass C;

};

Solution

#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;

class PointClass {
    public:
        void SetCoords(int x, int y);       /* Sets xCoord and yCoord values */
        double GetDistance(PointClass ptB); /* Returns distance to ptB */
        void Print();                       /* Prints \"(xCoord, yCoord)\ \" */
        int GetXcoord();                    /* Returns xCoord */
        int GetYcoord();                    /* Returns yCoord */
        PointClass operator+(PointClass B); /* Overloads + to add two points.
                                               (a, b) + (c, d) = (a+b, c+d) */
    private:
        int xCoord;
        int yCoord;
};

class TriangleClass {
    public:
        void SetPoints(PointClass A, PointClass B, PointClass C);
        double GetArea();
        void Print();
    private:
        PointClass A;
        PointClass B;
        PointClass C;
};

void PointClass::SetCoords(int x, int y) {
    xCoord = x;
    yCoord = y;
}

double PointClass::GetDistance(PointClass ptB) {
    return sqrt( pow(xCoord - ptB.xCoord, 2) +
                 pow(yCoord - ptB.yCoord, 2) );
}

void PointClass::Print() {
    cout << \"(\" << xCoord << \", \" << yCoord << \")\ \";
}

int PointClass::GetXcoord() {
    return xCoord;
}

int PointClass::GetYcoord() {
    return yCoord;
}

PointClass PointClass::operator+(PointClass B) {
    PointClass newPoint;
    newPoint.SetCoords(xCoord + B.xCoord, yCoord + B.yCoord);
    return newPoint;
}

void TriangleClass::SetPoints(PointClass A, PointClass B, PointClass C) {
    this->A = A;
    this->B = B;
    this->C = C;
}

double TriangleClass::GetArea() {
    /* Using Heron\'s Formula */
    double a = A.GetDistance(B);
    double b = B.GetDistance(C);
    double c = C.GetDistance(A);
    double s = (a + b + c) / 2;
    double area = sqrt(s * (s - a) * (s - b) * (s - c));
    return area;
}

void TriangleClass::Print() {
    cout << \"( (\" << A.GetXcoord() << \", \" << A.GetYcoord() << \"), \";
    cout << \"(\" << B.GetXcoord() << \", \" << B.GetYcoord() << \"), \";
    cout << \"(\" << C.GetXcoord() << \", \" << C.GetYcoord() << \") )\ \";

}


int main() {
    PointClass pt1;
    PointClass pt2;
    PointClass pt3;
    PointClass pt4;
    TriangleClass triangle;

    pt1.SetCoords(0, 0);
    pt2.SetCoords(0, 3);
    pt3.SetCoords(4, 0);
    pt4 = pt1 + pt2;                     /* Using the overloaded + operator */

    triangle.SetPoints(pt1, pt2, pt3);


    assert(pt1.GetDistance(pt2) == 3);   /* Testing distance function */

    assert(pt4.GetXcoord() == 0);        /* Checking to see if point adding works as expected */
    assert(pt4.GetYcoord() == 3);

    assert(triangle.GetArea() == 6); /* Testing area function */

    cout << \"ALL TESTS PASSED!\ \";

    return 0;
}

Use C++. In this lab you will create classes needed to represent a triangle w/ points in space. Your tasks is to write the code to implement both the PointClass
Use C++. In this lab you will create classes needed to represent a triangle w/ points in space. Your tasks is to write the code to implement both the PointClass
Use C++. In this lab you will create classes needed to represent a triangle w/ points in space. Your tasks is to write the code to implement both the PointClass

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site