Let's see if I can work through this correctly. NOT compiled, so may well have errors in it.
Code:
1: //modified from TYC++ 3rd edition example 13.9
2:
3: #include <iostream>
4:
5: using namespace std;
6:
7: class Shape
8: {
9: public:
10: Shape(){}
11: ~Shape(){}
12:
13:
14: virtual void Draw() = 0;
15: private:
16: };
17: //yes, you can define pure virtual functions
18: void Shape::Draw()
19: {
20: cout << "Base class method called but not base class object initialized\n";
21: }
22:
23: class Circle : public Shape
24: {
25: public:
26: Circle(int radius):itsRadius(radius){}
27: ~Circle(){}
28:
29:
30: void Draw();
31: private:
32: int itsRadius;
33: int itsCircumference;
34: };
35:
36: void Circle::Draw()
37: {
38: cout << "Circle drawing routine here!\n";
39: Shape::Draw();
40: }
41:
42:
43: class Rectangle : public Shape
44: {
45: public:
46: Rectangle(int len, int width):
47: itsLength(len), itsWidth(width){}
48: ~Rectangle(){}
49:
50:
51:
52:
53: void Draw();
54: private:
55: int itsWidth;
56: int itsLength;
57: };
58:
59: void Rectangle::Draw()
60: {
61: for (int i = 0; i<itsLength; i++)
62: {
63: for (int j = 0; j<itsWidth; j++)
64: cout << "x ";
65:
66: cout << "\n";
67: }
68:
69: }
70:
71:
72: class Square : public Rectangle
73: {
74: public:
75: Square(int len);
76:
77: ~Square(){}
78:
79: };
80:
81: Square::Square(int len):
82: Rectangle(len,len)
83: {}
84:
85:
86:
87:
88:
89:
90:
91:
92:
93: int main()
94: {
95: int choice;
96: bool fQuit = false;
97: Shape * sp;
98:
99: while (1)
100: {
101: cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: ";
102: cin >> choice;
103:
104: switch (choice)
105: {
106: case 1: sp = new Circle(5);
107: break;
108: case 2: sp = new Rectangle(4,6);
109: break;
110: case 3: sp = new Square (5);
111: break;
112: default: fQuit = true;
113: break;
114: }
115: if (fQuit)
116: break;
117:
118: sp->Draw();
119: cout << "\n";
120: }
//the above uses a pointer to an abstract base class to call methods for objects of classes derived from that abstract base class. In the case of Circle, it also demonstrates that you can call virtual base class methods, even pure virtual base class methods, if you want/need to.
//now I'll try to create an array of base class pointers to store data, then use the data, then delete the memory
Shape * arrayShapePtrs[4];
for(int i = 0; i < 4; i++)
{
cout << "(1)Circle (2)Rectangle (3)Square";
cin >> choice;
switch (choice)
{
case 1:
sp = new Circle(5);
break;
case 2: sp = new Rectangle(4,6);
break;
case 3: sp = new Square (5);
break;
default:
break;
}
arrayShapePtrs[i] = sp;
}
for(i = 0; i < 4; i++)
arrayShapePtrs[i]->Draw();
for(i = 0; i < 4; i++)
delete arrayShapePtrs[i];
//and now I'll try to use user input to determine size of array of pointers to Shape, use the pointers and delete the memory
int size;
cout << "input number of shapes to use" << endl;
cin >> size;
//an array of pointers on the heap
Shape ** pointers;
pointers = new Shape * [size];
//fill up the array
for(i = 0; i < size; ++i)
{
cout << "(1)Circle (2)Rectangle (3)Square";
cin >> choice;
switch (choice)
{
case 1:
sp = new Circle(5);
break;
case 2: sp = new Rectangle(4,6);
break;
case 3: sp = new Square (5);
break;
default:
break;
}
pointers[i] = sp;
}
//use the array of pointers
for(i = 0; i < 4; i++)
pointers[i]->Draw();
//delete memory for the individual pointers stored in the array
for(i = 0; i < 4; i++)
delete pointers[i];
//delete memory for the array itself
delete [] pointers;
//and if I didn't make any mistakes, I'm done
return 0;
}