Link to home
Start Free TrialLog in
Avatar of Anjeneya Murthy
Anjeneya MurthyFlag for India

asked on

Need help to understand the internal working of this C++ code

Hi,
This is basic C++ question. Want to understand the internals of what is going on in this code!
// BasicSampleCPPTest01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class base
{
public:
	virtual void show(int i = 7)
	{
		cout<<"Value of I in base: "<< i<<endl;
	}

};
class derived: public base
{
public:
	virtual void show(int i = 2)
	{
		cout<<"Value of I in derived: "<< i<<endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	base *obj = new derived();
	obj->show();
	return 0;
}

Open in new window


The output for this is 7. But I thought the output will be 2. can someone explain me how exactly this works? if you have seen some links please share.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anjeneya Murthy

ASKER

Thanks a lot Zoppo for Quick response. Do you know of any links that explains this?
Hm - unfortunateley I couldn't find where this is specified, but if you i.e. google for something like default value virtual function c++ you can find a lot of articles/discussion about this topic.