1. 程式人生 > >建構函式形參的預設值

建構函式形參的預設值

#include <iostream>  
using namespace std;  

class A{
private:
	int n;
	const double d;
public:
	A(int n=0, double d=9.0):n(n),d(d)
	{
		cout << "call A(int n)" << this << endl;
	}
	
	void show();
};

void A::show()
{
	cout << "n=" << n << "d=" << d << endl;
}

int main()
{
	A a1;
	A a2(200);
	A a3(300, 500);
	
	a1.show();
	a2.show();
	a3.show();
	
	return 0;
}