您的位置:首页 > 编程语言 > C语言/C++

【c++程序】拷贝构造函数

2015-10-16 21:17 344 查看
/********************************************************/
/**************---About   析构函数-------**************/
/**************---Author: Tibruce Zhao-----**************/
/**************---Date:   2015/10/15------***************/
/********************************************************/

#include<iostream>
using namespace std;

class F
{
public:
F(int n=0,int d=1);//默认构造函数
~F();
/*拷贝构造函数,如果没有写拷贝构造函数,编译器会为每个类自动写一个,
*完成所有成员的逐个复制。*/
F(const F &f):n(f.n-1),d(f.d+1)//初始化不给值,默认0;
{cout<<this<<"F(F f}"<<endl;}
void print(bool newline=true);
void print(bool newline=true)const;//构成重载
void input();
private:
int n;
int d;
};
F::F(int n,int d):n(n),d(d)//成员变量初始值
{
cout<<this<<",F("<<n<<','<<d<<")"<<endl;
}
F::~F()
{
cout<<"~F()"<<n<<'/'<<d<<endl;
}
void F::print(bool newline) const
{
cout<<"ONLY READ"<<n<<'/'<<d;
if(newline) cout<<endl;
}
void F::print(bool newline)
{
cout<<"FREE"<<n<<'/'<<d;
if(newline) cout<<endl;
}
F func(F x)
{
return x;
}
int main()
{
F a(3,5);
cout<<"******************************"<<endl;
func(a);
cout<<"******************************"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: