//---------main.cpp------------
#include <iostream>
#include <string>
//继承
using namespace std;
class Student
{
private:
string name;
int semesterHours ;
double average;
public:
Student(string pName = "noName") //构造函数,并添加默认值
{
name = pName;
semesterHours= 0;
average = 0;
}
void addCourse(int hours,double grade)
{
double totalGrade = (semesterHours*average + grade); //总分
semesterHours = semesterHours + hours; //总修学时
average = semesterHours ? totalGrade/semesterHours : 0; //平均分
}
void display()
{
std::cout<<"name is : "<<name/*添加头文件<string>*/<<" ,hours = "<<semesterHours<<" average = "<<average<<endl;
}
int getHours(){return semesterHours;}
double getAverage(){return average;}
};
class Advisor
{
int noOfMeeting;
};
class GraduateStudent : public Student //继承父类 Student
{
private:
Advisor advisor; //导师
int qualifierGrade; //资格考试分
public:
int getQualifier(){return qualifierGrade;}
};
int main(int argc,char **argv)
{
Student ds("Jack undergraduate"); //大学生对象 ds
GraduateStudent gs; //研究生对象 gs
//----------------------------
ds.addCourse(3,2.5);
ds.display();
//----------------------------
gs.addCourse(3,3.0);
gs.display();
return 0;
}
name is : Jack undergraduate ,hours = 3 average = 0.833333
name is : noName ,hours = 3 average = 1
请按任意键继续. . .
GraduateStudent gs;
Student s(gs) ; //OK,将gs中的Student对象实体部分,复制给s
Student &t = gs; // 将t作为gs中的Student对象实体的别名
Student *p = &gs; //
1