Answer the question
In order to leave comments, you need to log in
`
How to create class Triangle from class`a Point?
Here is the point class, a point which consists of a coordinate..
class Point {
protected:
float x, y;
public:
Point() {
std::cout << "Point constructor\n";
}
Point(float x, float y):x(x), y(y) {
std::cout << "Point constructor\n";
}
~Point() {
std::cout << "Point destructor\n";
}
float getX() {
return x;
}
float getY() {
return y;
}
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
};
class Triangle: public Point {
protected:
Point p1, p2, p3;
public:
Triangle() {
}
~Triangle() {
}
double getP(){
return p1 + p2 + p3;
}
};
Answer the question
In order to leave comments, you need to log in
1. Why do you need inheritance in general? Are you saying that a triangle is a point??
2. Well, leave the three points that you have already declared as fields, and make methods for operations defined on triangles in general, for example, calculate the perimeter or area. Make a constructor that will accept these three points and additionally check that no two of them lie on the same line - you will get an invariant of a non-degenerate triangle.
What books are you doing this for? Your teaching material is clearly not very good.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question