009继承与派生
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
继承与派生
类的继承是指新的类从已有的类获得已有的特性。
它较好地解决了代码重用问题。
已有的类叫基类或父类,产生的新类叫派生类或子类。
继承派生是两个相对的概念,继承是从子类的角度讲,而派生是从父类的角度讲。
一个子类只有一个直接父类,叫单继承。
一个子类同时有多个父类,叫多继承。
一、单继承派生类的声明
格式:
class 派生类名: 继承方式基类名
{
派生类新增的数据成员和函数成员;
};
其中继承方式有:private私有继承, protected受保护继承, public公有继承
二、基类成员在派生类中的访问属性
三、派生类对基类成员的访问规则
1.内部访问:由派生类中新增成员对基类继承来的成员的访问。
2.对象访问:在派生类的外部,通过派生类的对象对从基类继承来的成员进行访问。
四、访问实例
#include "iostream.h"
class base
{
private:
int a;
protected:
int b;
public:
void inab(int x,int y)
{a=x;b=y;}
void outab()
{cout<<"a="<<a<<",b="<<b<<endl;}
};
class sub:private base
{
private:
int c;
public:
//void inab(int x,int y,int z)子类的方法与基类重名,子类方法覆盖基类方法
void inabc(int x,int y,int z)
{//a=x;b=y;c=z;
//base::inab(x,y);
inab(x,y);
c=z;}
//void outab()
void outabc()
{//cout<<"a="<<a<<",b="<<b<<",c="<<c<<endl;
// base::outab();
outab();
cout<<"c="<<c<<endl;
}
};
main()
{
sub s;
s.inabc(1,2,3);
s.outabc();
}
1.私有继承的访问:
例:
#inclu de “iostream.h”
class base
{
public:
void setx(int n)
{x=n;}
void showx()
{cout<<x<<endl;}
private:
int x;
};
class sub:private base {
public:
void setxy(int n,int m) {
setx(n); //合法
y=m;
}
void showxy()
{
cout<<x; //非法
cout<<y<<endl;
}
private:
int y;
};
main()
{
sub obj;
obj.setx(10); //非法
obj.showx(); //非法
obj.setxy(20,30);
obj.showxy();
}
例:书桌价格计算:假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。
#include “iostream.h”
class desk
{
public:
desk(){}
desk(int n,int loc,float len,float w);
void init(int n,int loc,float len,float w);
float caculate();
void show();
private:
int number,locker;
float length,width,cost;
};
desk::desk(int n,int loc,float len,float w)
{
number=n;
locker=loc;
length=len;
width=w;
}
void desk::init(int n,int loc,float len,float w)
{
number=n;
locker=loc;
length=len;
width=w;
}
float desk::caculate()
{
cost=200;
if(locker!=0)
cost+=locker*10;
if(length*width>1.8)
cost+=50;
return cost;
}
void desk::show()
{
cout<<”书桌的费用是:”<<cost<<endl;//cout<<number<<”号书桌的
费用是:”<<cost<<endl;
}
class peachdesk:private desk
{
public:
void peachinit(int n,int loc,float len,float w)
{ init( n, loc, len, w);}
void showpeach()
{cout<<”这张桃木桌的价格是:”<< caculate()+50<<endl;} //此处如果要输出订单号,基类中应该有一返回订单号的公有成员
};
main()
{
int n,loc;
float len,w;
cout<<”输入订单号、抽屉数量、长度、宽度:”<<endl;
cin>>n>>loc>>len>>w;
peachdesk pd;
pd.peachinit(n,loc,len,w);
pd.showpeach();
}
例:描述人类,生成新的学生类,初始化然后输出。
#include “iostream.h”
#include “string.h”
class person
{
public:
person(){}
person(char *n,char s,int a);
~person();
void init(char *n,char s,int a); void show();
private:
char *name;
char sex;
int age;
};
person::person(char *n,char s,int a) {
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
person::~person()
{
delete []name;
}
void person::init(char *n,char s,int a)
{
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
void person::show()
{
cout<<name<<”,”;
if(sex==’f’||sex==’F’)
cout<<”女”;
else
cout<<”男”;
cout<<age<<”岁。
”<<endl;
}
class student:private person
{
public:
void stuinit(int c, char *n,char s,int a); void stushow();
private:
int code;
};
void student::stuinit(int c, char *n,char s,int a)
{
code=c;
init(n,s,a);
}
void student::stushow()
{
cout<<code<<”号”;
show();
}
main()
{
int c,a;
char n[10],s;//定义为char *n可以吗?
cout<<”输入学生的学号、姓名、性别(.f. or .m.)、年龄:\n”; cin>>c>>n>>s>>a;
student s1;
s1.stuinit(c,n,s,a);
s1.stushow();
}
总结:私有继承的访问规则
2.公有继承的访问
例:
#include “iostream.h”
class base
{
public:
void setxy(int n,int m)
{x=n;y=m;}
void showxy()
{cout<<”x的值是:”<<x<<endl;
cout<<”y的值是:”<<y<<endl;
}
private:
int x;
protected:
int y;
};
class sub:public base
{
public:
void setxyz(int n,int m,int l)
{
setxy(n,m); //合法
z=l;
}
void showxyz()
{
cout<<”x的值是:”<<x<<endl; //非法
cout<<”y的值是:”<<y<<endl; //非法可改为:showxy();
cout<<”z的值是:”<<z<<endl;
}
private:
int z;
};
main()
{
sub obj;
obj.setxy(1,2); //合法
obj.showxy(); //合法
obj.setxyz(10,20,30);
obj.showxyz();
}
例:书桌价格计算:假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。
#include “iostream.h”
class desk
{
public:
desk(){}
desk(int n,int loc,float len,float w);
void init(int n,int loc,float len,float w); float caculate();
void show();
private:
int number,locker;
float length,width,cost;
};
desk::desk(int n,int loc,float len,float w)
{
number=n;
locker=loc;
length=len;
width=w;
}
void desk::init(int n,int loc,float len,float w)
{
number=n;
locker=loc;
length=len;
width=w;
}
float desk::caculate()
{
cost=200;
if(locker!=0)
cost+=locker*10;
if(length*width>1.8)
cost+=50;
return cost;
}
void desk::show()
{
cout<<”书桌的费用是:”<<cost<<endl;//cout<<number<<”号书桌的费用是:”<<cost<<endl;
}
class peachdesk:public desk
{
public:
void peachinit(int n,int loc,float len,float w)
{ init( n, loc, len, w);}
void showpeach()
{cout<<”这张桃木桌的价格是:”<< caculate()+50<<endl;} //此处如果要输出订单号,基类中应该有一返回订单号的公有成员
};
main()
{
int n,loc;
float len,w;
cout<<”输入订单号、抽屉数量、长度、宽度:”<<endl; cin>>n>>loc>>len>>w;
peachdesk pd;
pd.init(n,loc,len,w);
pd.show();
pd.peachinit(n,loc,len,w);
pd.showpeach();
}
例:描述人类,生成新的学生类,初始化然后输出。
#include “iostream.h”
#include “string.h”
class person
{
public:
person(){}
person(char *n,char s,int a);
~person();
void init(char *n,char s,int a);
void show();
private:
char *name;
char sex;
int age;
};
person::person(char *n,char s,int a) {
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
void person::init(char *n,char s,int a) {
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
void person::show()
{
cout<<name<<”,”;
if(sex==’f’||sex==’F’)
cout<<”女”;
else
cout<<”男”;
cout<<age<<”岁。
”<<endl;
}
class student:public person
{
public:
void stuinit(int c, char *n,char s,int a);
void stushow();
private:
int code;
};
void student::stuinit(int c, char *n,char s,int a)
{
code=c;
init(n,s,a);
}
void students::stushow()
{
cout<<code<<”号”;
show();
}
main()
{
int c,a;
char *n,s;
cout<<”输入学生的学号、姓名、性别(.f. or .m.)、年龄:\n”; cin>>c>>n>>s>>a;
student s1;
s1.init(n,s,a);
s1.show();
s1.stuinit(c,n,s,a); s1.stushow();
}
总结:公有继承的访问规则
3.受保护继承的访问
#include “iostream.h”class base
{
public:
int z;
void setx(int i)
{x=i;}
int getx()
{return x;}
private:
int x;
protected:
int y;
};
class sub:protected base
{
public:
int p;
void setall(int a,int b,int c,int d,int e,int f);
void show();
private:
int m;
protected:
int n;
};
void sub::setall(int a,int b,int c,int d,int e,int f)
{
x=a; //非法改为setx(a);
y=b; //合法
z=c; //合法
m=d;
n=e;
p=f;
}
void sub::show()
{
cout<<”x=”<<x<<endl; //非法改为cout<<”x=”<<getx()<<endl;
cout<<”y=”<<y<<endl; //合法
cout<<”z=”<<z<<endl; //合法
cout<<”m=”<<y<<endl;
cout<<”n=”<<y<<endl;
}
main()
{
sub obj;
obj.setx(333); //非法
obj.setall(1,2,3,4,5,6); //合法
obj.show();
cout<<”p=”<<p<<endl;
}
例:书桌价格计算:假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。
(改) #include “iostream.h”
class desk
{
public:
desk(){}
desk(int n,int loc,float len,float w);
void init(int n,int loc,float len,float w);
float caculate();
void show();
private:
int number,locker;
float length,width,cost;
};
desk::desk(int n,int loc,float len,float w)
{
number=n;
locker=loc;
length=len;
width=w;
}
void desk::init(int n,int loc,float len,float w) {
number=n;
locker=loc;
length=len;
width=w;
}
float desk::caculate()
{
cost=200;
if(locker!=0)
cost+=locker*10;
if(length*width>1.8)
cost+=50;
return cost;
}
void desk::show()
{
cout<<”书桌的费用是:”<<cost<<endl;//cout<<number<<”号书桌的费用是:”<<cost<<endl;
}
class peachdesk:public desk
{
public:
void peachinit(int n,int loc,float len,float w)
{ init( n, loc, len, w);}
void showpeach()
{cout<<”这张桃木桌的价格是:”<< caculate()+50<<endl;} //此处如果要输出订单号,基类中应该有一返回订单号的公有成员
};
main()
{
int n,loc;
float len,w;
cout<<”输入订单号、抽屉数量、长度、宽度:”<<endl;
cin>>n>>loc>>len>>w;
peachdesk pd;
pd.init(n,loc,len,w);
pd.show();
pd.peachinit(n,loc,len,w);
pd.showpeach();
}
例:描述人类,生成新的学生类,初始化然后输出。
(改)#include “iostream.h”
#include “string.h”
class person
{
public:
person(){}
person(char *n,char s,int a);
~person();
void init(char *n,char s,int a);
void show();
private:
char *name;
char sex;
int age;
};
person::person(char *n,char s,int a)
{
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
void person::init(char *n,char s,int a)
{
name=new char[strlen(n)+1];
strcpy(name,n);
sex=s;
age=a;
}
void person::show()
{
cout<<name<<”,”;
if(sex==’f’||sex==’F’)
cout<<”女”;
else
cout<<”男”;
cout<<age<<”岁。
”<<endl;
}
class student:public person
{
public:
void stuinit(int c, char *n,char s,int a); void stushow();
private:
int code;
};
void student::stuinit(int c, char *n,char s,int a)
{
code=c;
init(n,s,a);
}
void students::stushow()
{
cout<<code<<”号”;
show();
}
main()
{
int c,a;
char *n,s;
cout<<”输入学生的学号、姓名、性别(.f. or .m.)、年龄:\n”; cin>>c>>n>>s>>a;
student s1;
s1.init(n,s,a);
s1.show();
s1.stuinit(c,n,s,a);
s1.stushow();
}
总结:私有继承的访问规则
四、派生类的构造函数和析构函数
1.基类的构造函数和析构函数不会被继承
2.执行顺序
创建对象时:基类构造函数——子类构造函数
撤消对象时:子类析构函数——基类析构函数
3.子类构造函数和析构函数构造规则
当基类构造函数没有参数或没有显式定义构造函数,子类可以不向基类传递参数,甚至可以不定义构造;当基类有带参的构造函数,子类定义构造函数,以提供把参数传递给基类构造函数的途径。
格式:
子类名(参数总表):基类名(参数表)
{//子类新增成员的初始化语句
}
例:基类有带参构造函数时,子类构造函数的构造方法。
#include “iostream.h”
class base
{
public:
base(int n)
{x=n;}
void input(int n)
{x=n;}
int getx()
{return x;}
private:
int x;
};
class sub:public base
{
public:
sub(int a,int b):base(a)//可以传递a ,也可以传递b {y=b;}
void input(int i)
{y=i;}
int gety()
{return y;}
private:
int y;
};
main()
{
sub obj(10,200);
cout<<obj.getx()<<endl;
cout<<obj.gety()<<endl;
}
例:多个层次继承时构造函数的构造规则
#include “iostream.h”
clase first
{
public:
first()
{a=0;b=0;}
first(int x,int y)
{a=x;b=y;}
void print()
{cout<<”\n a=”<<a<<”\t b=”<<b<<endl;} private:
int a,b;
};
clase second:public first
{
public:
second()
{c=0;d=0;}
second():first(1,1)
{c=0;d=0;}
second(int x,int y) :first(x+1,y+1)
{c=x;d=y;}
second(int x,int y,int m,int n):first(m,n)
{c=x;d=y;}
void print()
{first::print();
cout<<”\n c=”<<c<<”\t d=”<<d<<endl;
}
private:
int c,d;
};
class third:public second
{
public:
third()
{e=0;}
third(int x,int y,int z):second(x,y)
{e=z;}
third(int x,int y,int z,int m,int m):second(x,y,m,n) {e=z;}
void printf()
{second:print();
cout<<”\n e=”<<e<<endl;
}
private:
int e;
};
main()
{
first obj0;
obj0.print();
second obj1;
obj1.print();
second obj2(10,20,30,40);
obj2.print();
second obj3(100,200);
obj3.print();
third obj4;
obj4.print();
third obj5(10,20,30);
obj5.print();
third obj6(11,22,33,44,55);
obj6.print();
}
五、多继承
1.什么是多继承:当一个子类有多个基类时,这种派生方法称为多基派生或多继承。
在多继承中,不同继承方式对于基类成员在子类中的可访问性与单继承的规则相同。
2.多继承的格式:
class 派生类名:继承方式1 基类名1,继承方式2 基类名2…… { //派生类新增数据成员和函数成员 };
如果缺省继承方式,默认为private.
例:多继承情况下的访问特性
#include “iostream.h”
class f1
{
public:
void seta(int x)
{a=x;}
void outa()
{cout<<”\n a=”<<a<<endl;} private:
int a;
};
class f2
{
public:
void setb(int y)
{b=y;}
void outb()
{cout<<”\n b=”<<b<<endl;} private:
int b;
};
class s:public f1,private f2
{
public:
void setc(int x,int y)
void outc()
{outb();
cout<<”\n c=”<<c<<endl;
}
private:
int c;
};
main()
{
s obj;
obj.seta(100);
obj.outs();
obj.setb(200);
obj.outb();
obj.setc(88,99);
obj.outc();
}
3.多继承的构造函数和析构函数
格式:
派生类名(参数总表):基类名1(参数表),基类名2(参数表)…… { //派生类新增成员的初始化语句 }
改写上一例。
六、虚基类
1.为什么要引入虚基:
基类中从上一级基类继承来的成员就拥有相同的名称,在派生类的对象中,这些同名成员在内存中同时拥有多个拷贝,对这多个同名拷贝进行分辨的方法一是使用作用域运算符,另一方法就是使用虚基,从而使公共基只产生一个拷贝消除二义性。
2.虚基使用格式
class 子类名:virtual 继承方式基类名 //这个基类就是虚基
{ }
例:
#include "iostream.h"
class base
{
private:
int a;
public:
base(int x)
{a=x;}
void ina(int x)
{a=x;}
void outa()
{cout<<a<<endl;}
};
class sub1:public base
{public:
};
{};
class ssub:public sub1,public sub2 {
};
main()
{
ssub s;
s.ina(123);//访问的对象有多个,报错 s.outa();//访问的对象有多个,报错}
3.虚基的构造
#include "iostream.h"
class base
{
private:
int a;
public:
base(int x)
{a=x;
cout<<"base构造\n";}
void ina(int x)
{a=x;}
void outa()
{cout<<a<<endl;}
class sub1:virtual public base
{public:
sub1(int x):base(x){cout<<"sub1构造\n";}};
class sub2:virtual public base
{public:
sub2(int x):base(x){cout<<"sub2构造\n";}};
class ssub:public sub1,public sub2
{
public:
ssub(int x):sub1(x),sub2(x),base(x){cout<<"ssub构造\n";}//如果不是虚基,此处不能调用base构造,且base被构造两次,虚基则必须调用
};
main()
{
ssub s(123);
//s.ina(123);
s.sub1::outa();
}
七、练习
1.使用派生类的主要原因是:()
A.提高代码的可重用性
B.提高程序的运行效率
C.加强类的封装性
2.在C++中继承方式有几种:()
A.1 B.2 C.3 D.4
3.假设已经定义好了一个类student,现在要定义类derived,它是从student 私有派生的,定义类derived的正确写法是:()
A.clase derived::student private{ //...... };
B.clase derived:student public { //...... };
C.clase derived: private student { //...... };
D.clase derived:: public student{ //...... };
4.根据下面的程序,可以在主程序中使用的合法语句是:
#include<iostream.h>
# include<strinq.h>
class stock
{
public:
void set_price(float p)
{ price=p;}
void print_price()
{ cout<<price<<endl;}
private:
float price;
};
class deri_stock:private stock
{
int code;
void set_code(int c)
{ code +c;}
void print_code()
{ cout<<code<<endl;}
{;
void main()
{
deri_stock sl;
...
}
A.sl.set_price(8.89);
B.sl.code=1001;
C.sl.set_code(1001);
D.sl.print_price();
5.在下面程序中,不能在主程序中使用的语句是:#include <iostream.h>
# include <strinq.h>
class stock
{
public:
void set_price(float p)
{ price=p;}
void print_price()
{ cout<<price<<endl;}
float price;
};
class deri_stock:public stock {
public:
void set_code(int c)
{ code =c;}
void print_code()
{ cout<<code<<endl;} private:
int code;
};
void main()
{
deri_stock sl;
...
}
A.sl.set_price(8.89);
B.sl.code=1001;
C.sl.set_code(1001);
D.sl.print_price();
6.写出下列程序的运行结果。
#include <iostream.h>
# include <strinq.h>
{
public:
person{char *name,int aqe}
{
m_name =new char[strlen(name)+1];
strcpy(m_name,name);
m_aqe = aqe;
cout<<”person Name=”<<m_name<<endl;
}
~person()
{
delete m_name;
}
protected:
char *m_name;
int m_aqe;
};
class Student:public virtual person
{
public:
Student(char *name,int aqe,lona int classnu):person(name,aqe) {
m_slassnu = classnu;
cout<<”Student Name=”<<m_name<<endl;
~Student(){ }
protected:
lonq int m_classnu;
};
class Employee:public virtual person
{
public:
Employee(char *name,int aqe,float waqe):person(name,aqe) {
m_waqe =waqe;
cout<<”Employee Name=”<<m_name<<endl;;
}
~Employee(){ }
protected:
float m waqe;
};
class SideLine:public Student,public Employee
{
public:
SideLine(char *name,int aqe,linq int classnu,\float waqe) :person(name,aqe);
Student(name,aqe,classnu),Employee(name,aqe,waqe)
{
cout<<”SideLine Name=”<<m_name<<endl;
~SideLine(){ }
};
int main()
{
SideLine obj(“Anqel”,20,20000103,3000);
return 0;
}
7.写出下列程序的运行结果。
#include<iostream.h>
class b1{
public:
b1 (int ii)
{ i=ii;}
void display()
{ cout<<i<<end1 }
protected:
int i;
};
class b2 {
public:
b2(int j j)
{ j=j j; }
int qetj()
{ retutn j }
void display()
{ cout<<j<<end1; } protected:
int j;
};
class b3:public b1,pub1ic b2 { public:
b3(int ii,int jj,int kk):b1(ii),b2(jj)
{ k=kk; }
void seta11(int ii,int jj,int kk)
{
i=ii;
j=jj;
k=kk;
}
void display()
{
cout<<i<<end1;
cout<<j<<end1;
cout<<k<<end1;
}
private:
int k
};
void main()
{
b1 o1 (40);
o1.display();
b2 o2(50);
o2.display();
b3 x(10,20,30);
x.display();
x.setal l(40,50,60);
x.display();
}
8.下面的程序可以输出ASCII字符与所对应的数字的对照表,修改下列程序,可以输出字母a~z。
#include<iostream.h>
# include<iomanip.h>
class table{
public:
table(int p)
{ i=p }
void asci i(void);
protected :
int i;
};
void table: :asci i(void)
{
it k=1;
for (;i<127;i++)
{
cout<<setw(4)<<i<<”“<<(char)i;
if ( (k)%12= =0)
cout<<”\n”;
k+ +;
}
cout<<”\n”;
}
class der_table:public table {
:public:
der_table(int p,char *m):table(p) {c=m;}
void print(void);
protected:
char *c;
};
void der_table: :print(void)
{
cout<<”\n”;
table: :adci i();
}
void main()
{
der_table ob1(32,”ASCI I value---char”);
ob1 print();
der_table ob2(…a‟,” ASCI I value---char”);
ob1
ob2. print();
}
提示:修改后的主程序为:
void main()
{
der_table ob(…a‟,‟z‟,” ASCI I value---char”);
ob.print();
}
9.下面的程序包含了Time类和Date类的声明,要求设计一个Birthtime 类,它继承了Time类和Date类,并且还有一项出生孩子的名字Childname,同时设计主程序显示一个小孩的出生时间和名字.
#include<iostream.h>
# include<strinq.h>
class Time {
public:
Time(int h, int m, int s)
{
hours=h;
minutes=m;
seconds=s;
}
virtual void display()
{
cout<<hours<<”:”<<minutes<<”:”<<seconds<<end1;
}
protected:
int hours,minutes,seconds;
};
class Date {
public:
Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
virtual void display()
{
cout<<month<<”/”<<day<<”/”<<year;
}
protected:
int month,day,year;
};
10.已知类Animal的声明如下:如果希望显示结果为:如何编写主程序?class Animal {
public:
Animal()
{ name=NULL;}
Animal (char *n);
~ Animal()
{ delete name; }
virtual void WhoAmI(); protected:
char *name;
};
class Cat:public Animal { public:
Cat():Amimal() { }
Cat(char *n):Animal(n) { }
void WhoAmI();
};
class Tiqer:public Cat {
public:
Tiqer():Cat() { }
Tiqer(char *n):Cat(n) { }
void WhoAmI();
};
Animal: :Animal(char *n)
{
name=new char[strlen(n)+1];
strcpy(name,n);
}
void Animal: :WhoAmI()
{
cout<<”qeneric animal”;
}
void Cat: :WhoAmI()
{
cout<<”I am a cat named ”<<name<<endl;
}
void Tiqer: :WhoAmI()
{
cout<<” I am a Tiqer named ”<<name<<endl;
}
如果希望结果为:
I am a cat named John
I am a Tiqer named Richard
如何编写主程序?
11.建立普通的基类building,用来存储一座楼房的层数、房间数以及它的总平方米数。
建立派生类house,继承building,并存储下面的内容:卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。
12.按照图5-6的类层次图要求编写程序。
定义属于类score的对象c1及类teacher的对象t1,分别输入各为数据成员的值后再显示出这些数据。
13.编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。
小车类car是它的私有派生类其中包含载人数passenger_load。
卡车类truck是vehicle的私有派生类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。
14. 作业:编写一个学生和教师数据输入和显示程序,学生数据要求有编
号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。
要求将编号、姓名的输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类,学生数据中的班号和成绩的输入和显示在student类中实现,教师数据中的职称和部门的输入和显示在teacher类中实现。
最后在主函数中进行该类的测试。
下面给出了基类person的主要成员:
(1)私有成员:
●int no;编号
●char name[10]; 姓名
(2)公有成员:
●void input(); 编号和姓名的输入
●void display(); 编号和姓名的显示
15.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。
circle类包含私有数据成员radius和求圆面积的成员函数getarea();table 类包含私有数据成员height和返回高度的成员函数getheight()。
roundtable 类继承所有上述类的数据成员和成员函数,添加了私有数据成员color和相应的成员函数。