大学计算机程序设计8《继承》_第1页
大学计算机程序设计8《继承》_第2页
大学计算机程序设计8《继承》_第3页
大学计算机程序设计8《继承》_第4页
大学计算机程序设计8《继承》_第5页
已阅读5页,还剩83页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第8章继承不具有对称性不是所有植物都属于蕨类传递性

高等植物、蕨类植物、芒萁都是植物,具有植物的共同特征

植物低等植物高等植物藻类菌类地衣类苔藓类蕨类裸子类被子类地钱小金发草毛蕨芒萁杉木柏木荔枝橘子

重用&认识习惯继承是面向对象程序设计实现软件重用的重要方法。程序员可以在已有基类的基础上定义新的派生类。

构造与杂交多继承的派生类有多个基类。

访问控制派生类对基类成员的访问由继承方式和成员性质决定。要点

8.1类之间的关系

8.2基类和派生类

8.3基类的初始化

8.4继承的应用实例

8.5多继承8.1类之间的关系has-A,uses-A和is-Ahas-A

包含关系,用以描述一个类由多个“部件类”构成。实现has-A关系用类成员表示,即一个类中的数据成员是另一种已经定义的类。uses-A

一个类部分地使用另一个类。通过类之间成员函数的相互联系,定义友元或对象参数传递实现。is-A

机制称为“继承”。关系具有传递性,不具有对称性。8.1类之间的关系一个B类继承A类,或称从类A派生类B

类A称为基类(父类),类B称为派生类(子类)AB1B2C1C2C3B1,B2的基类A的派生类C1,C2,C3的基类A的派生类(单继承)C3的基类B1,B2的派生类(多继承)B1的派生类类继承关系的语法形式

class派生类名

:基类名表

{

数据成员和成员函数声明

};8.2基类和派生类基类名表构成

访问控制基类名1,访问控制基类名2

,…,访问控制基类名n类继承关系的语法形式

class派生类名

:基类名表

{

数据成员和成员函数声明

};8.2基类和派生类基类名表

构成

访问控制基类名1,访问控制基类名2

,…,访问控制基类名n访问控制

表示派生类对基类的继承方式,使用关键字:

public

公有继承

private

私有继承

protected

保护继承派生类对基类成员的使用,与继承访问控制和基类中成员性质有关公有继承 基类的公有成员派生类的公有成员 基类的保护成员派生类的保护成员私有继承基类的公有成员和保护成员派生类的私有成员保护继承基类的公有成员和保护成员派生类的保护成员不论种方式继承基类,派生类都不能直接使用基类的私有成员

8.2.1访问控制8.2.1访问控制8.2.1访问控制1.公有继承public成员 public成员protected成员 protected成员private成员public成员protected成员private成员基类派生类#include<iostream>usingnamespacestd;classA{public:voidget_XY(){cout<<"Entertwonumbersofx,y:";cin>>x>>y;}voidput_XY(){cout<<"x="<<x<<",y="<<y<<'\n';}protected:intx,y;};classB:publicA{public:

int

get_S(){returns;};voidmake_S(){s=x*y;}; //使用基类数据成员x,yprotected:ints;};classC:publicB{public:voidget_H(){cout<<"Enteranumberofh:";cin>>h;}

int

get_V(){returnv;}voidmake_V(){make_S();v=get_S()*h;} //使用基类成员函数

protected:inth,v;};例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAxy8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;

BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAxyobjB.sobjB.yobjB.xobjBs8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;

CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBxysobjC.hobjC.vobjC.sobjC.yobjC.xobjChv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjC对objA

的数据成员操作xyshv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjC调用基类A成员函数对objB

的数据成员操作xyshv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjC调用派生类B成员函数对objB

的数据成员操作xyshv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjC调用基类A成员函数对objC

的数据成员操作xyshv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjC调用派生类C成员函数对objC

的数据成员操作xyshv8.2.1访问控制例8-1公有继承的测试

intmain(){AobjA;BobjB;CobjC;

cout<<"Itisobject_A:\n";

objA.get_XY();

objA.put_XY();

cout<<"Itisobject_B:\n";

objB.get_XY();

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;

cout<<"Itisobject_C:\n";

objC.get_XY();

objC.get_H();

objC.make_V();

cout<<"V="<<objC.get_V()<<endl;}objA.yobjA.xobjAobjB.sobjB.yobjB.xobjBobjC.hobjC.vobjC.sobjC.yobjC.xobjCxyshv8.2.1访问控制例8-1公有继承的测试

