构造函数和析构函数,拷贝构造,赋值重载,调用练习



#include<iostream>

using namespace std;
class Demo
{
private:
	int dwint;
public:
	Demo(int i=0):dwint(i)
	{
		cout<<"default constructor called !"<<endl;
	}
	Demo(const Demo& d )
	{
		dwint=d.dwint;
		cout<<"copy constructor called !"<<endl;
	}
	~Demo()
	{
		cout<<"desconstructor called !"<<endl;
	}
	const Demo& operator=(const Demo& a )
	{
		this->dwint=a.dwint;
		cout<<"assignment operator used"<<endl;
		return *this;
	}
};
Demo func(Demo d)
{
	Demo demo;
	demo=d;
	return demo;
}
int main()
{
	Demo a(4);
	{
		Demo b;
		b=func(a);
	}
	Demo c=a;
	return 0;
}


构造函数和析构函数,拷贝构造,赋值重载,调用练习_第1张图片


你可能感兴趣的:(构造函数)