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.
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"
// Renamed from original question for sake of brevity
// Renamed from original question for sake of brevity
"( " << x << "," << y << " )"// 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,
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.