C++类和多态性的使用

2025-01-07 05:48:20
推荐回答(1个)
回答1:

#include
double zong;

class student
{
public:
student(double z):zong(z){};

double y(double xuefei)
{
zong=xuefei;
return(zong);
};

double y(double shuben,double zhusu)
{
zong=shuben+zhusu;
return(zong);
};

double y(double xuefei,double zhusu,double shuben)
{
zong=xuefei+zhusu+shuben;
return(zong);
};

double y(double xuefei,double zhusu,double shuben,double buzhu)
{
zong=xuefei+zhusu+shuben-buzhu;
return(zong);
}

private:
double zong;
};

class yanjiusheng:public student
{
public:
yanjiusheng(double a,double b):zhusu(a),shuben(b),student(a+b){};
private:
double zhusu;
double shuben;
};

class zhuankesheng:public student //专科生不是研究生
{
public:
zhuankesheng(double a):student(a){};
};

class benkesheng:public student //本科生不是专科生
{
public:
benkesheng(double a,double b):buzhu(b),student(a) //能用初始化的地方不用赋值
{};
private:
double buzhu;
};

void main()
{
yanjiusheng fei1(1000,1000);
zhuankesheng fei2(1000);
benkesheng fei3(1000,1000);
cout <<"yan jiu sheng de fei yong shi:"<cout <<"zhuan ke sheng de fei yong shi:"<cout <<"ben ke sheng de fei yong shi:"<}

//不要滥用继承和派生
//以后,你会发现它们真正的用途
//在C++中对象是不具有面向对象编程的性质的(包括:多态性),
//想要使用面向对象编程,必须使用引用或指针。
//非虚普通成员函数是不具有多态性的,必须使用虚函数。
……
//这方面要注意的还有很多。