| | |
Problem in c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 32
Reputation:
Solved Threads: 0
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
Define the function below :
and the main function :
I'm getting an error :
So how to solve this problem.
With Warm Regards
sdmahapatra
My class declaration is below
C++ Syntax (Toggle Plain Text)
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 :
C++ Syntax (Toggle Plain Text)
void Base::showdata(double a) { std::cout<<"The value of the Function : " << F(a); }
and the main function :
C++ Syntax (Toggle Plain Text)
int main() { //Base obj; Derived obj; obj.F(1.0); return 0; }
I'm getting an error :
•
•
•
•
error C3861: 'F': identifier not found
With Warm Regards
sdmahapatra
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??
Can't you create an object of the derived class inside the base class, and then call one of it's methods?
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."
•
•
•
•
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
sdmahapatra,
Definition of showdata must be:
and definition of main will be,
Definition of showdata must be:
C++ Syntax (Toggle Plain Text)
void Base::showdata(double a) { std::cout<<"The value of the Function : " << a; }
C++ Syntax (Toggle Plain Text)
int main() { //Base obj; Derived obj; obj.showdata(obj.F(1.0)); return 0; }
Failure is not fatal, but failure to change might be. - John Wooden
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 _ _ _ _.
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
(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
•
•
•
•
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??
C++ Syntax (Toggle Plain Text)
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; } };
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- Installing Windows 98 On VMware. Floppy problem (Windows 95 / 98 / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Cellphones, PDAs and Handheld Devices)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Implementing C++ classes
- Next Thread: First step in reading a text file and converting it to a binary file
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






