Inheritance help

This isnt all the code but I'm getting errors when I try to call the setX and setY values into getPerimeter and getArea, any quick help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <string>
#include <cmath>
using namespace std;

class Polygon {
protected:
    Polygon(double x, double y);
    int numPoints;
    string shapeName;
public:
    Polygon(){};
    void setPoints (double x, double y, int numP);
    // Writes shapeName and all vertices to the screen
    void displayPoints();
    double getPerimeter ();
};

void Polygon::setPoints(double x, double y, int numP) {
    double setX[10], setY[10];
    setX[numP] = x;
    setY[numP] = y;
}

double Polygon::getPerimeter(double x, double y, int numP){
    double setX[10], setY[10];
    for(int i=0;i<numP;i++){
        double Perim = sqrt(pow(setX[i]-setX[i+1],2)-pow(setY[i]-setY[i+1],2));
        return 0;
    }






class Triangle : public Polygon {
public:
    Triangle() {};
    Triangle(double x1, double y1, double x2, double y2, double x3, double y3);
    double getArea();
    int numPoints = 6;
    //void setPoints(double x[],double y[], int numP);
    string shapeName = "Triangle";
};

Triangle::Triangle (double x1, double y1, double x2, double y2, double x3, double y3){
    setPoints(x1, y1, 0);
    setPoints(x2, y2, 1);
    setPoints(x3, y3, 2);
}

double Triangle::getArea(){
    double setX, setY;
    abs((setY[2]-setY[1])*setX[0]-(setX[2]-setX[1]*setY[0]+setX[2]*setY[1]-setY[2]*setX[1]))/sqrt(pow(setY[2]-setY[1],2)+pow(setX[2]-setX[1],2));
}
Hi,

The variables setX and SetY are local to the functions, aren't initialized so have garbage values . In getArea they are doubles, but you refer to them as if they were arrays. They are poor names as well, sorry to say.

Perhaps you meant them to be a member of the base class?

BTW, you should have protected data, it is nearly as bad as public data. Make every data member private, use the class interface functions to interact with them.
Topic archived. No new replies allowed.