Any better code for the following simple case?

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
 
E

Evan

shuisheng said:
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?

Without a vast amount of overkill, I don't see how you can improve this
a whole lot.

You can however move the switch statement into another funtion as so:

Shape* makeShape(int i)
{
switch(i)
{
case 0: return new Sphere();
...
}
}

Then in main:

Shape* pShape = makeShape(i);


Don't forget that you still have to delete the returned pointer though.


A function like makeShape is a slight variant of something called a
factory function.

Evan
 
E

Evan

Evan said:
Without a vast amount of overkill, I don't see how you can improve this
a whole lot.

You can however move the switch statement into another funtion as so:

Shape* makeShape(int i)
{
switch(i)
{
case 0: return new Sphere();
...
}
}

Then in main:

Shape* pShape = makeShape(i);


Don't forget that you still have to delete the returned pointer though.


A function like makeShape is a slight variant of something called a
factory function.

Actually I take that back. I just looked at the GoF again and that's
not what a factory function is.

Evan
 
D

Daniel T.

shuisheng said:
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?

Adjusting one switch statement because you are adding a new type is
not a big deal. It's when you have to adjust several switch statements,
in disparet parts of the program that it causes a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top