How to call a non virtual function in the derived class by
using base class pointer

Answers were Sorted based on User's Feedback



How to call a non virtual function in the derived class by using base class pointer..

Answer / 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

How to call a non virtual function in the derived class by using base class pointer..

Answer / annu agrawal

This can be done by using 'base' keyword in C# language. For eg;

An abstract class is coded as follows:

abstract class AbstractDemo
{
public abstract void Habits();

public virtual void hello()
{
Console.WriteLine("Hello DerievedAbstract class,
hello()");
}

public void adddet()
{
Console.WriteLine("Hello Everybody...");
}
}

Then, I have created a derieved class of this class as:

class DerievedAbstract : AbstractDemo
{
public override void Habits()
{
Console.WriteLine("Hello DerievedAbstract class");
}

public override void hello()
{
base.adddet();
Console.WriteLine("Hello() is a function....");
}

new public void adddet()
{
Console.WriteLine("Hello Everybody....derieved
class");
}
}

In the Main() function, the object of Derieved class is
created as:

class classmain
{
public static void Main(string[] args)
{
DerievedAbstract ab = new DerievedAbstract();
ab.Habits();
ab.hello();
Console.ReadLine();
}
}

Is This Answer Correct ?    1 Yes 2 No

How to call a non virtual function in the derived class by using base class pointer..

Answer / aaa

Typecast the base pointer to derived class pointer type and
then invoke the derived class's function.

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More OOPS Interview Questions

program for insertion ,deletion,sorting in double link list

0 Answers  


class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash

0 Answers  


WRITE A SIMPLE C++ PROGRAM TO SWAP TWO NOS WITHOUT USING TEMP

2 Answers  


What is command routing in MFC

1 Answers   GE,


How to deploy web appliction in web logic ?

1 Answers   Unisys,






Why is it so that we can have virtual constructors but we cannot have virtual destructors?

2 Answers  


can inline function declare in private part of class?

1 Answers  


what is oops

4 Answers   NIIT,


Difference between new operator and operator new

2 Answers  


2. Give the different notations for the class.\

0 Answers  


What is public, protected, private?

6 Answers   IBS, Satyam,


How to overload new operator in c++

2 Answers  


Categories