8.2.1访问控制public成员protected成员private成员public成员protected成员private成员基类派生类private成员private成员2.私有继承#include<iostream>usingnamespacestd;classA{public:voidget_XY(){cout<<"Entertwonumbersofxandy:";cin>>x>>y;}voidput_XY(){cout<<"x="<<x<<",y="<<y<<'\n';}protected:intx,y;};classB

:privateA{public:

int

get_S(){returns;}voidmake_S(){get_XY();s=x*y;} private: ints;};intmain(){BobjB;

cout<<"Itisobject_B:\n";

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;}例8-2私有继承的测试

classB:privateAclassAobjB.sobjB.yobjB.xobjBprivate成员8.2.1访问控制#include<iostream>usingnamespacestd;classA{public:voidget_XY(){cout<<"Entertwonumbersofxandy:";cin>>x>>y;}voidput_XY(){cout<<"x="<<x<<",y="<<y<<'\n';}protected:intx,y;};classB

:privateA{public:

int

get_S(){returns;}voidmake_S(){get_XY();s=x*y;} private: ints;};intmain(){BobjB;

cout<<"Itisobject_B:\n";

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;}classB:privateAclassA调用基类成员函数objB.sobjB.yobjB.xobjBprivate成员8.2.1访问控制例8-2私有继承的测试#include<iostream>usingnamespacestd;classA{public:voidget_XY(){cout<<"Entertwonumbersofxandy:";cin>>x>>y;}voidput_XY(){cout<<"x="<<x<<",y="<<y<<'\n';}protected:intx,y;};classB

:privateA{public:

int

get_S(){returns;}voidmake_S(){get_XY();s=x*y

;} private: ints;};intmain(){BobjB;

cout<<"Itisobject_B:\n";

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;}classB:privateAclassA访问私有数据成员objB.sobjB.yobjB.xobjBprivate成员8.2.1访问控制例8-2私有继承的测试#include<iostream>usingnamespacestd;classA{public:voidget_XY(){cout<<"Entertwonumbersofxandy:";cin>>x>>y;}voidput_XY(){cout<<"x="<<x<<",y="<<y<<'\n';}protected:intx,y;};classB:privateA{public:

int

get_S(){returns;}voidmake_S(){get_XY();s=x*y;} private: ints;};intmain(){BobjB;

cout<<"Itisobject_B:\n";

objB.make_S();

cout<<"S="<<objB.get_S()<<endl;}classB:privateAclassAobjB.sobjB.yobjB.xobjBprivate成员8.2.1访问控制例8-2私有继承的测试8.2.1访问控制public成员protected成员private成员public成员protected成员private成员基类派生类protected成员protected成员3.保护继承访问申明使得派生类中不可见成员变为可访问语法基类名::成员名注意访问声明仅调整名字的访问权限不可说明为任何类型不允许降低或者提升基类成员可访问性对重载函数同名重载函数–都起作用不同访问域重载函数–不能派生类和基类重名–不可

派生类定义了与基类同名的成员,在派生类中访问同名成员时屏蔽了基类的同名成员在派生类中使用基类的同名成员,显式地使用类名限定符:类名

::成员

8.2.2重名成员8.2.2重名成员例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;d.base::b=2;d.b=3;d.c=4;};8.2.2重名成员1.重名数据成员baseabderived ab b c

例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;d.base::b=2;d.b=3;d.c=4;};8.2.2重名成员1.重名数据成员baseabderived

dderived ab b c

例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;

d.a=1;d.base::b=2;d.b=3;d.c=4;};8.2.2重名成员baseab1derived

dderived ab b c

访问从base类继承的数据成员a1.重名数据成员例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;

d.base::b=2;d.b=3;d.c=4;};8.2.2重名成员访问从base类继承的bbaseab21derived

dderived ab b c

1.重名数据成员例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;d.base::b=2;

d.b=3;d.c=4;};8.2.2重名成员访问derived类定义的数据成员bbaseab321derived

