1 / 10

第四章继承和派生类

第四章继承和派生类. 继承 : 派生类自动地将基类的所有成员作为自己的成员; 派生类中有四种访问级别的成员。. class 派生类名 : 访问控制 基类名 { private: 成员说明列表 public: 成员说明列表 protected: 成员说明列表 };. 基类的 私有成员不允许 派生类的成员函数 访问 ,但可通过基类提供的公有成员函数访问 基类的 公有成员和保护成员允许 派生类的成员函数 访问 公有派生:基类的公有成员在派生类中公有; 保护成员在派生类中保护 赋值兼容规则

adair
Download Presentation

第四章继承和派生类

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第四章继承和派生类 • 继承:派生类自动地将基类的所有成员作为自己的成员;派生类中有四种访问级别的成员。 class 派生类名:访问控制基类名{ private:成员说明列表 public:成员说明列表 protected:成员说明列表}; • 基类的私有成员不允许派生类的成员函数访问,但可通过基类提供的公有成员函数访问 • 基类的公有成员和保护成员允许派生类的成员函数访问 • 公有派生:基类的公有成员在派生类中公有; 保护成员在派生类中保护 赋值兼容规则 • 私有派生:基类的所有成员在派生类中私有

  2. 第四章继承和派生类 • 构造函数形式: Y::Y(参数表0):X1(参数表1),X2(参数表2),…Xn(参数表n) 基类定义缺省(无参)构造函数或无构造函数,则初始化列表可省 • 派生类只能调用它的直接基类的构造函数 • 构造函数调用顺序 • 先执行虚基类的构造函数按它们被继承的顺序构造 • 所有非虚基类的构造函数按它们被继承的顺序构造 • 所有派生类子对象的构造函数按它们声明的顺序构造 • 派生类自己的构造函数 • 从虚基类直接或间接派生的派生类的构造函数的成员初始化列表中都要列出对虚基类构造函数的调用,但只有用于建立对象的最派生类的构造函数调用虚基类的构造函数,保证对虚基类子对象只初始化一次 • 构造函数和析构函数不能被继承

  3. 第四章继承和派生类 • 两义性 • 成员名限定 类名::类标识符 • 派生类中重定义基类的函数,只要函数名相同,函数参数与返回值不考虑,则遵守支配规则执行派生类的同名函数 • 虚基类 class 派生类::virtual 派生方式 基类。虚基类在内存中只有一个实例

  4. 勘误表 • P77输出结果第六行 a.func() b.func()

  5. class BoxShape{ //改错 int width,height; public: void SetWidth(int w){width=w;} void SetHeight(int h){height=h;} int GetWidth(){return width;} int GetHeight(){return height;}}; class ColorBoxShape:private BoxShape{ int color; public: ColorBoxShape(int w,int h,int n) {SetWidth(w);SetHeight(h);color=n;}}; void main() {ColorBoxShape cbox(5,6,8); cbox.SetWidth(3); cout<<“width=“<<cbox.width<<endl;} 该句错误 该句错误

  6. 例题分析 • 在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是 A.派生类的对象可以赋给基类的对象 B.派生类的对象可以初始化基类的引用 C.派生类的对象可以直接访问基类中的成员 D.派生类的对象的地址可以赋给指向基类的指针 C • 对基类和派生类的关系描述中,错误的是 • A.派生类是基类的具体化 B.基类继承了派生类的属性 • C.派生类是基类定义的延续 D.派生类是基类的特殊化 B 基类 • 派生类从一个或多个以前定义的该类的继承数据和函数 cout.setf(ios::hex) • 表达式cout<<hex还可表示为 性质约束 • C++实现继承机制时,采用的面向对象技术为:即对父类的性质加以限制或删除,即增加父类的性质 性质扩展

  7. 例题分析输出结果 #include <iostream.h> void main() {int *a; int*&p=a; int b=10; p=&b; cout<<*a; } 输出为: 10

  8. 例题分析改错 #include <iostream.h> class A{ public:void fun(){cout<<“a.fun”<<endl;}}; class B{ public:void fun(){cout<<“b.fun”<<endl;} void gun(){cout<<“b.gun”<<endl;}}; class C:public A,public B{ private:int b; public:void gun(){cout<<“c.gun”<<endl;} void hun(){fun();}}; void hun(){fun();}错误,有两义性

  9. 例题分析改错 #include <iostream.h> class A{ public:void fun(){cout<<“a.fun”<<endl;}}; class B{ public:void fun(){cout<<“b.fun”<<endl;} void gun(){cout<<“b.gun”<<endl;}}; class C:public A,public B{ private:int b; public:void gun(){cout<<“c.gun”<<endl;}}; void main() { C obj; obj.fun(); obj.gun();} obj.fun();错误,有两义性

  10. class goods{ //当输入25,60时的输出结果 static int totalWeight; int weight; public: goods(int w){weight=w;totalWeight+=w;} goods(goods& gd){weight=gd.weight;totalWeight+=weight;} ~goods(){totalWeight-=weight;} int getwg(){return weight;} static int getTotal(){return totalWeight;}}; int goods:totalWeight=0; void main() { int w; cout<<“The weight of goods:”<<goods:getTotal()<<endl; cin>>w; goods g1(w); cin>>w; goods g2(w); cout<<“Total weight of goods:”<<goods::getTotal()<<endl;} 输出为: • The weight of goods:0 • 60 • Total weight of goods:85

More Related