virtual class help.

Hi guys, got a prob. In my main i want to call the perimeter int rectangle class rectangle but there seems to be a problem with my object. Can i get some help. This is the problem in my main "cout << ptr[0] -> rect.perimeter(4, 10);"

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
using namespace std;
const int MAX = 100;

class polygon
{
public:
    polygon(int numSides = 0);//+ Polyon(numSides:int) default is 0
    //{+ set(sideNum: int, value:int) : void, ensures no negative side values
    void set(int sideNum, int value){
        sideNum=0;
        value=0;
    }
    //+ get (sideNum:int):int, returns the value of sides[sideNum]
    int get(int sideNum){
        return sides[sideNum];
    }
    //+ perimeter(): int virtual returns the perimeter of the polygon
    int virtual perimeter(){
        return perimeter();
    }
    //+ area(): double virtual, returns the area of the polygon
    double virtual area(){
        return area();
    }
private:
    int numOfSides();
    int sides[MAX];// listed in clockwise order , MAX sides is 100
};

class Rectangle : public polygon
{
public:
    Rectangle(int w=0, int h=0);//class polygon(4)
    double area();//appropriatley
    int perimeter(int w, int h){// appropriatley
        return (2 * (w + h));
    }
};

class Square : public Rectangle
{
public:
    Square(int w=0, int h=0);//appropriately
    double area();//appropriately
    int perimeter(int w, int h){
        return (2 * (w + h));
    }
};

class RightTri : public polygon
{
public:
    RightTri();//class polygon(3)
    double area(); // appropriately
    int perimeter();// appropriately
};

class RectSolid : public Rectangle
{
public:
    int volume();//return the volume.
private:
    int height;
};

int main()
{
    /*
     In your main
     Make a an array of pointers to Polygon and add an instance
     Of a rectangle with sides of length 4 and 10
     A square with sides of length 5
     A right triangle with sides 3, 4 and 5
     A rectangular solid with sides of length 6 and 3 and height 5.
     
     Use the array to have each shape print out its area perimeter , as well
     As the rectangular solid print out its volume
     */
    
    
    polygon *ptr[MAX];
    Rectangle rect;
    Square sqr;
    RightTri right;
    RectSolid rectS;
    
    ptr[0] = new Rectangle();
    cout << ptr[0] -> rect.perimeter(4, 10);
    
    ptr[1] = new Square();
    
    return 0;
}
try ptr[0]->perimeter(4,10)
you need to use dynamic cast or use a rectangle pointer array instead.
In class polygon:
I suspect that perimeter and area should be abstract:
1
2
3
4
    //+ perimeter(): int virtual returns the perimeter of the polygon
    int virtual perimeter() = 0;
    //+ area(): double virtual, returns the area of the polygon
    double virtual area() = 0;

This tells the compiler that it should never create a polygon class directly, but rather should only create derived classes.

Shouldn't numOfSides be a member variable instead of a function? Otherwise how does the polygon know how many sides it has?

Your set() method doesn't look right. Shouldn't it set the appropriate side number to the appropriate value?

In function main:
To print the perimeter and area, I think they're looking for something like this:
1
2
3
for (int i=0; i<4; ++i) {
    cout << "Perimeter = " << ptr[i]->perimeter() << ". Area = " << ptr[i]->area() << '\n';
}

Topic archived. No new replies allowed.