#include
#include
#pragma warning(disable:4996)
using namespace std;
class Person
{
protected:
char name[10];
char sex;
public:
Person(const char name[], char sex) :sex(sex)
{
strcpy(this->name, name);
}
};
class DoubleArray
{
protected:
double score;
public:
DoubleArray(double score) :score(score) {}
DoubleArray(DoubleArray &a)
{
score=a.score;
}
void print() const
{
cout << score;
}
int operator-()// 重载取反运算符 operator-,使Student类中的cheat函数可正确运行
{
return -score;
}
};
class Student : public Person
{
private:
int id;
char name[20];
DoubleArray score;
public:
static int count;
~Student()
{
count--;
}
void cheat(DoubleArray& ds)
{
score = -ds;
} // 若作弊,则分数为负
Student(Student &s,DoubleArray& sc);
Student(int id, const char* name, char sex, DoubleArray& sc);
Student(int id, const char* name, char sex, double ds);
void disp();
};
Student::Student(int id, const char* name, char sex, DoubleArray& sc):Person(name,sex),score(sc)
{
count+=1;
}
Student::Student(int id, const char* name, char sex, double ds):Person(name,sex),score(ds)
{
count+=1;
}
void Student::disp()
{
cout<<"(id, name, sex, score)="<<"("<<id<<","<<name<<","<<sex<<",";
score.print();
cout<<")"<<endl;
}
// 类外实现Student类的成员函数,令main函数得以正确运行并输出相应的结果
int main()
{
cout << "count=" << Student::count << endl;
DoubleArray sc(97);
Student s[3]={Student(1,"Ann",'F',sc),Student(2,"Mike",'M',79),Student(3,"Lucy",'F',sc)};
s[2].cheat(sc);
for (int i = 0; i < 3; i++)
s[i].disp();
cout << "count=" << Student::count << endl;
return 0;
}
解决办法:删除DoubleArray的拷贝构造函数。
同Xcode警告 !No matching constructor for initialization of 'Student'同问啊!为什么说** 没有用于初始化“Student”的匹配构造函数**
(宁也是大车埋土学校的?
btw说一个小问题你的两个student构造函数id没初始化 id(id)
在student的类里加一个构造函数
Student ():Person("",'M'),score(0.0){}