给类中定义了不带参数的构造函数的对象数组赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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;


}