954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem in c++

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

class Base{

public:

void showdata(double);

};

class Derived : public Base{

public:
	inline double F(double x)
{
	return exp(-x) + x*x;
}


Define the function below :

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


and the main function :

int main()
{
//Base obj;
Derived obj;
obj.F(1.0);
return 0;
}


I'm getting an error :error C3861: 'F': identifier not found
So how to solve this problem.
With Warm Regards
sdmahapatra

sdmahapatra
Light Poster
32 posts since May 2009
Reputation Points: 19
Solved Threads: 0
 

You have to end your class declarations with a semicolon.

EDIT:: Oops! This post probably doesn't make much sense :(

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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().

necrolin
Posting Whiz in Training
225 posts since Jun 2009
Reputation Points: 105
Solved Threads: 26
 

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..

sdmahapatra
Light Poster
32 posts since May 2009
Reputation Points: 19
Solved Threads: 0
 

>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?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 
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.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

sdmahapatra,
Definition of showdata must be:

void Base::showdata(double a)
{
	std::cout<<"The value of the Function : " << a;
}

and definition of main will be,

int main()
{
//Base obj;
Derived obj;
obj.showdata(obj.F(1.0));
return 0;
}
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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 _ _ _ _.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 
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:

class Base
{
public:
    void showdata(double a)
    {
        cout<<"The value of the Function : " << F(a);
    }
protected:
    virtual double F(double) { return 0; }
};

class Derived: public Base
{
public:
    double F(double x)
    {
        return exp(-x) + x*x;
    }
};

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?

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
Some people just get assignments which aren't logical.

Sorry, I couldn't resist. :icon_rolleyes: Spock.pngThat is illogical captain.

Attachments Spock.png 104.37KB
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

tom Gun

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??

As per the OOP paradigm strickly speaking in OOP, whenever you need the default behavior in Generic Type & want to specialize the behavior in derived you use the polymorphic behavior. Or if you just wanted to delegate the implementation on derive classes you can do so the concept of pure virtual function in that scenario generic type cannot define the implementation perfect example is shape heirarchy in that Draw () = 0 must be pure virtual and derived classes triangle, square etc should implement that.

well I look this query as a try to acheive[Quote] Multiple dispatch ideom[Quote].


Well I can write article on that, but try to reuse the existing one over the internet :) hope this helps

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You