# Read the following code and then implement the following parts:

Read the following code and then implement the following parts:
class Point {
public int xCoordinate;
public int yCoordinate;
}
1. Derive a class from Point and call it DerivedPoint, the new class will contain two constructors, the first one takes no arguments, it assigns the point to the point (1,1) and the second constructor takes two parameters that represents the x and y coordinates respectively.
2. The class Point Implements a method called Display that prints a representation of a point in the following format ( x,y ) if invoked on an object of type point.
3. The class DerivedPoint will contain a method called Distance that calculates the Euclidian distance between this DerivedPoint and another DerivedPoint which is provided as a parameter to this method, this method should return the calculated distance.(Hint: use the built in Math class).
4. Implement another class called Triangle, a triangle is specified by three vertices each of which is a DerivedPoint. This class will contain a constructor that takes three parameters of type DerivedPoint. It also will contain a method called IsRight, this method returns true if the triangle invoked by is a Right triangle and false otherwise.

#### danarusu

• Nov 1st, 2015

```Code#include<iostream>
#include "math.h"
using namespace std;

class Point
{
public:
int xCoord;
int yCoord;
Point();
~Point();
void Display();
private:
};

Point::Point()
{
}

Point::~Point()
{
}

void Point::Display()
{
cout << "( " << this->xCoord << " , " << this->yCoord << " )" << endl;
}

class DerivedPoint: public Point
{
public:
DerivedPoint();
DerivedPoint(int x, int y);
~DerivedPoint();
//DerivedPoint* p;
float distance(DerivedPoint& _p);
private:

};

DerivedPoint::DerivedPoint()
{
this->xCoord = 1;
this->yCoord = 1;
}

DerivedPoint::DerivedPoint(int x, int y)
{
this->xCoord = x;
this->yCoord = y;
}

DerivedPoint::~DerivedPoint()
{
}

float DerivedPoint::distance(DerivedPoint& _d)
{
float d = 0.0;
d = sqrt((_d.xCoord - this->xCoord)*(_d.xCoord - this->xCoord) + (_d.yCoord - this->yCoord)*(_d.yCoord - this->yCoord));

return d;
}

class Triangle
{
public:
DerivedPoint _v1;
DerivedPoint _v2;
DerivedPoint _v3;
Triangle();
Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3);
~Triangle();
bool isRight();
private:
};

Triangle::Triangle()
{
}

Triangle::Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3)
{
this->_v1 = v1;
this->_v2 = v2;
this->_v3 = v3;
}

Triangle::~Triangle()
{
}

bool Triangle::isRight(){

float c1 = _v1.distance(_v2);
float c2 = _v1.distance(_v3);
float c3 = _v2.distance(_v3);

float ipotenuse = sqrt(c1*c1 + c2*c2);

if (c3 == ipotenuse){
return true;
}
else{
return false;
}
}

int main(int argc, char * argv){

Point* p1 = new Point();

p1->xCoord = 2;
p1->yCoord = 3;
p1->Display();

DerivedPoint* p2 = new DerivedPoint(2, 5);
DerivedPoint* p3= new DerivedPoint(3, 7);
cout << "Distance: " << p2->distance(*p3) << endl;

DerivedPoint* v1 = new DerivedPoint(0, 0);
DerivedPoint* v2 = new DerivedPoint(1, 0);
DerivedPoint* v3 = new DerivedPoint(0, 1);

Triangle* t = new Triangle(*v1, *v2, *v3);

cout << "Triangle is right? " << t->isRight() << endl;

return 0;
}```

#### kerrigan Profile Answers by kerrigan

• Nov 1st, 2015

