文档详情

C语言程序设计课件第07章继承和派生.ppt

发布:2017-04-17约3.88千字共63页下载文档
文本预览下载声明
第七章 继承与派生;本章主要内容;类的继承与派生;继承与派生问题举例;继承与派生的目的;派生类的声明;继承方式;公有继承(public);例7-1 公有继承举例;class Rectangle: public Point { public: void InitR(float x, float y,float w, float h) {InitP(x,y);W=w;H=h;} float GetH() {return H;} float GetW() {return W;} private: float W,H; };;#includeiostream using namespace std; void main() { Rectangle rect; rect.InitR(2,3,20,10); rect.Move(3,2); coutrect.GetX(),rect.GetY(), rect.GetW(),rect.GetH(); };私有继承(private);例7-2 私有继承举例;#includeiostream using namespace std; void main() { Rectangle rect; rect.InitR(2,3,20,10); rect.Move(3,2); coutrect.GetX(),rect.GetY(), rect.GetH(),rect.GetW(); }; class Rectangle: private Point { public: void InitR(float x, float y,float w, float h) {InitP(x,y);W=w;H=h;} void Move(float xOff, float yOff) { Point::Move(xOff,yOff);} float GetX() { return Point::GetX();} float GetY() { return Point::GetY();} float GetH() {return H;} float GetW() {return W;} private: float W,H; };;#includeiostream using namespace std; void main() { Rectangle rect; rect.InitR(2,3,20,10); rect.Move(3,2); coutrect.GetX(),rect.GetY(), rect.GetH(),rect.GetW(); };保护继承(protected);#includeiostream using namespace std; class A { protected: int x; }; class B: protected A { public: void Function(); }; class C: private B { public: void Function(); };;#includeiostream using namespace std; class A { protected: int x; }; class B: protected A { public: void Function(); }; class C: private B { public: void Function(); };; ;类型兼容规则;类型兼容;类型兼容;基类与派生类的对应关系;多继承时派生类的声明;class A { public: void setA(int); void showA(); private: int a; }; class B { public: void setB(int); void showB(); private: int b; };;void A::setA(int x) { a=x; } void B::setB(int x) { b=x; } void C::setC(int x, int y, int z) { setA(x); setB(y); c=z; } //其他函数实现略;继承时的构造函数;继承时的构造函数;单一继承时的构
显示全部
相似文档