dderived ab b c

1.重名数据成员例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;d.base::b=2;d.b=3;

d.c=4;};8.2.2重名成员访问derived类定义的数据成员cbaseab4321derived

dderived ab b c

1.重名数据成员例:classbase{public:

inta,b;};classderived:publicbase{public:

intb,c;};voidf(){derivedd;d.a=1;d.base::b=2;d.b=3;d.c=4;};8.2.2重名成员baseab4321derived

dderived ab b c

基类成员的作用域延伸到所有派生类派生类的重名成员屏蔽基类的同名成员1.重名数据成员#include<iostream> //例8-4usingnamespacestd;classA{public:

inta1,a2;A(inti1=0,inti2=0){a1=i1;a2=i2;}voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}};classB:publicA{public:

intb1,b2;B(intj1=1,intj2=1){b1=j1;b2=j2;}voidprint() //定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}voidprintAB(){A::print(); //派生类对象调用基类版本同名成员函数

print(); //派生类对象调用自身的成员函数

}};intmain(){Bb;b.A::print(); b.printAB();}8.2.2重名成员2.重名成员函数voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}voidprint()

//定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}#include<iostream> //例8-4usingnamespacestd;classA{public:

inta1,a2;A(inti1=0,inti2=0){a1=i1;a2=i2;}voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}};classB:publicA{public:

intb1,b2;B(intj1=1,intj2=1){b1=j1;b2=j2;}voidprint() //定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}voidprintAB(){A::print(); //派生类对象调用基类版本同名成员函数

print();

//派生类对象调用自身的成员函数

}};intmain(){Bb;b.A::print(); b.printAB();}8.2.2重名成员2.重名成员函数voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}voidprint()

//定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}派生类屏蔽基类同名成员函数调用自身的成员函数#include<iostream> //例8-4usingnamespacestd;classA{public:

inta1,a2;A(inti1=0,inti2=0){a1=i1;a2=i2;}voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}};classB:publicA{public:

intb1,b2;B(intj1=1,intj2=1){b1=j1;b2=j2;}voidprint() //定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}voidprintAB(){A::print();

//派生类对象调用基类版本同名成员函数

print();

//派生类对象调用自身的成员函数

}};intmain(){Bb;b.A::print();

b.printAB();}8.2.2重名成员2.重名成员函数voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}voidprint()

//定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}派生类对象调用从基类继承的同名成员函数#include<iostream> //例8-4usingnamespacestd;classA{public:

inta1,a2;A(inti1=0,inti2=0){a1=i1;a2=i2;}voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}};classB:publicA{public:

intb1,b2;B(intj1=1,intj2=1){b1=j1;b2=j2;}voidprint() //定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}voidprintAB(){A::print();

//派生类对象调用基类版本同名成员函数

print();

//派生类对象调用自身的成员函数

}};intmain(){Bb;b.A::print();

b.printAB();}8.2.2重名成员2.重名成员函数voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}voidprint()

//定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}基类this指针指向派生类对象#include<iostream> //例8-4usingnamespacestd;classA{public:

inta1,a2;A(inti1=0,inti2=0){a1=i1;a2=i2;}voidprint(){cout<<"a1="<<a1<<'\t'<<"a2="<<a2<<endl;}};classB:publicA{public:

intb1,b2;B(intj1=1,intj2=1){b1=j1;b2=j2;}voidprint() //定义同名函数

{cout<<"b1="<<b1<<'\t'<<"b2="<<b2<<endl;}voidprintAB(){A::print(); //派生类对象调用基类版本同名成员函数

print(); //派生类对象调用自身的成员函数

}};intmain(){Bb;b.A::print(); b.printAB();}8.2.2重名成员2.重名成员函数8.2.3派生类中访问静态成员

基类定义的静态成员,将被所有派生类共享根据静态成员自身的访问特性和派生类的继承方式,在类层次体系中具有不同的访问性质

派生类中访问静态成员,用以下形式显式说明: 类名

::成员

或通过对象访问

对象名

.成员8.2.3派生类中访问静态成员#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}classBclassD:privateB#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}staticvoidAdd(){i++;}静态成员函数#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}staticinti;静态数据成员int

