class CourseScore{
private:
int score1;
int score2;
int score3;
CourseScore(){//构造函数1,初始化各科成绩为-1
score1=-1;
score2=-1;
score3=-1;
}
CourseScore(int s1,int s2,int s3){//构造函数2,初始化各科成绩
score1=s1;
score2=s2;
score3=s3;
}
~CourseScore(){//析构函数
}
public://获取各科成绩
int getScore1()const{
cout<<"score1="<<score1<<endl;
return score1;
}
int getScore2()const{
cout<<"score2="<<score2<<endl;
return score2;
}
int getScore3()const{
cout<<"score3="<<score3<<endl;
return score3;
}
public://设置各科成绩
void setScore1(int score){
score1=score;
}
void setScore2(int score){
score2=score;
}
void setScore3(int score){
score3=score;
}
public://打印平均成绩
double printAvgScore(){
int flag=0;
double avg;
if(score1==-1){
cout<<"Error,score1 is not initialized!"<<endl;
flag=1;
}
if(score2==-1){
cout<<"Error,score2 is not initialized!"<<endl;
flag=1;
}
if(score3==-1){
cout<<"Error,score3 is not initialized!"<<endl;
flag=1;
}
if(flag==0){//如果没有任何科目分数为-1(即所有科目都赋值了)
avg=(score1+score2+score3)/3;
cout<<"The average score of the courses is:"<<avg<<endl;
return avg;
}
}
};
//函数都直接内联了,没有在外面定义。