Solved

polymorphism ?

Posted on 2003-10-27
3
283 Views
Last Modified: 2010-04-02
Hi C++ Experts,

   I have a code that I don't quiet understand :
 ---------------------------------------------
class Base{
 public :
   virtual int f(int i){ cout << " Base  f()" << endl;}
} ;

class Derived: public Base{
 public:
  Derived(){ f(0);}
  int f(char *s){ cout << "Derived f()" << endl ;}
};
int main()
{
  Derived d ;
  return 0 ;
}
------------------------------
In the main code is executed, it prints : "Derived f()". However, I feel
kind of wierd ... because when f(0) is called in the constructor,
the object d actually hasn't been constructed yet... then how can it call the f(0) in the derived class ? not in the base class ?

In any case, is this a polymorphism ? if not, what's the reason that it calls the f() in the derived class ?

Thanks a lot !!!


--------------------------
0
Comment
Question by:meow00
3 Comments
 
LVL 7

Assisted Solution

by:burcarpat
burcarpat earned 20 total points
ID: 9628545
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9628548
The compiler will look for the closest methods possible before going up to the base class.
Derived() { Base::f(0); } would call the other f(int) in base. (Note that 0 is like a NULL address for char*)
What you're doing is just normal function overloading. virtual int f(int) in base is not related to int f(char*) in Derived.
0
 
LVL 6

Accepted Solution

by:
GaryFx earned 30 total points
ID: 9628742
> the object d actually hasn't been constructed yet

Actually, it has - at least enough for its own methods to be called.  By the time you get to the body of the constructor, all of the base class constructors have been called, and all of the constructors for the member variables have been called.  The presumption is that you know what you're doing inside your constructor, and that you'll only call methods that you know are safe to call at this point.  The body of the constructor is allowed to call any method of the object.

This is important for a common pattern, in which there's a separate initialize method that is called both by the constructor and by other methods that need to reinitialize the object.  Since the constructor cannot be called explicitly by other methods, the only way to achieve this is partition some of the initialization code into a separate method and have both the constructor and higher level methods call the initialization code.

Gary
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now