![]() |
|
#1
|
|||
|
|||
C++ inheritance questionHi all,
Suppose, I have the following classes B, D1 and D2 as defined below. Also, I have function test which have only pointer to the base class and say key which will tell D1 or D2 to create. CPP / C++ / C Code:
I would like to be able inside of test to call fd1 of D1 or fd2 of D2 depending on key. Based on relationships between B,D1,D2 I cannot do it. Do you have any suggestion how I would do it? Thank you in advance, nuclph. CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: c++ inheritance questionQuote:
Quote:
|
|
#3
|
|||
|
|||
Re: C++ inheritance questionI am not so clear about your doubt. Though, I can explain as for as i understood.
The second argument of test() is somewhat unclear to me. Anyway i just take it as, it informs u about which one among D1 and D2 to choose. In class B{ }; create two virtual functions fd1() and fd2. In test() function, create an object with both D1 d1; and D2 d2; b is a pointer to an object of class B. Put b = &d1 for fd1() or b = &d2 for fd2(). now b->fd1() or b->fd2() will access D1 :: fd1() or D2 :: fd2() respectively. Hope this would help you. |
|
#4
|
|||
|
|||
Re: C++ inheritance questionQuote:
Quote:
The purpose of this site is to provide guidance & help students help themselves. Yes, there are sites where code will be written freely or for a fee, but all that is learned is for posters in a bind to go back to them. Very little learning (if any...) occurs. Most of us here choose to spend our time more constructively. If you have specific questions and/or you want to discuss code you have written, then we can have a dialogue. As it is, you have posted a vague description/understanding of what is most likely a homework problem, & until you are clear as to the fundamental issue(s), me writing code for you is counterproductive. This problem obviously revolves around the concept of the abstraction provided by virtual member functions. I suspect that this second string argument is meant to be a flag which differentiates whether D1::fd1() or D2::fd2() is called. This may be implemented simply as a conditional if-statement or it may be more involved. At this point, you haven't provided sufficient information for me to be any more definitive. I would also invite you to study the following example which demonstrates how different virtual member functions can be called through a base class pointer. Understanding what instance type is assigned to the pointer is fundamental to understanding the problem you are trying to implement. CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Socket programming question. | boreder | C Programming Language | 3 | 12-Oct-2008 00:39 |
| Please, This is Difficult question try for me! | bcompt143 | C++ Forum | 4 | 24-Aug-2008 10:14 |
| Inheritance Suspicious Problem | Peter_APIIT | C++ Forum | 12 | 15-May-2007 23:01 |
| Question about locking surfaces to directly access pixels, SDL. | george89 | C++ Forum | 0 | 18-Jun-2006 22:16 |
| question of practice | magiccreative | C++ Forum | 1 | 06-Feb-2004 08:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The