#include<iostream>
using namespace std;
class exam
{
int x;
public:
exam()//不带参数的构造函数
{x=3;}
exam(int n)//带参数的构造函数
{x=n;}
int getx()
{return x;}
};
void main()
{ exam op(2);//调用带参数的构造函数
cout<<op.getx()<<endl;
exam ob[4];//调用不带参数的构造函数 数组中对象的x值都为3
cout<<ob[3].getx()<<endl;
}
给类中定义了不带参数的构造函数的对象数组赋值
最新推荐文章于 2023-04-07 23:20:19 发布