What will be displayed on the screen?
class A1
{
public:
       virtual void k(){std::cout<<"A1";}
};


class B1: public A1
{
public:
    void k(){std::cout<<"B1";}
};


void function(A1 a)
{
    a.k();
}

int main()
{
    B1 b;
    function(b);
}
Explanation
If you pass an object to a function by value, we pass a copy of the value object, and this is called a copy constructor. As a parameter to a function is an object of class A1, then the copy constructor is invoked for the A1, therefore the copy will be an object of class A1. If the object was passed by value and by reference then volunteered would function k() of class B1, because it is declared virtual.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes