009继承与派生.docx

上传人:b****1 文档编号:13347631 上传时间:2023-06-13 格式:DOCX 页数:47 大小:24.54KB
下载 相关 举报
009继承与派生.docx_第1页
第1页 / 共47页
009继承与派生.docx_第2页
第2页 / 共47页
009继承与派生.docx_第3页
第3页 / 共47页
009继承与派生.docx_第4页
第4页 / 共47页
009继承与派生.docx_第5页
第5页 / 共47页
009继承与派生.docx_第6页
第6页 / 共47页
009继承与派生.docx_第7页
第7页 / 共47页
009继承与派生.docx_第8页
第8页 / 共47页
009继承与派生.docx_第9页
第9页 / 共47页
009继承与派生.docx_第10页
第10页 / 共47页
009继承与派生.docx_第11页
第11页 / 共47页
009继承与派生.docx_第12页
第12页 / 共47页
009继承与派生.docx_第13页
第13页 / 共47页
009继承与派生.docx_第14页
第14页 / 共47页
009继承与派生.docx_第15页
第15页 / 共47页
009继承与派生.docx_第16页
第16页 / 共47页
009继承与派生.docx_第17页
第17页 / 共47页
009继承与派生.docx_第18页
第18页 / 共47页
009继承与派生.docx_第19页
第19页 / 共47页
009继承与派生.docx_第20页
第20页 / 共47页
亲,该文档总共47页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

009继承与派生.docx

《009继承与派生.docx》由会员分享,可在线阅读,更多相关《009继承与派生.docx(47页珍藏版)》请在冰点文库上搜索。

009继承与派生.docx

009继承与派生

继承与派生

类的继承是指新的类从已有的类获得已有的特性。

它较好地解决了代码重用问题。

已有的类叫基类或父类,产生的新类叫派生类或子类。

继承派生是两个相对的概念,继承是从子类的角度讲,而派生是从父类的角度讲。

一个子类只有一个直接父类,叫单继承。

一个子类同时有多个父类,叫多继承。

一、单继承派生类的声明

格式:

class派生类名:

继承方式基类名

{

派生类新增的数据成员和函数成员;

};

其中继承方式有:

private私有继承,protected受保护继承,public公有继承

二、基类成员在派生类中的访问属性

基类中的属性

继承方式

派生类中的属性

Private

Private

不可直接访问

Private

public

不可直接访问

Private

Protected

不可直接访问

public

Private

Private

public

public

public

public

Protected

Protected

Protected

Private

Private

Protected

public

Protected

Protected

Protected

Protected

三、派生类对基类成员的访问规则

1.内部访问:

由派生类中新增成员对基类继承来的成员的访问。

2.对象访问:

在派生类的外部,通过派生类的对象对从基类继承来的成员进行访问。

四、访问实例

#include"iostream.h"

classbase

{

private:

inta;

protected:

intb;

public:

voidinab(intx,inty)

{a=x;b=y;}

voidoutab()

{cout<<"a="<

};

classsub:

privatebase

{

private:

intc;

public:

//voidinab(intx,inty,intz)子类的方法与基类重名,子类方法覆盖基类方法

voidinabc(intx,inty,intz)

{//a=x;b=y;c=z;

//base:

:

inab(x,y);

inab(x,y);

c=z;}

//voidoutab()

voidoutabc()

{//cout<<"a="<

//base:

:

outab();

outab();

cout<<"c="<

}

};

main()

{

subs;

s.inabc(1,2,3);

s.outabc();

}

1.私有继承的访问:

例:

#include“iostream.h”

classbase

{

public:

voidsetx(intn)

{x=n;}

voidshowx()

{cout<

private:

intx;

};

classsub:

privatebase

{

public:

voidsetxy(intn,intm)

{

setx(n);//合法

y=m;

}

voidshowxy()

{

cout<

cout<

}

private:

inty;

};

main()

{

subobj;

obj.setx(10);//非法

obj.showx();//非法

obj.setxy(20,30);

obj.showxy();

}

例:

书桌价格计算:

假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。

#include“iostream.h”

classdesk

{

public:

desk(){}

desk(intn,intloc,floatlen,floatw);

voidinit(intn,intloc,floatlen,floatw);

floatcaculate();

voidshow();

private:

intnumber,locker;

floatlength,width,cost;

};

desk:

:

desk(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

voiddesk:

:

init(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

floatdesk:

:

caculate()

{

cost=200;

if(locker!

=0)

cost+=locker*10;

if(length*width>1.8)

cost+=50;

returncost;

}

voiddesk:

:

show()

{

cout<<”书桌的费用是:

”<

”<

}

classpeachdesk:

privatedesk

{

public:

voidpeachinit(intn,intloc,floatlen,floatw)

{init(n,loc,len,w);}

voidshowpeach()

{cout<<”这张桃木桌的价格是:

”<

//此处如果要输出订单号,基类中应该有一返回订单号的公有成员

};

main()

{

intn,loc;

floatlen,w;

cout<<”输入订单号、抽屉数量、长度、宽度:

”<

cin>>n>>loc>>len>>w;

peachdeskpd;

pd.peachinit(n,loc,len,w);

pd.showpeach();

}

例:

描述人类,生成新的学生类,初始化然后输出。

#include“iostream.h”

#include“string.h”

classperson

{

public:

person(){}

person(char*n,chars,inta);

~person();

voidinit(char*n,chars,inta);

voidshow();

private:

char*name;

charsex;

intage;

};

person:

:

person(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

person:

:

~person()

{

delete[]name;

}

voidperson:

:

init(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

voidperson:

:

show()

{

cout<

if(sex==’f’||sex==’F’)

cout<<”女”;

else

cout<<”男”;

cout<

”<

}

classstudent:

privateperson

{

public:

voidstuinit(intc,char*n,chars,inta);

voidstushow();

private:

intcode;

};

voidstudent:

:

stuinit(intc,char*n,chars,inta)

{

code=c;

init(n,s,a);

}

voidstudent:

:

stushow()

{

cout<

show();

}

main()

{

intc,a;

charn[10],s;//定义为char*n可以吗?

cout<<”输入学生的学号、姓名、性别(.f.or.m.)、年龄:

\n”;

cin>>c>>n>>s>>a;

students1;

s1.stuinit(c,n,s,a);

s1.stushow();

}

总结:

私有继承的访问规则

基类成员

private

public

protected

内部访问

不可访问

可访问

可访问

对象访问

不可访问

不可访问

不可访问

2.公有继承的访问

例:

#include“iostream.h”

classbase

{

public:

voidsetxy(intn,intm)

{x=n;y=m;}

voidshowxy()

{cout<<”x的值是:

”<

cout<<”y的值是:

”<

}

private:

intx;

protected:

inty;

};

classsub:

publicbase

{

public:

voidsetxyz(intn,intm,intl)

{

setxy(n,m);//合法

z=l;

}

voidshowxyz()

{

cout<<”x的值是:

”<

cout<<”y的值是:

”<

showxy();

cout<<”z的值是:

”<

}

private:

intz;

};

main()

{

subobj;

obj.setxy(1,2);//合法

obj.showxy();//合法

obj.setxyz(10,20,30);

obj.showxyz();

}

例:

书桌价格计算:

假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。

#include“iostream.h”

classdesk

{

public:

desk(){}

desk(intn,intloc,floatlen,floatw);

voidinit(intn,intloc,floatlen,floatw);

floatcaculate();

voidshow();

private:

intnumber,locker;

floatlength,width,cost;

};

desk:

:

desk(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

voiddesk:

:

init(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

floatdesk:

:

caculate()

{

cost=200;

if(locker!

=0)

cost+=locker*10;

if(length*width>1.8)

cost+=50;

returncost;

}

voiddesk:

:

show()

{

cout<<”书桌的费用是:

”<

”<

}

classpeachdesk:

publicdesk

{

public:

voidpeachinit(intn,intloc,floatlen,floatw)

{init(n,loc,len,w);}

voidshowpeach()

{cout<<”这张桃木桌的价格是:

”<

//此处如果要输出订单号,基类中应该有一返回订单号的公有成员

};

main()

{

intn,loc;

floatlen,w;

cout<<”输入订单号、抽屉数量、长度、宽度:

”<

cin>>n>>loc>>len>>w;

peachdeskpd;

pd.init(n,loc,len,w);

pd.show();

pd.peachinit(n,loc,len,w);

pd.showpeach();

}

例:

描述人类,生成新的学生类,初始化然后输出。

#include“iostream.h”

#include“string.h”

classperson

{

public:

person(){}

person(char*n,chars,inta);

~person();

voidinit(char*n,chars,inta);

voidshow();

private:

char*name;

charsex;

intage;

};

person:

:

person(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

voidperson:

:

init(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

voidperson:

:

show()

{

cout<

if(sex==’f’||sex==’F’)

cout<<”女”;

else

cout<<”男”;

cout<

”<

}

classstudent:

publicperson

{

public:

voidstuinit(intc,char*n,chars,inta);

voidstushow();

private:

intcode;

};

voidstudent:

:

stuinit(intc,char*n,chars,inta)

{

code=c;

init(n,s,a);

}

voidstudents:

:

stushow()

{

cout<

show();

}

main()

{

intc,a;

char*n,s;

cout<<”输入学生的学号、姓名、性别(.f.or.m.)、年龄:

\n”;

cin>>c>>n>>s>>a;

students1;

s1.init(n,s,a);

s1.show();

s1.stuinit(c,n,s,a);

s1.stushow();

}

总结:

公有继承的访问规则

基类成员

private

public

protected

内部访问

不可访问

可访问

可访问

对象访问

不可访问

可访问

不可访问

3.受保护继承的访问

#include“iostream.h”

classbase

{

public:

intz;

voidsetx(inti)

{x=i;}

intgetx()

{returnx;}

private:

intx;

protected:

inty;

};

classsub:

protectedbase

{

public:

intp;

voidsetall(inta,intb,intc,intd,inte,intf);

voidshow();

private:

intm;

protected:

intn;

};

voidsub:

:

setall(inta,intb,intc,intd,inte,intf)

{

x=a;//非法改为setx(a);

y=b;//合法

z=c;//合法

m=d;

n=e;

p=f;

}

voidsub:

:

show()

{

cout<<”x=”<

cout<<”y=”<

cout<<”z=”<

cout<<”m=”<

cout<<”n=”<

}

main()

{

subobj;

obj.setx(333);//非法

obj.setall(1,2,3,4,5,6);//合法

obj.show();

cout<<”p=”<

}

例:

书桌价格计算:

假设所有书桌计费都有的属性是订单号书桌长度和宽度、抽屉,书桌计费方式最低成本200元,长宽超过1.8平米则增加50元,每个抽屉增加10元;如果是桃木,增加50元,橡木,增加125元。

(改)

#include“iostream.h”

classdesk

{

public:

desk(){}

desk(intn,intloc,floatlen,floatw);

voidinit(intn,intloc,floatlen,floatw);

floatcaculate();

voidshow();

private:

intnumber,locker;

floatlength,width,cost;

};

desk:

:

desk(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

voiddesk:

:

init(intn,intloc,floatlen,floatw)

{

number=n;

locker=loc;

length=len;

width=w;

}

floatdesk:

:

caculate()

{

cost=200;

if(locker!

=0)

cost+=locker*10;

if(length*width>1.8)

cost+=50;

returncost;

}

voiddesk:

:

show()

{

cout<<”书桌的费用是:

”<

”<

}

classpeachdesk:

publicdesk

{

public:

voidpeachinit(intn,intloc,floatlen,floatw)

{init(n,loc,len,w);}

voidshowpeach()

{cout<<”这张桃木桌的价格是:

”<

//此处如果要输出订单号,基类中应该有一返回订单号的公有成员

};

main()

{

intn,loc;

floatlen,w;

cout<<”输入订单号、抽屉数量、长度、宽度:

”<

cin>>n>>loc>>len>>w;

peachdeskpd;

pd.init(n,loc,len,w);

pd.show();

pd.peachinit(n,loc,len,w);

pd.showpeach();

}

例:

描述人类,生成新的学生类,初始化然后输出。

(改)

#include“iostream.h”

#include“string.h”

classperson

{

public:

person(){}

person(char*n,chars,inta);

~person();

voidinit(char*n,chars,inta);

voidshow();

private:

char*name;

charsex;

intage;

};

person:

:

person(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

voidperson:

:

init(char*n,chars,inta)

{

name=newchar[strlen(n)+1];

strcpy(name,n);

sex=s;

age=a;

}

voidperson:

:

show()

{

cout<

if(sex==’f’||sex==’F’)

cout<<”女”;

else

cout<<”男”;

cout<

”<

}

classstudent:

publicperson

{

public:

voidstuinit(intc,char*n,chars,inta);

voidstushow();

private:

intcode;

};

voidstudent:

:

stuinit(intc,char*n,chars,inta)

{

code=c;

init(n,s,a);

}

voidstudents:

:

stushow()

{

cout<

show();

}

main()

{

intc,a;

char*n,s;

cout<<”输入学生的学号、姓名、性别(.f.or.m.)、年龄:

\n”;

cin>>c>>n>>s>>a;

students1;

s1.init(n,s,a);

s1.show();

s1.stuinit(c,n,s,a);

s1.stushow();

}

总结:

私有继承的访问规则

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2