文档详情

C++继承与派生讲解.ppt

发布:2017-04-14约4.78千字共63页下载文档
文本预览下载声明
*;第8章 继承与派生;回顾;*;自行车 Bicycle;双人自行车 Tandem Bike;竞速自行车 Racing Bike;8.1 继承的概念-例子;8.1 继承的概念-例子;8.1 继承的概念;*;8.2 定义基类和派生类;*;例8-1 简单的继承和派生;class Tshape { private: uint _x, _y; //几何形状的位置 public: TShape( ); uint getX( ); uint getY( ); void setX( uint x ); void setY( uint y ); void Draw( ); };;//TShape01.cpp: 类TShape的实现 #include TShape01.h #include iostream using namespace std; TShape::TShape( ) { _x = 10; _y = 10; } void TShape::Draw( ) { coutThis is TShape::Draw()endl; };#include TShape01.h class TEllipse: public TShape { public: void Draw( ); };;// TEllipse01.cpp: 类TEllipse的实现 #include TEllipse01.h #include iostream using namespace std; void TEllipse::Draw( ) { coutDraw an ellipse with colorendl; };*;8.2.2 定义派生类;*;8.2.3 访问控制和继承关系;8.2.3 访问控制和继承关系;1. 公有继承 ;class Point {//基类 public: void InitP(float xx=0, float yy=0) {X=xx; Y=yy;} void Move(float xOff, float yOff) {X+=xOff; Y+=yOff;} float GetX() {return X;} float GetY() {return Y;} private: float X,Y; };;2. 私有继承 ;3.保护继承 ;class Point { public: void InitP( float xx=0, float yy=0 ) {X=xx; Y=yy;} void Move( float xOff, float yOff ) {X+=xOff; Y+=yOff;} float GetX( ) {return X;} float GetY( ) {return Y;} private: float X,Y; };;私有继承和保护继承的区别;继承方式影响访问控制;*;8.2.4 同名覆盖 override ;class Shape { public: ┇ void Draw(){ }; protected: ┇ }; ;*;8.3 构造函数与析构函数 ;*;8.3.1 基类只有无参数的构造函数;*;8.3.2 派生类的构造函数;8.3.2 派生类的构造函数;//TShape04.cpp #include TShape04.h #include iostream TShape::TShape(uint x, uint y){ _x = x; _y = y; } void TShape::Draw(){ std::coutThis is TShape::Draw()std::endl; } void TShape::getXY(uint x, uint y){ x = _x; y = _y; } void TShape::getRGB(uchar R, uchar G, uchar B){ R = _RED; G = _GREEN; B = _BLUE; };//TEllipse04.cpp #include TEllipse04.h #include iostream TEllipse::TEllipse(uint longR, uint shortR, uint x, uint y):TShape(x,y){ _longR = longR; _shortR = shortR
显示全部
相似文档