DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Puzzles && Algorithms
*****************************************************
#Post#: 22--------------------------------------------------
Output of the C++ program
By: ravu Date: August 15, 2012, 11:18 am
---------------------------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 0 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
virtual void fun ( int x )
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1;
Base *bp = &d1;
bp->fun();
return 0;
}
#Post#: 23--------------------------------------------------
Re: Output of the C++ program
By: dinesh Date: August 16, 2012, 9:51 am
---------------------------------------------------------
since the base class pointer is pointing to a derived class
object and the function is virtual, the derived class method
will be invoked but initialization is done by using base class
value at compile time.
For more details:
HTML https://www.securecoding.cert.org/confluence/display/cplusplus/OOP04-CPP.+Prefer+not+to+give+virtual+functions+default+argument+initializers
#Post#: 57--------------------------------------------------
Re: Output of the C++ program
By: kpr29 Date: August 25, 2012, 3:51 am
---------------------------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 0)
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Derived : public Base
{
public:
virtual void fun ( int x = 10 ) // NOTE THIS CHANGE
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1;
Base *bp = &d1;
bp->fun();
return 0;
}
What is the output of the above program. I want the answer first
then expalnation.
*****************************************************