B::i=0;#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}i=5;i是类D的私有静态数据成员类中可见#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}Add();Add()是类D的私有静态成员函数类中可调用#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}B::i++;B::Add();访问B类的静态成员#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}cout<<"statici="<<B::i<<endl;访问B类的静态数据成员#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}cout<<"statici="<<x.i<<endl;通过B类对象访问静态数据成员#include<iostream>usingnamespacestd;classB{public:

staticvoidAdd(){i++;}

staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;//cout<<"statici="<<y.i<<endl;}//cout<<"statici="<<y.i<<endl;错误,i是类D的私有静态数据成员#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf(){i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;//cout<<"statici="<<y.i<<endl;}#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:voidf();{i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;//cout<<"statici="<<y.i<<endl;}#include<iostream>usingnamespacestd;classB{public:staticvoidAdd(){i++;}staticinti;voidout(){cout<<"statici="<<i<<endl;}};int

B::i=0;classD:privateB{public:

voidf();{i=5;Add();

B::i++;

B::Add();}};//例8-5在派生类中访问静态成员intmain(){Bx;Dy;

x.Add();

x.out();

y.f();

cout<<"statici="<<B::i<<endl;

cout<<"statici="<<x.i<<endl;

//cout<<"statici="<<y.i<<endl;}8.3基类的初始化

建立一个类层次后,通常创建某个派生类的对象,包括使用基类的数据和函数

C++提供一种机制,在创建派生类对象时用指定参数调用基类的构造函数来初始化派生类继承基类的数据

派生类构造函数声明为派生类构造函数(变元表):基类(变元表),对象成员1(变元表) …对象成员n(变元表

);构造函数执行顺序:基类对象成员派生类//例8-6调用构造函数顺序测试,构造函数无参数#include<iostream>usingnamespacestd;classBase{public:Base(){cout<<"Basecreated.\n";}};classD_class:publicBase{public:D_class(){cout<<"D_classcreated.\n";}};intmain(){D_classd1;}8.3基类的初始化//例8-6调用构造函数顺序测试,构造函数无参数#include<iostream>usingnamespacestd;classBase{public:Base(){cout<<"Basecreated.\n";}};classD_class:publicBase{public:D_class(){cout<<"D_classcreated.\n";}};intmain(){D_classd1;}8.3基类的初始化//例8-6调用构造函数顺序测试,构造函数无参数#include<iostream>usingnamespacestd;classBase{public:Base(){cout<<"Basecreated.\n";}};classD_class:publicBase{public:D_class(){cout<<"D_classcreated.\n";}};intmain(){D_classd1;}8.3基类的初始化①调用基类构造函数//例8-6调用构造函数顺序测试,构造函数无参数#include<iostream>usingnamespacestd;classBase{public:Base(){cout<<"Basecreated.\n";}};classD_class:publicBase{public:D_class(){cout<<"D_classcreated.\n";}};intmain(){D_classd1;}8.3基类的初始化②调用派生类构造函数例8-8考察一个点、圆、圆柱体的层次结构8.4继承的应用实例PointCircleCylinderclassPoint{friendostream&operator<<(ostream&,constPoint&);public:Point(int=0,int=0); //带默认参数的构造函数

voidsetPoint(int,int); //对点坐标数据赋值

int

getX()const{returnx;} int

getY()const{returny;}protected:intx,y; //Point类的数据成员};classCircle:publicPoint{friendostream&operator<<(ostream&,constCircle&); //友元函数

public:

Circle(doubler=0.0,intx=0,inty=0); //构造函数

voidsetRadius(double);/*置半径*/doublegetRadius()const;/*返回半径*/doublearea()const; //返回面积

protected:doubleradius; //数据成员,半径};classCylinder:publicCircle{friendostream&operator<<(ostream&,constCylinder&);//友元函数

public:

Cylinder(doubleh=0.0,doubler=0.0,intx=0,inty=0);//构造函数

voidsetHeight(double);/*置高度值*/doublegetHeight()const; /*返回高度值*/doublearea()const; /*返回面积*/doublevolume()const; /*返回体积*/protected:doubleheight; //数据成员,高度};8.4继承的应用实例classPoint{friendostream&operator<<(ostream&,constPoint&);public:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论