943,640 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 679
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 20th, 2009
0

Problem in c++

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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 :

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

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

I'm getting an error :
Quote ...
error C3861: 'F': identifier not found
So how to solve this problem.
With Warm Regards
sdmahapatra
Similar Threads
Reputation Points: 19
Solved Threads: 0
Light Poster
sdmahapatra is offline Offline
32 posts
since May 2009
Jul 20th, 2009
0

Re: Problem in c++

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 20th, 2009
0

Re: Problem in c++

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().
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Jul 20th, 2009
0

Re: Problem in c++

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..
Reputation Points: 19
Solved Threads: 0
Light Poster
sdmahapatra is offline Offline
32 posts
since May 2009
Jul 20th, 2009
0

Re: Problem in c++

>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?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 20th, 2009
1

Re: Problem in c++

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
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Jul 20th, 2009
0

Re: Problem in c++

Click to Expand / Collapse  Quote originally posted by Laiq Ahmed ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 20th, 2009
0

Re: Problem in c++

sdmahapatra,
Definition of showdata must be:
C++ Syntax (Toggle Plain Text)
  1. void Base::showdata(double a)
  2. {
  3. std::cout<<"The value of the Function : " << a;
  4. }
and definition of main will be,
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. //Base obj;
  4. Derived obj;
  5. obj.showdata(obj.F(1.0));
  6. return 0;
  7. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 20th, 2009
1

Re: Problem in c++

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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jul 20th, 2009
0

Re: Problem in c++

Quote ...
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:
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Implementing C++ classes
Next Thread in C++ Forum Timeline: First step in reading a text file and converting it to a binary file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC