《七章继承--2.ppt》由会员分享,可在线阅读,更多相关《七章继承--2.ppt(82页珍藏版)》请在三一办公上搜索。
1、7.2.1 访问控制,3.保护继承,派生类定义了与基类同名的成员,在派生类中访问同名成员 时屏蔽了基类的同名成员 在派生类中使用基类的同名成员,显式地使用类名限定符:类名:成员,7.2.2 重名成员,7.2.2 重名成员,例:class base public:int a,b;class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,1.重名数据成员,base a b,derived a b bc,例:class base public:int a,b;
2、class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,1.重名数据成员,base a b,derived a b bc,例:class base public:int a,b;class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,base a b,derived a b bc,访问从base 类继承的数据成
3、员a,1.重名数据成员,例:class base public:int a,b;class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,访问从base 类继承的 b,base a b,derived a b bc,1.重名数据成员,例:class base public:int a,b;class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.
4、c=4;,7.2.2 重名成员,访问derived 类定义的数据成员b,base a b,derived a b bc,1.重名数据成员,例:class base public:int a,b;class derived:public base public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,访问derived 类定义的数据成员c,base a b,derived a b bc,1.重名数据成员,例:class base public:int a,b;class derived:public b
5、ase public:int b,c;void f()derived d;d.a=1;d.base:b=2;d.b=3;d.c=4;,7.2.2 重名成员,base a b,derived a b bc,基类成员的作用域延伸到所有派生类派生类的重名成员屏蔽基类的同名成员,1.重名数据成员,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int
6、j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,void print()cout a1=a1 t a2=a2 endl;,void print()/定义同名函数 cout b1=b1 t b2=b2 endl;,/例7-4#includeclass A public:in
7、t a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重
8、名成员,2.重名成员函数,void print()cout a1=a1 t a2=a2 endl;,void print()/定义同名函数 cout b1=b1 t b2=b2 endl;,派生类屏蔽基类同名成员函数 调用自身的成员函数,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void prin
9、t()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,void print()cout a1=a1 t a2=a2 endl;,void print()/定义同名函数 cout b1=b1 t b2=b2 endl;,派生类对象调用从基类继承的同名成员函数,/例7-4#includeclass A public:int a1,a
10、2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.
11、重名成员函数,void print()cout a1=a1 t a2=a2 endl;,void print()/定义同名函数 cout b1=b1 t b2=b2 endl;,基类this指针指向派生类对象,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cou
12、t b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,i
13、nt j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;cla
14、ss B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2
15、=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:print();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,通过继承,类B具有两个同名成员函数void A
16、:print();void B:print();,/void print(A*this);,/void print(B*this);,/例7-4#includeclass A public:int a1,a2;A(int i1=0,int i2=0)a1=i1;a2=i2;void print()cout a1=a1 t a2=a2 endl;class B:public A public:int b1,b2;B(int j1=1,int j2=1)b1=j1;b2=j2;void print()/定义同名函数 cout b1=b1 t b2=b2 endl;void printAB()A:p
17、rint();/派生类对象调用基类版本同名成员函数 print();/派生类对象调用自身的成员函数;void main()B b;b.A:print();b.printAB();,7.2.2 重名成员,2.重名成员函数,通过继承,类B具有两个同名成员函数void A:print();void B:print();派生类也是基类,基类指针可以指向 派生类对象 派生类中定义与基类同名的成员函数,称为重载成员函数,/void print(A*this);,/void print(B*this);,7.2.3 派生类中的静态成员,基类定义的静态成员,将被所有派生类共享 根据静态成员自身的访问特性和派生
18、类的继承方式,在类层次 体系中具有不同的访问性质 派生类中访问静态成员,用以下形式显式说明:类名:成员 或通过对象访问对象名.成员,7.2.3 派生类中的静态成员,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.out();y.f();coutst
19、atic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,class B,class D:private B,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.out();y.f();cou
20、tstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,static void Add()i+;,静态成员函数,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.out();y.f(
21、);coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,static int i;,静态数据成员,int B:i=0;,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.o
22、ut();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,i=5;,i 是类D的私有静态数据成员类中可见,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.
23、out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,Add();,Add()是类D的私有静态成员函数类中可调用,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.A
24、dd();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,B:i+;B:Add();,访问B类的静态成员,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.
25、Add();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,coutstatic i=B:iendl;,访问B类的静态数据成员,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main(
26、)B x;D y;x.Add();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,coutstatic i=x.iendl;,通过B类对象访问静态数据成员,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静
27、态成员,void main()B x;D y;x.Add();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,/coutstatic i=y.iendl;,错误,i 是类D的私有静态数据成员,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add
28、();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,v
29、oid main()B x;D y;x.Add();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,#includeclass B public:static void Add()i+;static int i;void out()coutstatic i=iendl;int B:i=0;class D:private B public:void f();i=5;Add();B:i+;B:Add();,/例7-5 在派生类中访问静态成员,void main()B x;D y;x.Add
30、();x.out();y.f();coutstatic i=B:iendl;coutstatic i=x.iendl;/coutstatic i=y.iendl;,7.3 基类的初始化,建立一个类层次后,通常创建某个派生类的对象,包括使用基类的数据和函数 C+提供一种机制,在创建派生类对象时用指定参数调用基类的构造函数来初始化派生类继承基类的数据 派生类构造函数声明为派生类构造函数(变元表):基类(变元表),对象成员1(变元表)对象成员n(变元表);构造函数执行顺序:基类 对象成员 派生类,/例7-6 调用构造函数顺序测试,构造函数无参数#include class Base public:B
31、ase()cout Base created.n;class D_class:public Base public:D_class()cout D_class created.n;void main()D_class d1;,7.3 基类的初始化,/例7-6 调用构造函数顺序测试,构造函数无参数#include class Base public:Base()cout Base created.n;class D_class:public Base public:D_class()cout D_class created.n;void main()D_class d1;,7.3 基类的初始化,
32、/例7-6 调用构造函数顺序测试,构造函数无参数#include class Base public:Base()cout Base created.n;class D_class:public Base public:D_class()cout D_class created.n;void main()D_class d1;,调用基类构造函数,7.3 基类的初始化,/例7-6 调用构造函数顺序测试,构造函数无参数#include class Base public:Base()cout Base created.n;class D_class:public Base public:D_cla
33、ss()cout D_class created.n;void main()D_class d1;,调用派生类构造函数,7.3 基类的初始化,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()cout data1=data1,data2=data2 endl;class derived_class:private parent_clas
34、s int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,
35、2,-5);d1.inc1();d1.display();,例7-7 带参数构造函数调用顺序测试,7.3 基类的初始化,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()cout data1=data1,data2=data2 endl;class derived_class:private parent_class int data3
36、;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,2,-5);d1.in
37、c1();d1.display();,例7-7 带参数构造函数调用顺序测试,7.3 基类的初始化,int data1,data2;,int data3;,parent_class data4;,例7-7 带参数构造函数调用顺序测试,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()cout data1=data1,data2=data2
38、 endl;class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=da
39、ta3 endl;void main()derived_class d1(17,18,1,2,-5);d1.inc1();d1.display();,class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()coutdata1=data1,data2=data2endl;,基类有一个参数化的构造函数,7.3 基类的初始化,例7-7 带参数构造函数调用顺序测试,#
40、include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()cout data1=data1,data2=data2 endl;class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3
41、,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,2,-5);d1.inc1();d1.display();,class parent_class int data1,data2;public:
42、parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void display()coutdata1=data1,data2=data2endl;,成员函数数据成员自增,7.3 基类的初始化,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data
43、2;void display()cout data1=data1,data2=data2 endl;class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_c
44、lass:display();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,2,-5);d1.inc1();d1.display();,class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()ret
45、urn parent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=data3 endl;,7.3 基类的初始化,例7-7 带参数构造函数调用顺序测试,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void
46、display()cout data1=data1,data2=data2 endl;class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:di
47、splay();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,2,-5);d1.inc1();d1.display();,class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return par
48、ent_class:inc1();int inc3()return+data3;void display()parent_class:display();data4.display();cout data3=data3 endl;,类成员,7.3 基类的初始化,例7-7 带参数构造函数调用顺序测试,#include class parent_class int data1,data2;public:parent_class(int p1,int p2)data1=p1;data2=p2;int inc1()return+data1;int inc2()return+data2;void dis
49、play()cout data1=data1,data2=data2 endl;class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent_class:inc1();int inc3()return+data3;void display()parent_class:displ
50、ay();data4.display();cout data3=data3 endl;void main()derived_class d1(17,18,1,2,-5);d1.inc1();d1.display();,class derived_class:private parent_class int data3;parent_class data4;public:derived_class(int p1,int p2,int p3,int p4,int p5):parent_class(p1,p2),data4(p3,p4)data3=p5;int inc1()return parent