```Code#include<iostream>
#include "math.h"
using namespace std;

class Point
{
public:
int xCoord;
int yCoord;
Point();
~Point();
void Display();
private:
};

Point::Point()
{
}

Point::~Point()
{
}

void Point::Display()
{
cout << "( " << this->xCoord << " , " << this->yCoord << " )" << endl;
}

class DerivedPoint: public Point
{
public:
DerivedPoint();
DerivedPoint(int x, int y);
~DerivedPoint();
//DerivedPoint* p;
float distance(DerivedPoint& _p);
private:

};

DerivedPoint::DerivedPoint()
{
this->xCoord = 1;
this->yCoord = 1;
}

DerivedPoint::DerivedPoint(int x, int y)
{
this->xCoord = x;
this->yCoord = y;
}

DerivedPoint::~DerivedPoint()
{
}

float DerivedPoint::distance(DerivedPoint& _d)
{
float d = 0.0;
d = sqrt((_d.xCoord - this->xCoord)*(_d.xCoord - this->xCoord) + (_d.yCoord - this->yCoord)*(_d.yCoord - this->yCoord));

return d;
}

class Triangle
{
public:
DerivedPoint _v1;
DerivedPoint _v2;
DerivedPoint _v3;
Triangle();
Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3);
~Triangle();
bool isRight();
private:
};

Triangle::Triangle()
{
}

Triangle::Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3)
{
this->_v1 = v1;
this->_v2 = v2;
this->_v3 = v3;
}

Triangle::~Triangle()
{
}

bool Triangle::isRight(){

float c1 = _v1.distance(_v2);
float c2 = _v1.distance(_v3);
float c3 = _v2.distance(_v3);

float ipotenuse = sqrt(c1*c1 + c2*c2);

if (c3 == ipotenuse){
return true;
}
else{
return false;
}
}

int main(int argc, char * argv){

Point* p1 = new Point();

p1->xCoord = 2;
p1->yCoord = 3;
p1->Display();

DerivedPoint* p2 = new DerivedPoint(2, 5);
DerivedPoint* p3= new DerivedPoint(3, 7);
cout << "Distance: " << p2->distance(*p3) << endl;

DerivedPoint* v1 = new DerivedPoint(0, 0);
DerivedPoint* v2 = new DerivedPoint(1, 0);
DerivedPoint* v3 = new DerivedPoint(0, 1);

Triangle* t = new Triangle(*v1, *v2, *v3);

cout << "Triangle is right? " << t->isRight() << endl;

return 0;
}```

#### kerrigan Profile Answers by kerrigan

• Nov 1st, 2015

( 2 , 3 )
Distance: 2.23607
Triangle is right? 1

#### Chandra

• Nov 1st, 2015

```Codeclass Point{
public int xCoordinate;
public int yCoordinate;
}
class DerivePoint :: public  Point
{
public DerivePoint() : Point (10, 20)
{
}
public DerivePoint (int x, int y)
{
xCoordinate = x;
yCoordinate = y;
}
};  ```

#### ThePurpleTwist

• Nov 12th, 2015

There is a mistake in the code submitted by chandra ;
DerivedPoint : public Point instead of "::"

• Nov 28th, 2015

Danarusus answer was pretty much there, but there are a few things that could be improved with it.
- Display() doesnt output x and y coords in exactly the format requested. There should be no spaces around the comma.
- The isRight() implementation only considers whether one pair of the sides are at right angles to each other - specifically v1-v2 and v1-v3. It doesnt check whether the other two pairs of sides are at right angles, and so the function will return false in those situations. Another problem is that a floating point comparison is made, which due to floating point rounding errors will fail most of the time. It would be better to check whether the hypotenuse is within a small range or the other side.
- DerivedPoint::distance and the Triangle constructor take DerivedPoint arguments by non-const reference. This will prevent construction of a Triangle using const DerivedPoints, or calling distance with a const DerivedPoint without copying them first. These functions ought to take const DerivedPoint& so that they can be used in all contexts. On the subject of const correctness, distance() and isRight() ought to be marked const, because they do not modify the internal state of DerivedPoint or Triangle.
- Destructor in Point needs to be marked virtual since the class is being inherited from, to make sure memory gets cleaned up properly if a DerivedPoint object is deleted from a Point pointer (although a moot point since DerivedPoint doesnt use any extra memory...). Since the destructors dont do anything anyway, and the question doesnt ask for them, it would be better to omit them completely and let the compiler create default destructors for you.
- The demonstrations in main() leak memory, although it doesnt really matter since the program exits immediately. Its better practice to stack allocate these objects when their lifetime is so short, rather than dynamically allocating them like this.
Heres my version, attempting to improve on danarusus answer.

