473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Any better code for the following simple case?

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

Dec 12 '06 #1
3 1180

shuisheng wrote:
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

Dec 12 '06 #2

Evan wrote:
shuisheng wrote:
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.
Actually I take that back. I just looked at the GoF again and that's
not what a factory function is.

Evan

Dec 12 '06 #3
shuisheng wrote:
>
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.

Dec 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Michele Simionato | last post by:
I often feel the need to extend the string method ".endswith" to tuple arguments, in such a way to automatically check for multiple endings. For instance, here is a typical use case: if...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
100
by: Peter | last post by:
Company thought DB2 will be better than Oracle. The bottom line is when you do select, the system crash. I think it may take 4-5 years for DB2 to reach Oracle standard. Peter
15
by: gabor | last post by:
hi, there are 2 versions of a simple code. which is preferred? === if len(line) >= (n+1): text = line else:
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
4
by: nw | last post by:
Hi, I was wondering if someone would be able to give me some comments on the following class structure, it feels to me as if there must be a better way, but I'm unsure what it is, perhaps I...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.