#include<iostream>
using namespace std;
class exam{
public:
void set_exam(int a,int b)
{
m=a;
n=b;
}
void show()
{
cout<<m<<" "<<n<<" "<<endl;
}
private:
int m;
int n;
};
int main()
{
exam a[2]; //调用默认构造函数,下同
exam *p;
a[0].set_exam(90,80);
a[1].set_exam(20,30);
p=a;
p->show();
p++;
p->show();
return 0;
}
此处不可以定义带有参数的构造函数,如果要定义,必须再加上不带参数的构造定义,如:
#include<iostream>
using namespace std;
class exam{
public:
exam(int a,int b)
{
a=m;
b=n;
}
void set_exam(int a,int b)
{
m=a;
n=b;
}
exam()
{
}
exam(const exam &q)
{
m=q.m;
n=q.n;
}
void show()
{
cout<<m<<" "<<n<<endl;
}
private:
int m;
int n;
};
int main()
{
exam a[2];
exam *p;
a[0].set_exam(90,80);
a[1].set_exam(20,30);
p=a;
p->show();
return 0;
}