```Code#include<iostream>
#include "math.h"

using namespace std;

class Point
{
public:
int x; // Renamed from original question for sake of brevity
int y; // Renamed from original question for sake of brevity

void Display() const;
};

void Point::Display() const
{
cout << "( " << x << "," << y << " )" << endl;
}

class DerivedPoint : public Point
{
public:
DerivedPoint();
DerivedPoint(int inX, int inY);

float Distance(const DerivedPoint& inRHS) const;
};

DerivedPoint::DerivedPoint()
{
x = 1;
y = 1;
}

DerivedPoint::DerivedPoint(int inX, int inY)
{
x = inX;
y = inY;
}

float DerivedPoint::Distance(const DerivedPoint& inRHS) const
{
DerivedPoint delta(inRHS.x - x, inRHS.y - y);
int distanceSquared = delta.x*delta.x + delta.y*delta.y;
return sqrt(static_cast<float>(distanceSquared));
}

class Triangle
{
public:
DerivedPoint mV1;
DerivedPoint mV2;
DerivedPoint mV3;

Triangle(const DerivedPoint& inV1, const DerivedPoint& inV2, const DerivedPoint& inV3);
bool IsRight() const;
};

Triangle::Triangle(const DerivedPoint& inV1, const DerivedPoint& inV2, const DerivedPoint& inV3)
: mV1(inV1)
, mV2(inV2)
, mV3(inV3)
{
}

bool Triangle::IsRight() const
{
// Two vectors are perpendicular to each other
// if their dot product is 0. We use this property
// to detect whether a triangle has a right angle
// without losing any precision.

// Helper lambdas.
// Ideally these would be functions on DeltaPoint,
// but the question doesnt ask for them.

auto deltaPoint = [] (const DerivedPoint& inLHS, const DerivedPoint& inRHS)
{
return DerivedPoint(inLHS.x - inRHS.x, inLHS.y - inRHS.y);
};

auto dotProduct = [] (const DerivedPoint& inLHS, const DerivedPoint& inRHS)
{
return (inLHS.x * inRHS.x) + (inLHS.y * inRHS.y);
};

DerivedPoint edge1 = deltaPoint(mV1, mV2);
DerivedPoint edge2 = deltaPoint(mV2, mV3);
DerivedPoint edge3 = deltaPoint(mV1, mV3);

return dotProduct(edge1, edge2) == 0
|| dotProduct(edge2, edge3) == 0
|| dotProduct(edge1, edge3) == 0;
}

int main(int argc, char * argv)
{
{
Point p;
p.x = 2;
p.y = 3;
p.Display(); // outputs ( 2,3 )
}

{
DerivedPoint p0;
p0.Display(); // outputs ( 1,1 )

DerivedPoint p1(2, 3);
p1.Display(); // outputs ( 2,3 )
}

{
DerivedPoint p0(0, 0);
DerivedPoint p1(3, 4);

cout << "Distance: " << p0.Distance(p1) << endl; // outputs 5
}

{
DerivedPoint p0(0, 0);
DerivedPoint p1(3, 0);
DerivedPoint p2(3, 4);
DerivedPoint p3(4, 4);

// Testing different vertex ordering
Triangle t0(p0, p1, p2);
Triangle t1(p1, p2, p0);
Triangle t2(p2, p0, p1);

// Testing non-right angle triangle
Triangle t3(p0, p1, p3);

cout << "Is Triangle 0 right? " << t0.IsRight() << endl; // outputs 1
cout << "Is Triangle 1 right? " << t1.IsRight() << endl; // outputs 1
cout << "Is Triangle 2 right? " << t2.IsRight() << endl; // outputs 1
cout << "Is Triangle 3 right? " << t3.IsRight() << endl; // outputs 0
}

return 0;
}```