Vector class, Iterator and Inheritance

I have made two classes of Canvas one makes use of Vectors and other uses and array.

The problem is in class that uses vectors and the problem is that toString method of Rectangle class is not being invoked

how can I do that.

Rephrasing

I want to use vector to invoke method toString of Rectangle class

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class Shapes
{
    protected:
        string name;
        string type;
        string color;
        Border outline;
    public:
        //constructors
        Shapes()
        {
            name = "*";
            type = "*";
            color = "*";
        }
        Shapes(string n, string t, string c,string bC,int bW)
        {
            outline.setBorder(bC,bW);
            setName(n);
            setType(t);
            setColor(c);
        }
        //setters
        void setShape(string n, string t, string c,string bC,int bW)
        {
            setName(n);
            setType(t);
            setColor(c);
            outline.setBorder(bC,bW);
        }
        void setName(string n)
        {
            name = n;
        }
        void setType(string t)
        {
            type = t;
        }
        void setColor(string c)
        {
            color = c;
        }
        //getters
        string getName()
        {
            return name;
        }
        string getType()
        {
            return type;
        }
        string  getColor()
        {
            return color;
        }
        virtual string toString()
        {
            string s;
            ostringstream oss;
            oss <<"Name         :"<<getName()<<endl;
            oss <<"Type         :"<<getType()<<endl;
            oss <<"Border Color :"<<outline.getBorderColor()<<endl;
            oss <<"Border Width :"<<outline.getBorderWidth()<<endl;
            oss <<"Shape Color  :"<<getColor()<<endl;
            s = oss.str();
            return s;
        }
};
class Rectangle : public Shapes
{
    protected:
        int length;
        int width;
    public:
        //constructors
        Rectangle()
        {
            length =0;
            width =0;
        }
        Rectangle(string n, string t, string c,string bC,int bW,int l, int w)
            :Shapes(n,t,c,bC,bW)
        {
            setLength(l);
            setWidth(w);
        }
        //setters
        void setRectangle(string n, string t, string c,string bC,int bW,int l, int w)
        {
            Shapes::setShape(n,t,c,bC,bW);
            setLength(l);
            setWidth(w);
        }
        void setLength(int l)
        {
            length = l;
        }
        void setWidth(int w)
        {
            width = w;
        }
        //getters
        int getLength()
        {
            return length;
        }
        int getWidth()
        {
            return width;
        }
        //method to toString
        string toString()
        {
            string s;
            ostringstream oss;
            oss <<"Rectangle";
            oss <<"\tlength"<<length;
            oss <<"\twidth"<<width;
            s = oss.str();
            return s;

        }

};

class Canvas
{
    private:
        vector<Shapes> listOfshapes;
    public:
        //constructor
        Canvas()
        {
        }
        //method to add a shape
        void addShape(Shapes shap)
        {
            listOfshapes.push_back(shap);
        }
        void display()
        {
            for(vector<Shapes>::iterator iter =listOfshapes.begin()
                ;iter != listOfshapes.end();iter++)
            {
                cout << iter->toString()<<endl;
            }
        }
};
/*class Canvas
{
      private:

             Shapes *s[100];
             int count;

      public:
             Canvas()
             {
				 count = 0;
			 }
             void addShape(Shapes *f)
             {
				 s[count] = f;
				 count+=1;
			 }
			 void display()
			 {
				 int j=0;
				 while(j<count)
				 {
					 cout << endl << s[j]->toString();
					 j+=1;
				 }
			 }

};*/
int main()
{
    Rectangle r1("my Rectangle","teet","blue","freen",3,4,5);
    Canvas c;
    c.addShape(r1);  //use this when canvas is made of vector
    //c.addShape(&r1); //use this when canvas is made of array
    c.display();
    return 0;
}
You need a vector of pointers to Shape.
is this the way ??
this is also not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Canvas
{
    private:
        vector<Shapes> *listOfshapes;
    public:
        //constructor
        Canvas()
        {
        }
        //method to add a shape
        void addShape(Shapes *shap)
        {
            listOfshapes->push_back(*shap);
        }
        void display()
        {
            for(vector<Shapes>::iterator iter =listOfshapes->begin()
                ;iter != listOfshapes->end();iter++)
            {
                cout << iter->toString()<<endl;
            }
        }
};
No, that declares a pointer to a vector of shapes.
This way:
vector<Shapes*> listOfshapes;
Alaa :)
Thanks You were super help.

here is the class, it works fine. But is it fine logically?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Canvas
{
    private:
        vector<Shapes*> listOfshapes;
    public:
        //constructor
        Canvas()
        {
        }
        //method to add a shape
        void addShape(Shapes *shap)
        {
            listOfshapes.push_back(shap);
        }
        void display()
        {
            for(vector<Shapes*>::iterator iter =listOfshapes.begin()
                ;iter != listOfshapes.end();iter++)
            {
                //cout<< listOfshapes[0]->toString();
                cout << (*iter)->toString()<<endl;
            }
        }
};
Last edited on
Well, the main problem I see is memory management. You aren't deleting the pointers in the vector anywhere.
Which is good, because in your example the only element in it wasn't created by new, so that wouldn't be valid.
However, you'll likely want be able to store shapes allocated by new.
Topic archived. No new replies allowed.