例题继承与多态.docx
《例题继承与多态.docx》由会员分享,可在线阅读,更多相关《例题继承与多态.docx(18页珍藏版)》请在冰豆网上搜索。
![例题继承与多态.docx](https://file1.bdocx.com/fileroot1/2023-2/9/5c75b5b6-fb33-42b6-9a82-2f9381d76444/5c75b5b6-fb33-42b6-9a82-2f9381d764441.gif)
例题继承与多态
例1:
继承中的构造函数
#include
classB1//基类B1,构造函数有参数
{
public:
B1(inti){cout<<"constructingB1"<
};
classB2//基类B2,构造函数有参数
{
public:
B2(intj){cout<<"constructingB2"<};
classB3//基类B3,构造函数无参数
{
public:
B3(){cout<<"constructingB3*"<};
classC:
publicB2,publicB1,publicB3//派生新类C
//注意基类名的顺序
{
public:
//派生类的公有成员
C(inta,intb,intc,intd):
B1(a),memberB2(d),memberB1(c),B2(b){}
//注意基类名的个数与顺序
//注意成员对象名的个数与顺序
private:
//派生类的私有对象成员
B1memberB1;
B2memberB2;
B3memberB3;
};
voidmain()
{
Cobj(1,2,3,4);
}
输出结果:
constructingB22
constructingB11
constructingB3*
constructingB13
constructingB24
constructingB3*
Pressanykeytocontinue
例:
2-1虚基类
#include
usingnamespacestd;
classB0//声明基类B0
{intnV;
public:
//外部接口
B0(intn){nV=n;}
voidfun(){cout<<"MemberofB0:
nV="<};
classB1:
publicB0//virtual
{intnV1;
public:
B1(inta,intb):
B0(a){nV1=b;}
voidfun()
{B0:
:
fun();cout<<"MemberofB1:
nV1="<};
classB2:
publicB0//virtual
{intnV2;
public:
B2(inta,intb):
B0(a){nV2=b;}
voidfun()
{B0:
:
fun();cout<<"MemberofB2:
nV2="<};
classD1:
publicB1,publicB2
{intnVd;
public:
D1(inta,intb,intc,intd):
B1(a,b),B2(b,c){nVd=d;}//B0(a),
voidfund(){cout<<"MemberofD1:
nVd="<};
intmain()
{
D1d1(1,2,3,4);
d1.fund();
d1.B1:
:
fun();
d1.B2:
:
fun();
//d1.B0:
:
fun();
return0;
}
无虚基类
MemberofD1:
nVd=4
MemberofB0:
nV=1
MemberofB1:
nV1=2
MemberofB0:
nV=2
MemberofB2:
nV2=3
Pressanykeytocontinue
有虚基类:
MemberofD1:
nVd=4
MemberofB0:
nV=1
MemberofB1:
nV1=2
MemberofB0:
nV=1
MemberofB2:
nV2=3
MemberofB0:
nV=1
Pressanykeytocontinue
例2-2:
虚基类
#include
#include
classperson
{
public:
person(intn,char*strname,chars,char*strfrom)
{cout<id=n;
strcpy(name,strname);
sex=s;
strcpy(from,strfrom);
}
voidshowinfo()
{
cout<"<<"<<"<<"<}
protected:
intid;
charname[10];
charsex;
charfrom[20];
};
classundergraduate:
virtualpublicperson//虚继承
{
public:
undergraduate(intn,char*strname,chars,char*strfrom,floatsc)
:
person(n,strname,s,strfrom)
{
cout<score=sc;
}
voidshowinfo()
{
person:
:
showinfo();
cout<"<}
protected:
floatscore;
};
classteacher:
virtualpublicperson//虚继承
{
public:
teacher(intn,char*strname,chars,char*strfrom,floatsa)
:
person(n,strname,s,strfrom)
{
salary=sa;
cout<}
voidshowinfo()
{
person:
:
showinfo();
cout<"<}
protected:
floatsalary;
};
classgraduate:
publicundergraduate,publicteacher//从两个类继承
{
public:
graduate(intn,char*strname,chars,char*strfrom,floatsc,floatsa)
:
person(n,strname,s,strfrom),undergraduate(2,"jetty",'M',"china",90),
teacher(3,"rose",'F',"American",2000)
{
/*id=n;
strcpy(name,strname);
sex=s;
strcpy(from,strfrom);
score=sc;
salary=sa;*/
}
voidshowinfo()
{
cout<"<<"<<"<<"<<"<<"<}
};
voidmain()
{
graduateg(1,"jetty",'M',"china",95,3000);
g.showinfo();
}
输出结果:
personconstructor
undergraduateconstructor
teacherconstructor
ID:
1
name:
jetty
sex:
M
from:
china
score:
90
salary:
2000Pressanykeytocontinue
例3:
多态性
3-1:
#include
classBase//基类声明
{inti;
public:
//外部接口
Base(inti1){i=i1;}
//virtual
voidprint(){cout<<"i="<
};
classD:
publicBase//公有派生
{charch;
public:
D(charc1,intc2):
Base(c2)
{ch=c1;}
//virtual
voidprint()
{Base:
:
print();cout<<"i="<
};
voidmain()输出结果:
{Base*b1,b2(10);
Dd1(‘A’,100);
b2.print();//i=10i=10
b1=&d1;
d1.print();//i=100ch=Ai=100ch=A
b1->print();//i=100i=100ch=A
}
3--2:
#include
classB0//基类B0声明
{
public:
//外部接口
//virtual
voiddisplay(){cout<<"B0:
:
display()"<};
classB1:
publicB0//公有派生
{
public:
voiddisplay(){cout<<"B1:
:
display()"<};
classD1:
publicB1//公有派生
{
public:
voiddisplay(){cout<<"D1:
:
display()"<};
voidfun(B0*ptr)//普通函数
{
ptr->display();
}
voidmain()//主函数
{
B0b0,*p;//定义基类对象和指针
B1b1;//定义派生类对象
D1d1;//定义派生类对象
p=&b0;
fun(p);//调用基类B0函数成员
p=&b1;
fun(p);//调用派生类B1函数成员
p=&d1;
fun(p);//调用派生类D1函数成员
}
输出结果:
B0:
:
display()
B0:
:
display()
B0:
:
display()
Pressanykeytocontinue
加上虚函数结果:
B0:
:
display()
B1:
:
display()
D1:
:
display()
例4:
实例——公司人员管理
//employee.h
classemployee
{
protected:
char*name;//姓名
intindividualEmpNo;//个人编号
intgrade;//级别
floataccumPay;//月薪总额
staticintemployeeNo;//本公司职员编号目前最大值
public:
employee();//构造函数
~employee();//析构函数
virtualvoidpay()=0;//计算月薪函数(纯虚函数)
virtualvoidpromote(intincrement=0);//升级函数(虚函数)
virtualvoiddisplayStatus()=0;//显示人员信息(纯虚函数)
};
classtechnician:
publicemployee//兼职技术人员类
{
private:
floathourlyRate;//每小时酬金
intworkHours;//当月工作时数
public:
technician();//构造函数
voidpromote(int);//升级函数
voidpay();//计算月薪函数
voiddisplayStatus();//显示人员信息
};
classsalesman:
virtualpublicemployee//兼职推销员类
{
protected:
floatCommRate;//按销售额提取酬金的百分比
floatsales;//当月销售额
public:
salesman();//构造函数
voidpromote(int);//升级函数
voidpay();//计算月薪函数
voiddisplayStatus();//显示人员信息
};
classmanager:
virtualpublicemployee//经理类
{
protected:
floatmonthlyPay;//固定月薪数
public:
manager();//构造函数
voidpromote(int);//升级函数
voidpay();//计算月薪函数
voiddisplayStatus();//显示人员信息
};
classsalesmanager:
publicmanager,publicsalesman//销售经理类
{
public:
salesmanager();//构造函数
voidpromote(int);//升级函数
voidpay();//计算月薪函数
voiddisplayStatus();//显示人员信息
};
//empfunc.cpp
#include
#include
#include"employee.h"
intemployee:
:
employeeNo=1000;//员工编号基数为1000
employee:
:
employee()
{
charnamestr[50];//输入雇员姓名时首先临时存放在namestr中
cout<<"请输下一个入雇员的姓名:
";
cin>>namestr;
name=newchar[strlen(namestr)+1];//动态申请用于存放姓名的内存空间
strcpy(name,namestr);//将临时存放的姓名复制到name
individualEmpNo=employeeNo++;//新输入的员工,其编号为目前最大编号加1
grade=1;//级别初值为1
accumPay=0.0;//月薪总额初值为0
}
employee:
:
~employee()
{
deletename;//在析构函数中删除为存放姓名动态分配的内存空间
}
voidemployee:
:
promote(intincrement)
{
grade+=increment;//升级,提升的级数由increment指定
}
technician:
:
technician()
{
hourlyRate=100;//每小时酬金100元
}
voidtechnician:
:
promote(int)
{
employee:
:
promote
(2);//调用基类的升级函数,升2级
}
voidtechnician:
:
pay()
{
cout<<"请输入"<";
cin>>workHours;
accumPay=hourlyRate*workHours;//计算月薪,按小时计酬
cout<<"兼职技术人员"<<<"本月工资"<}
voidtechnician:
:
displayStatus()
{
cout<<"兼职技术人员"<<<"级别为"<}
salesman:
:
salesman()
{
CommRate=0.04;//销售提成比例4%
}
voidsalesman:
:
promote(int)
{
employee:
:
promote(0);//调用基类的升级函数,升0级
}
voidsalesman:
:
pay()
{
cout<<"请输入"<";
cin>>sales;
accumPay=sales*CommRate;//月薪=销售提成
cout<<"推销员"<<<"本月工资"<}
voidsalesman:
:
displayStatus()
{
cout<<"推销员"<<<"级别为"<}
manager:
:
manager()
{
monthlyPay=8000;//固定月薪8000元
}
voidmanager:
:
promote(int)
{
employee:
:
promote(3);//调用基类的升级函数,升3级
}
voidmanager:
:
pay()
{
accumPay=monthlyPay;//月薪总额即固定月薪数
cout<<"经理"<<<"本月工资"<}
voidmanager:
:
displayStatus()
{
cout<<"经理"<<<"级别为"<}
salesmanager:
:
salesmanager()
{
monthlyPay=5000;
CommRate=0.005;
}
voidsalesmanager:
:
promote(int)
{
employee:
:
promote
(2);//调用基类的升级函数,升2级
}
voidsalesmanager:
:
pay()
{
cout<<"请输入"<:
name<<"所管辖部门本月的销售总额:
";
cin>>sales;
accumPay=monthlyPay+CommRate*sales;//月薪=固定月薪+销售提成
cout<<"销售经理"<<<"本月工资"<}
voidsalesmanager:
:
displayStatus()
{
cout<<"销售经理"<<<"级别为"<}
//8_7.cpp
#include
#include"employee.h"
intmain()
{
managerm1;
techniciant1;
salesmanagersm1;
salesmans1;
employee*emp[4]={&m1,&t1,&sm1,&s1};
//用指针数组的个元素存放各对象的地址
inti;
for(i=0;i<4;i++)
{/*依次调用各派生类对象的成员函数,完成各自不同的升级、
计算月薪、显示信息功能。
*/
emp[i]->promote();
emp[i]->pay();
emp[i]->displayStatus();
}
return0;
}