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.

Showing Answers 1 - 18 of 18 Answers

danarusu

  • Nov 1st, 2015
 

Code
  1. #include<iostream>

  2. #include "math.h"

  3. using namespace std;

  4.  

  5. class Point

  6. {

  7. public:

  8.         int xCoord;

  9.         int yCoord;

  10.         Point();

  11.         ~Point();

  12.         void Display();

  13. private:

  14. };

  15.  

  16. Point::Point()

  17. {

  18. }

  19.  

  20. Point::~Point()

  21. {

  22. }

  23.  

  24. void Point::Display()

  25. {

  26.         cout << "( " << this->xCoord << " , " << this->yCoord << " )" << endl;

  27. }

  28.  

  29. class DerivedPoint: public Point

  30. {

  31. public:

  32.         DerivedPoint();

  33.         DerivedPoint(int x, int y);

  34.         ~DerivedPoint();

  35.         //DerivedPoint* p;

  36.         float distance(DerivedPoint& _p);

  37. private:

  38.        

  39. };

  40.  

  41. DerivedPoint::DerivedPoint()

  42. {

  43.         this->xCoord = 1;

  44.         this->yCoord = 1;

  45. }

  46.  

  47. DerivedPoint::DerivedPoint(int x, int y)

  48. {

  49.         this->xCoord = x;

  50.         this->yCoord = y;

  51. }

  52.  

  53. DerivedPoint::~DerivedPoint()

  54. {

  55. }

  56.  

  57. float DerivedPoint::distance(DerivedPoint& _d)

  58. {

  59.         float d = 0.0;

  60.         d = sqrt((_d.xCoord - this->xCoord)*(_d.xCoord - this->xCoord) + (_d.yCoord - this->yCoord)*(_d.yCoord - this->yCoord));

  61.  

  62.         return d;

  63. }

  64.  

  65. class Triangle

  66. {

  67. public:

  68.         DerivedPoint _v1;

  69.         DerivedPoint _v2;

  70.         DerivedPoint _v3;

  71.         Triangle();

  72.         Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3);

  73.         ~Triangle();

  74.         bool isRight();

  75. private:

  76. };

  77.  

  78. Triangle::Triangle()

  79. {

  80. }

  81.  

  82. Triangle::Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3)

  83. {

  84.         this->_v1 = v1;

  85.         this->_v2 = v2;

  86.         this->_v3 = v3;

  87. }

  88.  

  89. Triangle::~Triangle()

  90. {

  91. }

  92.  

  93. bool Triangle::isRight(){

  94.  

  95.         float c1 = _v1.distance(_v2);

  96.         float c2 = _v1.distance(_v3);

  97.         float c3 = _v2.distance(_v3);

  98.  

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

  100.  

  101.         if (c3 == ipotenuse){

  102.                 return true;

  103.         }

  104.         else{

  105.                 return false;

  106.         }

  107. }

  108.  

  109. int main(int argc, char * argv){

  110.  

  111.         Point* p1 = new Point();

  112.  

  113.         p1->xCoord = 2;

  114.         p1->yCoord = 3;

  115.         p1->Display();

  116.  

  117.  

  118.         DerivedPoint* p2 = new DerivedPoint(2, 5);

  119.         DerivedPoint* p3= new DerivedPoint(3, 7);

  120.         cout << "Distance: " << p2->distance(*p3) << endl;

  121.  

  122.         DerivedPoint* v1 = new DerivedPoint(0, 0);

  123.         DerivedPoint* v2 = new DerivedPoint(1, 0);

  124.         DerivedPoint* v3 = new DerivedPoint(0, 1);

  125.  

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

  127.        

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

  129.  

  130.         return 0;

  131. }

  Was this answer useful?  Yes

kerrigan

  • Nov 1st, 2015
 

