How to call a non virtual function in the derived class by
using base class pointer
Answer Posted / ak
It's simple.
Since in question it is asked how to call "non virtual
function in derived class" which means in derived class we
need to access non-virtual function using Base Class's pointer.
Note:
In question its no where mentioned that we cannot use
virtual function in Base class.
So in Base class same function can be made virtual and we
can use it through Base's pointer.
See eg. below:
class Base
{
public:
virtual void fun()
{
cout<<"Inside Base's fun";
}
};
class Derived : public Base
{
public:
void fun()
{
cout<<"Inside Derived's fun";
}
};
int main()
{
Base *bp = new Derived;
bp->fun();
getch();
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
What are the benefits of interface?
Explain the advantages of inheritance.
write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.
What makes a language oop?
What polymorphism means?
What is polymorphism explain its types?
What are the benefits of oop?
What is difference between multiple inheritance and multilevel inheritance?
any one please tell me the purpose of operator overloading
INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?
How to hide the base class functionality in Inheritance?
Why do pointers exist?
What is object and class in oops?
They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?
What is the purpose of polymorphism?