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
Can we create object of interface?
What are constructors in oop?
Whats is abstraction in oops?
Why do we need polymorphism in c#?
What is solid in oops?
How do you use inheritance in unity?
What is class and example?
What is object in oops?
What is the difference between abstraction and polymorphism?
Can we define a class within the interface?
What is abstraction oop?
Can a varargs method be overloaded?
What is polymorphism what are the different types of polymorphism?
Where You Can Use Interface in your Project
What is byval and byref? What are differences between them?