Code
  1. #include<iostream>

  2. #include "math.h"

  3. using namespace std;

  4.  

  5. class Point

  6. {

  7. public:

  8.         int xCoord;

  9.         int yCoord;

  10.         Point();

  11.         ~Point();

  12.         void Display();

  13. private:

  14. };

  15.  

  16. Point::Point()

  17. {

  18. }

  19.  

  20. Point::~Point()

  21. {

  22. }

  23.  

  24. void Point::Display()

  25. {

  26.         cout << "( " << this->xCoord << " , " << this->yCoord << " )" << endl;

  27. }

  28.  

  29. class DerivedPoint: public Point

  30. {

  31. public:

  32.         DerivedPoint();

  33.         DerivedPoint(int x, int y);

  34.         ~DerivedPoint();

  35.         //DerivedPoint* p;

  36.         float distance(DerivedPoint& _p);

  37. private:

  38.        

  39. };

  40.  

  41. DerivedPoint::DerivedPoint()

  42. {

  43.         this->xCoord = 1;

  44.         this->yCoord = 1;

  45. }

  46.  

  47. DerivedPoint::DerivedPoint(int x, int y)

  48. {

  49.         this->xCoord = x;

  50.         this->yCoord = y;

  51. }

  52.  

  53. DerivedPoint::~DerivedPoint()

  54. {

  55. }

  56.  

  57. float DerivedPoint::distance(DerivedPoint& _d)

  58. {

  59.         float d = 0.0;

  60.         d = sqrt((_d.xCoord - this->xCoord)*(_d.xCoord - this->xCoord) + (_d.yCoord - this->yCoord)*(_d.yCoord - this->yCoord));

  61.  

  62.         return d;

  63. }

  64.  

  65. class Triangle

  66. {

  67. public:

  68.         DerivedPoint _v1;

  69.         DerivedPoint _v2;

  70.         DerivedPoint _v3;

  71.         Triangle();

  72.         Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3);

  73.         ~Triangle();

  74.         bool isRight();

  75. private:

  76. };

  77.  

  78. Triangle::Triangle()

  79. {

  80. }

  81.  

  82. Triangle::Triangle(DerivedPoint& v1, DerivedPoint& v2, DerivedPoint& v3)

  83. {

  84.         this->_v1 = v1;

  85.         this->_v2 = v2;

  86.         this->_v3 = v3;

  87. }

  88.  

  89. Triangle::~Triangle()

  90. {

  91. }

  92.  

  93. bool Triangle::isRight(){

  94.  

  95.         float c1 = _v1.distance(_v2);

  96.         float c2 = _v1.distance(_v3);

  97.         float c3 = _v2.distance(_v3);

  98.  

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

  100.  

  101.         if (c3 == ipotenuse){

  102.                 return true;

  103.         }

  104.         else{

  105.                 return false;

  106.         }

  107. }

  108.  

  109. int main(int argc, char * argv){

  110.  

  111.         Point* p1 = new Point();

  112.  

  113.         p1->xCoord = 2;

  114.         p1->yCoord = 3;

  115.         p1->Display();

  116.  

  117.  

  118.         DerivedPoint* p2 = new DerivedPoint(2, 5);

  119.         DerivedPoint* p3= new DerivedPoint(3, 7);

  120.         cout << "Distance: " << p2->distance(*p3) << endl;

  121.  

  122.         DerivedPoint* v1 = new DerivedPoint(0, 0);

  123.         DerivedPoint* v2 = new DerivedPoint(1, 0);

  124.         DerivedPoint* v3 = new DerivedPoint(0, 1);

  125.  

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

  127.        

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

  129.  

  130.         return 0;

  131. }

  Was this answer useful?  Yes

Chandra

  • Nov 1st, 2015
 

Code
  1. class Point{

  2. public int xCoordinate;

  3. public int yCoordinate;

  4. }

  5. class DerivePoint :: public  Point

  6. {

  7.    public DerivePoint() : Point (10, 20)

  8. {

  9. }

  10. public DerivePoint (int x, int y)

  11. {

  12.  xCoordinate = x;

  13. yCoordinate = y;

  14. }

  15. };  

  Was this answer useful?  Yes

ThePurpleTwist

  • Nov 12th, 2015
 

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

  Was this answer useful?  Yes

