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

What is an example of genetic polymorphism?

0 Answers  


What is a friend function & its advantage?

2 Answers   TCS,


Where You Can Use Interface in your Project

0 Answers   KPIT,


What is inheritance and how many types of inheritance?

0 Answers  


What is oops in programming?

0 Answers  






me get an assignent n its question is this 1.creat a set as in math i.ea={1,2} 2.insert element in it3. delete element don,t repeat any element 4.union 5. intersection of two sets plz help me i always pray for u n send me at ayeshawzd@hotmail.com f u have c++ how to program 5th addition then it is the 10.9 question in 10th chapter exercise

1 Answers  


What is oops?what is its use in software engineering?

0 Answers  


What are the four main oops concepts?

1 Answers  


What do you mean by binding of data and functions?

3 Answers  


write a program in c++ to overload the function add (s1,s2) where s1 and s2 are integers and floating point values.

4 Answers  


What are objects in oop?

0 Answers  


WAP find square root of any number (without using sqrt() )?

3 Answers  


Categories