4_9.cpp
来自「10个比较经典的C++程序。初学者就先多学习学习别人吧。」· C++ 代码 · 共 18 行
CPP
18 行
#include <iostream>
using namespace std;
class Ratio
{ int num, den;
public:
Ratio(int n=0, int d=1) : num(n), den(d) {cout << "Common constructor called\n"; }
//拷贝构造函数
Ratio(const Ratio& r):num(r.num), den(r.den) {cout<< "Copy constructor called\n"; }
void disp() {cout <<num<<"/"<<den<<endl;}
};
int main()
{ Ratio x(20,7);
cout<<"Ratio x is:"; x.disp();
Ratio y(x); //调用拷贝构造函数,用x初始化y
cout<<"Ratio y is:"; y.disp();
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?