Problem in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 32
Reputation: sdmahapatra is an unknown quantity at this point 
Solved Threads: 0
sdmahapatra sdmahapatra is offline Offline
Light Poster

Problem in c++

 
0
  #1
Jul 20th, 2009
I have a function under Base class and at the definition tine of this base class function I need to call another function which is define under the Derived class. how to do this??
My class declaration is below
  1. class Base{
  2.  
  3. public:
  4.  
  5. void showdata(double);
  6.  
  7. };
  8.  
  9. class Derived : public Base{
  10.  
  11. public:
  12. inline double F(double x)
  13. {
  14. return exp(-x) + x*x;
  15. }

Define the function below :

  1. void Base::showdata(double a)
  2. {
  3. std::cout<<"The value of the Function : " << F(a);
  4. }

and the main function :
  1. int main()
  2. {
  3. //Base obj;
  4. Derived obj;
  5. obj.F(1.0);
  6. return 0;
  7. }

I'm getting an error :
error C3861: 'F': identifier not found
So how to solve this problem.
With Warm Regards
sdmahapatra
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Problem in c++

 
0
  #2
Jul 20th, 2009
You have to end your class declarations with a semicolon.

EDIT:: Oops! This post probably doesn't make much sense
Last edited by tux4life; Jul 20th, 2009 at 8:07 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 149
Reputation: necrolin is on a distinguished road 
Solved Threads: 14
necrolin's Avatar
necrolin necrolin is offline Offline
Junior Poster

Re: Problem in c++

 
0
  #3
Jul 20th, 2009
Do you specifically need to use the base class instead of the derived class? I was taught that you derive classes to add functionality to a base class. After which you use the derived class instead of the base class whenever you know that you will need the extra methods in the derived class. You can call the methods in the base class from the derived class using: Base::method().
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 32
Reputation: sdmahapatra is an unknown quantity at this point 
Solved Threads: 0
sdmahapatra sdmahapatra is offline Offline
Light Poster

Re: Problem in c++

 
0
  #4
Jul 20th, 2009
I know this is a correct error but I need to call a derived class function into the base class function.Is it possible any how?? or I have to go different way like template etc..
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Problem in c++

 
0
  #5
Jul 20th, 2009
>I know this is a correct error but I need to call a derived class function into the base class function.Is it possible any how??
Can't you create an object of the derived class inside the base class, and then call one of it's methods?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Problem in c++

 
1
  #6
Jul 20th, 2009
I don't understand your objective behing calling derive funciton in base at all, I understand C++ is multi paradigm language, but I can't see any logical reasoning behind this, could you please elloborate more.. so we can provide you better alternative than this, or correct any mistakes whatsoever
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Problem in c++

 
0
  #7
Jul 20th, 2009
Originally Posted by Laiq Ahmed View Post
I don't understand your objective behing calling derive funciton in base at all, I understand C++ is multi paradigm language, but I can't see any logical reasoning behind this, could you please elloborate more.. so we can provide you better alternative than this, or correct any mistakes whatsoever
Some people just get assignments which aren't logical.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem in c++

 
0
  #8
Jul 20th, 2009
sdmahapatra,
Definition of showdata must be:
  1. void Base::showdata(double a)
  2. {
  3. std::cout<<"The value of the Function : " << a;
  4. }
and definition of main will be,
  1. int main()
  2. {
  3. //Base obj;
  4. Derived obj;
  5. obj.showdata(obj.F(1.0));
  6. return 0;
  7. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Problem in c++

 
1
  #9
Jul 20th, 2009
You really need to go down to the basics of inheritance - why we use it.
When you construct a base class, it is not really a base class but it is 'just a class'. It is when you use that class to derive a child class, the former becomes a base class.
The base class knows none about its derived class (because at the time of writing base class, the programmer didn't knew(or suppose to know) that there would be some derived class D which would be inherited from this class.
Such anomalous demands indicate a major flaw in design.
I agree with Laiq in that you should re-factor your design.
Think before you code or else you will be in deep _ _ _ _.
Last edited by siddhant3s; Jul 20th, 2009 at 10:08 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Problem in c++

 
0
  #10
Jul 20th, 2009
I have a function under Base class and at the definition tine of this base class function I need to call another function which is define under the Derived class. how to do this??
Make the method virtual and protected in the base class, then override it as public in the derived class:
  1. class Base
  2. {
  3. public:
  4. void showdata(double a)
  5. {
  6. cout<<"The value of the Function : " << F(a);
  7. }
  8. protected:
  9. virtual double F(double) { return 0; }
  10. };
  11.  
  12. class Derived: public Base
  13. {
  14. public:
  15. double F(double x)
  16. {
  17. return exp(-x) + x*x;
  18. }
  19. };
That solves the problem of a base class needing to call a derived class method, but your example does not show why you need that behavior. What are you trying to do?
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC