LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2007, 01:27 PM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
C++ Polymorphism: small question


I have to write a program that uses 3 C++ classes. The user knows only about one of them (let's say class C). The C constructor, depending on the input received should decide with what type of object to work with. Any ideas? Thanks.

Last edited by cdog; 01-13-2007 at 01:37 PM.
 
Old 01-13-2007, 01:47 PM   #2
johnty01
LQ Newbie
 
Registered: Aug 2005
Location: vancouver, bc
Distribution: fc 4
Posts: 26

Rep: Reputation: 15
not sure exactly what you mean here...

so you have 3 classes, A B and C

since you say C is exposed, and 'uses' the other classes, does that mean you want C to contain an instance of either A or B, and it will choose which one based on the C constructor?
 
Old 01-13-2007, 01:50 PM   #3
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Yes John, exactly.
 
Old 01-13-2007, 01:59 PM   #4
johnty01
LQ Newbie
 
Registered: Aug 2005
Location: vancouver, bc
Distribution: fc 4
Posts: 26

Rep: Reputation: 15
i'm not sure if this is the most elegant way to do it, but could you have a member variable in C called useA, and initialize it accordingly in the C constructor.

and then in every C member that would use either class A or B, you'd go

if (useA) {A related code} else {B related code}

Last edited by johnty01; 01-13-2007 at 02:00 PM.
 
Old 01-13-2007, 02:12 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Ok just to make sure what you are asking, as you said "exactly" to something that isn't polymorphism. Are A and B types of C?
If they are and the user does not know about A and B then the correct way would be to use the design pattern factory.

Last edited by dmail; 01-13-2007 at 02:17 PM.
 
Old 01-13-2007, 02:39 PM   #6
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnty01
i'm not sure if this is the most elegant way to do it, but could you have a member variable in C called useA, and initialize it accordingly in the C constructor.

and then in every C member that would use either class A or B, you'd go

if (useA) {A related code} else {B related code}
You're right John it's not very elegant.

Quote:
Originally Posted by johnty01
Ok just to make sure what you are asking, as you said "exactly" to something that isn't polymorphism. Are A and B types of C?
If they are and the user does not know about A and B then the correct way would be to use the design pattern factory.
Please elaborate dmail. I thought I'd use polymorphism. A and B don't have to be types of C. The design is up to me. Thank you both for for ideas.
 
Old 01-13-2007, 02:48 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I'm in a good mood so GPL factory pattern
Although you have me confused maybe you could explain what the class are.
[edit]removed link[/edit]

Last edited by dmail; 01-13-2007 at 03:21 PM.
 
Old 01-13-2007, 02:55 PM   #8
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
class A should be a string and class B a bool array - I thought about templates, but it won't work.
 
Old 01-13-2007, 03:08 PM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
That's all there is to the classes?
If so then just use an std::vector<T> where T is string(ie chars) or bool

Last edited by dmail; 01-13-2007 at 03:11 PM.
 
Old 01-13-2007, 03:16 PM   #10
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
No, I have to use 3 classes: one to call one of the other 2; it's a school assignment.
 
Old 01-13-2007, 03:19 PM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by cdog
No, I have to use 3 classes: one to call one of the other 2; it's a school assignment.
Why didn't you say this in the first place
You will have to think about it yourself then or consult your tutor. You will not learn by somebody supplying the answer for you.
 
Old 01-13-2007, 05:12 PM   #12
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
I didn't ask for the code only the idea. What's the point of this forum if not to ask questions and it's not like you've told me much. Thanks anyway for your time.
 
Old 01-13-2007, 07:17 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by cdog
I didn't ask for the code only the idea. What's the point of this forum if not to ask questions and it's not like you've told me much. Thanks anyway for your time.
did you happen to review the Factory pattern that dmail mentioned. that may shed some light on it for you, and then you could ask questions that make a little more sense.
 
Old 01-13-2007, 08:30 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
I agree that you should ask your instructor... after you have thoroughly reviewed the material to see if you can discover the answer for yourself.

Instructors need feedback! (Believe me, I know...) They can be at the front of that classroom thinking that they are absolutely in a vacuum. When one student comes back with a question, they know that many other students must have the same problem; without it, they don't.

Perhaps you do not yet understand the problem; to me, your description of it was pretty vague. If you don't understand what is being asked of you, that might make it impossible to arrive at the answer. And if that be the case, I can flatly say to you that you are not the only one. "It happens. A lot." Believe me, it's okay to (timely...) ask the instructor to clarify. I assure you that there have been many times when I have sent a "broadcast message to all students" in response to a single inquiry from any one.

That instructor is, after all, in a way working for you. You're paying good money to learn this stuff. It's a two way street. The point, after all, is not that the instructor knows the material! The point is that, by the end of the course, you do, and (of course) you've got the good grades to prove it. If information is flowing in one direction only, it ain't gonna happen and you're wasting your time.

Last edited by sundialsvcs; 01-13-2007 at 08:31 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Small question about /etc/rc.local - Symbolic link question Arodef Linux - General 4 05-13-2006 02:29 AM
Polymorphism problem lucky6969b Programming 3 12-29-2005 02:59 AM
Polymorphism in VBA ashirazi Programming 4 07-05-2005 04:44 PM
small question. bruse Linux - Newbie 9 04-11-2005 07:22 AM
polymorphism in java jwn7 Programming 4 11-17-2004 04:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration