📄 +
字号:
【例5.7】 构造函数的定义和执行过程实例程序。
class Test {
private:
int num;
public:
Test();//声明不带参数的构造函数
Test(int);//声明带1个参数的构造函数
};
#include<iostream>
using namespace std;
Test ∷Test(): num(0)//定义不带参数的构造函数
{cout<<″Initializing default″<<endl; }
Test∷Test(int n): num(n)//定义带1个参数的构造函数
{cout<<″Initializing″<<″ ″<<n<<endl;}
void main(){
Test x;//使用不带参数的构造函数产生对象x
Test y(15);//使用带参数的构造函数产生对象y
Test array[2]={5,7};//使用带参数的构造函数产生对象数组array
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -