类的继承


//---------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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值