D
D
Demigodd2018-04-19 08:52:04
C++ / C#
Demigodd, 2018-04-19 08:52:04

How to calculate Perimeter and Area of ​​a triangle in Class?

spoiler
class Point {
    protected:
        double x, y;
    public:
        Point(double x = 0, double y = 0) 
        {
            this->x = x;
            this->y = y;
        }    
        ~Point() {}
        
        double getX() {
            return x;
        }
        double getY() {
            return y;
        }
        
        void setX(double x) {
            this->x = x; 
        }
        void setY(double y) {
            this->y = y;
        }
};

class Line: public Point {
    private:
        Point a;
    public:
        Line(Point a,Point b): Point(b) {
            this->a = a;
        }
        ~Line() {}
        
        Point getA() {
            return a;
        }
        
        void setA(Point a) {
            this->a = a;
        }
        
        double lineLength() {
            double L = sqrt(pow(a.getX() - getX(), 2) + pow(a.getY() - getY(), 2)); 
            return L;
        }
};

class Triangle: public Point {
    private:
        Point a, b;
    public:
        Triangle(Point a, Point b): Point(c)
        {
            this->a = a;
            this->b = b;
        }
        ~Triangle() {}
        
        Point getA() {
            return a;
        }
        Point getB() {
            return b;
        }
        
        void setA(Point a) {
            this->a = a;
        }
        void setB(Point b) {
            this->b = b;
        }

        double Area() {
            
        }
        double Perimeter() {
            
        }
};

There is a Triangle class that inherited the Point class.
Now you can create a triangle from three points, but how to calculate their
Perimeter P \u003d a + b + c and Area S \u003d 1/2 * ah?
Or maybe you need to create a triangle through Line and calculate its perimeter, if so, how? can be an example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-04-19
@Demigodd

Inheritance implies an "is" relationship. Is the line a point? No. Is a triangle a point? No. So why the hell are you inheriting them like that? A triangle is three points. And not any, but three different points that do not lie on one straight line. The triangle must satisfy these invariants.
Area and perimeter are calculated from three points.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question