S
shuisheng
Dear All,
Assume I have a base class of Shape, and derived classes of Sphere,
Cube, Cylinder. When user input 1, it creates a new default sphere; 2,
a new default cube; and 3 a new default cylinder.
class Shape {...};
class Sphere: public Shape {...};
class Cube: public Shape {...};
class Cylinder: public Shape {...};
int main()
{
Shape* pShape;
int i;
cin >> i;
switch(i)
{
case(1): pShape = new Sphere(); break;
case(2): pShape = new Cube(); break;
case(3): pShape = new Cylinder(); break;
default: break;
}
delete pShape;
return 0;
}
If considering maintenance, I believe there must be a better code for
it, which may remove the switch. So when I add another shape, I do not
need to modify my main function. Would you please give me some
suggestion?
I appreciate your help.
Shuisheng
Assume I have a base class of Shape, and derived classes of Sphere,
Cube, Cylinder. When user input 1, it creates a new default sphere; 2,
a new default cube; and 3 a new default cylinder.
class Shape {...};
class Sphere: public Shape {...};
class Cube: public Shape {...};
class Cylinder: public Shape {...};
int main()
{
Shape* pShape;
int i;
cin >> i;
switch(i)
{
case(1): pShape = new Sphere(); break;
case(2): pShape = new Cube(); break;
case(3): pShape = new Cylinder(); break;
default: break;
}
delete pShape;
return 0;
}
If considering maintenance, I believe there must be a better code for
it, which may remove the switch. So when I add another shape, I do not
need to modify my main function. Would you please give me some
suggestion?
I appreciate your help.
Shuisheng