Question

A virtual function is called using a reference to a base class. Discuss if the function...

A virtual function is called using a reference to a base class. Discuss if the function in base class is called or the function in the derived class and why?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

That's called polymorphism

case 1)
if the base class pointer is pointing to an object of the base class, then the function in the base class is called

case 2)
if the base class pointer is pointing to an object of the derived class, and if this virtual function is not overriden in the derived class, then the function in the base class is called

case 3)
if the base class pointer is pointing to an object of the derived class, and if this virtual function is overriden in the derived class, then the function in the derived class is called

This is how polymorphism works..
Add a comment
Know the answer?
Add Answer to:
A virtual function is called using a reference to a base class. Discuss if the function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 8. Virtual function is class function which expected to be redefined in class, so that when...

    8. Virtual function is class function which expected to be redefined in class, so that when reference is made to derived class object using pointer then we can call virtual function to execute class definition version. [3] a) Base, derived, derived b) Derived, Derived, Derived c) Base, derived, base d) Base, base, derived

  • Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...

    Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...

  • Suppose the base class and the derived class each have a member function with the same...

    Suppose the base class and the derived class each have a member function with the same signature. When you have a pointer to a base class object and call a function member through the pointer, discuss what determines which function is actually called—the base class member function or the derived-class function.

  • If a derived class does not contain an overridden function for a pure virtual function in...

    If a derived class does not contain an overridden function for a pure virtual function in the base class, then objects of that derived class will not be allowed to be declared. Select one: O True O False

  • problem, you will implement a no virtual base class template using the Curiously Recurring Template Pattern...

    problem, you will implement a no virtual base class template using the Curiously Recurring Template Pattern n this or CRTP Your task is to define the Comparable template, implementing the following functions in terms of the Derived class operator<: operator operator operator operator operator Additionally, each function should be noexcept and const-qualified. Please use C++. And also I want a answer implementint the functions in term of the Derived class operator<. There is a question on this website before but...

  • {C++} Create a base class called Person with a public member function called sayHi which outputs...

    {C++} Create a base class called Person with a public member function called sayHi which outputs “Hi, I am a person.” Create a derived class called Student which inherits from the Person class (public inheritance) and overwrites the sayHi function and outputs “Hi, I am a student.” Create another derived class called EngineeringStudent which inherits from the Student class (public inheritance) and overwrites the sayHi function and outputs “Hi, I am an engineering student.” In the main program, create a...

  • This is In C++ only Thank you 2. Employee is a base class and Hourly Worker...

    This is In C++ only Thank you 2. Employee is a base class and Hourly Worker is a derived class. Which statement about the destructors of both classes is true? al They should be declared virtual u Declaring them virtual or not does not make any difference c) They should be implemented as friend functions d) The destructor of HourlyWorker must be virtual whereas the destructor of Employee should 3. If a base class has a non-virtual member function called...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • (T/F) ______ A base class pointer/reference may contain the address of an object of the base...

    (T/F) ______ A base class pointer/reference may contain the address of an object of the base class or an object of classes derived from the base class.

  • A) Suppose you have a class called base which has been inherited by a class called...

    A) Suppose you have a class called base which has been inherited by a class called derived.     ( both classes have public, protected and private members )     What can a client of derived access within the class base ?     What can the class derived access within the class base ? B) Suppose you have a class called more which has a private object of a class called simple. ( both classes have public, protected and private members...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT