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

You have to end your class declarations with a semicolon.

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

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

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

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

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

Comments
So true, So true

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.

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;
}

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

Comments
Nice explanation :)

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?

Some people just get assignments which aren't logical.

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

Attachments Spock.png 104.37 KB

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

Multiple dispatch ideom

.


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

This article has been dead for over six months. Start a new discussion instead.