给类中无自定义构造函数的对象数组赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
class exam
{
int x;
public:
void setx(int n)
{x=n;}
int getx()
{return x;}
};
void main()
{
exam ob[4];
int i;
for(i=0;i<4;i++)
ob[i].setx(i);
for(i=0;i<4;i++)
cout<<ob[i].getx()<<endl;
}