Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In base class i am having one function say f().The same function i am having in derived class (ie)f().Now i am going to call the function f() in main means which function get called first either in base class or in derived class?
Posted

1 solution

If depends on the declaration of the function and how you call the function.

Let BaseClass be the base class and DerivedClass is the derived class.

Then in the main function:

BaseClass b;
DerivedClass d;

b.f(); // Calls base class f().
d.f(); // Calls derived class f().


Now if function f() is declared virtual in the base class then:

BaseClass b;
DerivedClass d;

b.f(); // Calls base class f().
d.f(); // Calls derived class f().

BaseClass* bp;

bp = &b;
bp->f() // Call base class f().

bp = &d;
bp->f(); // Calls derived class f().


This is basic c++ and any book should give you this information.

-Saurabh
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900