GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Mar-2009, 16:46
nuclph nuclph is offline
New Member
 
Join Date: Mar 2009
Posts: 1
nuclph is on a distinguished road

C++ inheritance question


Hi 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:
void test(B *b, const std::string &key)
{
...
}

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:

class B
{
public:
	virtual void print() = 0;
};

class D1 : public B
{
public:
	void print()
	{
		cout << "in class D1" << endl;
	}
	void fd1()
	{
		cout << "class D1 function" << endl;
	}
};

class D2 : public B
{
public:
	void print()
	{
		cout << "in class D2" << endl;
	}
	void fd2()
	{
		cout << "class D2 function" << endl;
	}
};

  #2  
Old 13-Mar-2009, 17:27
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: c++ inheritance question


Quote:
Originally Posted by nuclph
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.
Correct. Instances of D1 can only call D1::fd1(). Instances of D2 can only call D2::fd2() by design.
Quote:
Do you have any suggestion how I would do it?
Why can't fd1() & fd2() reside in the same class?
  #3  
Old 14-Mar-2009, 06:40
sdinfo sdinfo is offline
New Member
 
Join Date: Mar 2009
Posts: 1
sdinfo is on a distinguished road

Re: C++ inheritance question


I 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  
Old 14-Mar-2009, 07:43
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: C++ inheritance question


Quote:
Originally Posted by sdinfo
In class B{ }; create two virtual functions fd1() and fd2.
This isn't what was initially described. Originally, B:: print() was the only virtual member function defined in the base class.
Quote:
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.
This appears to be a homework problem. While I can implement it, you would learn very little if I were to simply give you the answer. You will learn more if you think about it, toil with the concepts, & implement it yourself.

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:
#include <iostream>

using namespace std;

class abstract_base {
public:
  virtual void f() = 0;
};

class derived0 : public abstract_base {
public:
  void f()  { cout << "derived0::f()" << endl; }
};

class derived1 : public abstract_base {
public:
  void f()  { cout << "derived1::f()" << endl; }
};

int
main() {
  derived0 d0;
  derived1 d1;
  abstract_base *p;

  p = &d0;
  p->f();

  p = &d1;
  p->f();

  return 0;
}
So if you want to continue discussion, post what code you may have completed wrapped in [cpp] & [/cpp] tags (according to the posting guidelines...), & we can continue from that point.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.