kladarak

  • 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
  1. #include<iostream>

  2. #include "math.h"

  3.  

  4. using namespace std;

  5.  

  6. class Point

  7. {

  8. public:

  9.         int x; // Renamed from original question for sake of brevity

  10.         int y; // Renamed from original question for sake of brevity

  11.  

  12.         void Display() const;

  13. };

  14.  

  15. void Point::Display() const

  16. {

  17.         cout << "( " << x << "," << y << " )" << endl;

  18. }

  19.  

  20. class DerivedPoint : public Point

  21. {

  22. public:

  23.         DerivedPoint();

  24.         DerivedPoint(int inX, int inY);

  25.        

  26.         float Distance(const DerivedPoint& inRHS) const;

  27. };

  28.  

  29. DerivedPoint::DerivedPoint()

  30. {

  31.         x = 1;

  32.         y = 1;

  33. }

  34.  

  35. DerivedPoint::DerivedPoint(int inX, int inY)

  36. {

  37.         x = inX;

  38.         y = inY;

  39. }

  40.  

  41. float DerivedPoint::Distance(const DerivedPoint& inRHS) const

  42. {

  43.         DerivedPoint delta(inRHS.x - x, inRHS.y - y);

  44.         int distanceSquared = delta.x*delta.x + delta.y*delta.y;

  45.         return sqrt(static_cast<float>(distanceSquared));

  46. }

  47.  

  48. class Triangle

  49. {

  50. public:

  51.         DerivedPoint mV1;

  52.         DerivedPoint mV2;

  53.         DerivedPoint mV3;

  54.  

  55.         Triangle(const DerivedPoint& inV1, const DerivedPoint& inV2, const DerivedPoint& inV3);

  56.         bool IsRight() const;

  57. };

  58.  

  59. Triangle::Triangle(const DerivedPoint& inV1, const DerivedPoint& inV2, const DerivedPoint& inV3)

  60.         : mV1(inV1)

  61.         , mV2(inV2)

  62.         , mV3(inV3)

  63. {

  64. }

  65.  

  66. bool Triangle::IsRight() const

  67. {

  68.         // Two vectors are perpendicular to each other

  69.         // if their dot product is 0. We use this property

  70.         // to detect whether a triangle has a right angle

  71.         // without losing any precision.

  72.  

  73.         // Helper lambdas.

  74.         // Ideally these would be functions on DeltaPoint,

  75.         // but the question doesnt ask for them.

  76.  

  77.         auto deltaPoint = [] (const DerivedPoint& inLHS, const DerivedPoint& inRHS)

  78.         {

  79.                 return DerivedPoint(inLHS.x - inRHS.x, inLHS.y - inRHS.y);

  80.         };

  81.  

  82.         auto dotProduct = [] (const DerivedPoint& inLHS, const DerivedPoint& inRHS)

  83.         {

  84.                 return (inLHS.x * inRHS.x) + (inLHS.y * inRHS.y);

  85.         };

  86.  

  87.         DerivedPoint edge1 = deltaPoint(mV1, mV2);

  88.         DerivedPoint edge2 = deltaPoint(mV2, mV3);

  89.         DerivedPoint edge3 = deltaPoint(mV1, mV3);

  90.  

  91.         return dotProduct(edge1, edge2) == 0

  92.                 || dotProduct(edge2, edge3) == 0

  93.                 || dotProduct(edge1, edge3) == 0;

  94. }

  95.  

  96. int main(int argc, char * argv)

  97. {

  98.         {

  99.                 Point p;

  100.                 p.x = 2;

  101.                 p.y = 3;

  102.                 p.Display(); // outputs ( 2,3 )

  103.         }

  104.  

  105.         {

  106.                 DerivedPoint p0;

  107.                 p0.Display(); // outputs ( 1,1 )

  108.  

  109.                 DerivedPoint p1(2, 3);

  110.                 p1.Display(); // outputs ( 2,3 )

  111.         }

  112.  

  113.         {

  114.                 DerivedPoint p0(0, 0);

  115.                 DerivedPoint p1(3, 4);

  116.  

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

  118.         }

  119.  

  120.         {

  121.                 DerivedPoint p0(0, 0);

  122.                 DerivedPoint p1(3, 0);

  123.                 DerivedPoint p2(3, 4);

  124.                 DerivedPoint p3(4, 4);

  125.  

  126.                 // Testing different vertex ordering

  127.                 Triangle t0(p0, p1, p2);

  128.                 Triangle t1(p1, p2, p0);

  129.                 Triangle t2(p2, p0, p1);

  130.  

  131.                 // Testing non-right angle triangle

  132.                 Triangle t3(p0, p1, p3);

  133.  

  134.                 cout << "Is Triangle 0 right? " << t0.IsRight() << endl; // outputs 1

  135.                 cout << "Is Triangle 1 right? " << t1.IsRight() << endl; // outputs 1

  136.                 cout << "Is Triangle 2 right? " << t2.IsRight() << endl; // outputs 1

  137.                 cout << "Is Triangle 3 right? " << t3.IsRight() << endl; // outputs 0

  138.         }

  139.  

  140.         return 0;

  141. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions