Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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)\n" */
- 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 << ")\n";
- }
- 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() << ") )\n";
- }
- int main() {
- PointClass ptA;
- PointClass ptB;
- PointClass ptC;
- PointClass ptD;
- TriangleClass triangleABC;
- ptA.SetCoords(0, 0);
- ptB.SetCoords(0, 3);
- ptC.SetCoords(4, 0);
- ptD = ptA + ptB; /* Using the overloaded + operator */
- triangleABC.SetPoints(ptA, ptB, ptC);
- assert(ptA.GetDistance(ptB) == 3); /* Testing distance function */
- assert(ptD.GetXcoord() == 0); /* Checking to see if point adding works as expected */
- assert(ptD.GetYcoord() == 3);
- assert(triangleABC.GetArea() == 6); /* Testing area function */
- cout << "ALL TESTS PASSED!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement