- 1
- 0
- 约1.55万字
- 约 66页
- 2017-02-08 发布于江苏
- 举报
多态性与虚函数培训教案
例4.6 (续一)。 #include iostream #include string using namespace std; class CPoint{ private: int X, Y; public: CPoint(int x=0, int y=0){X=x; Y=y;} CPoint(CPoint p){ X=p.X; Y=p.Y; } int GetX(){ return X; } int GetY() {return Y;} }; 例4.6 (续二)。 class CShape{ private: string Color; public: CShape(string c){Color = c;} virtual void Draw(){ cout Draw a shape. The color is Color endl; } void PrintColor(){ cout Color endl;} }; 例4.6 (续三)。 class CLine:public CShape{ private: CPoint Start; //线段的起点 CPoint End; //线段的终点 public: CLine(CPoint s, CPoint e, string c):CShape(c),Start(s),End(e){} virtual void Draw(){ cout Draw a Line from ( Start.GetX() , Start.GetY(); cout) to (End.GetX(),End.GetY() ), with color ; PrintColor(); } }; 例4.6 (续四)。 class CCircle:public CShape{ private: CPoint Center; int Radius; public: CCircle(CPoint ctr, int r, string c):CShape(c),Center(ctr){ Radius = r; } virtual void Draw(){ cout Draw a circle at center ( Center.GetX() , ; cout Center.GetY() ) with radius Radius and color ; PrintColor(); } }; 例4.6 (续五)。 int main(){ CShape *ps[3]; CShape s(Red); CPoint p1(10,10), p2(100,100),p3(50,50); CLine l(p1,p2,Green); CCircle c(p3, 20, Black); ps[0] = s; ps[1] = l; ps[2] = c; for(int i=0; i3; i++) ps[i]-Draw(); return 0; } 例4.9 (续一)。 #include iostream #include string using namespace std; class CPoint{ private: int X, Y; public: CPoint(int x=0, int y=0){X=x; Y=y;} CPoint(CPoint p){ X=p.X; Y=p.Y; } int GetX(){ return X; } int GetY() {return Y;} }; 例4.9 (续二)。 class CShape{ private: string Color; public: CShape(string c){Color = c;} virtual void Draw() = 0; void PrintColor(){ cout Color endl;} }; 例4.6 (续三)。 class CLine:public CShape{ private: CPoint Start; //线段的起点 CPoint End; //线段的终点 public: CLine(CPoint s, CPoint e, string c):CShape(c),Start(s),End(e){} virtual void Draw(){ co
您可能关注的文档
- 多媒体应用(声音)培训教案.ppt
- 多元随机变量及其分布演示文件修改版.ppt
- 多媒体产品方案调查培训教案.ppt
- 多媒体技术图像处理质量衡量标准PSNR培训教案.ppt
- 多媒体技术 培训教案.ppt
- 多媒体技术复习培训教案.ppt
- 多媒体数据压缩编码技术培训教案.ppt
- 多媒体技术应用图形图像处理技术(陈永强)培训教案.ppt
- 多媒体数据处理的技术标准演示文件修改版.ppt
- 多媒体数据压缩编码技术第一节培训教案.ppt
- GB/T 42818.2-2026认知无障碍 第2部分:报告.pdf
- 中国国家标准 GB/T 47116-2026地下采矿机械 工作面移动式采掘机械 采煤机和犁式系统的安全要求.pdf
- 《GB/T 47116-2026地下采矿机械 工作面移动式采掘机械 采煤机和犁式系统的安全要求》.pdf
- 中国国家标准 GB/T 42818.2-2026认知无障碍 第2部分:报告.pdf
- 《GB/T 42818.2-2026认知无障碍 第2部分:报告》.pdf
- 《GB/T 27664.1-2026无损检测仪器 超声检测设备的性能与检验 第1部分:仪器》.pdf
- 中国国家标准 GB/T 27664.1-2026无损检测仪器 超声检测设备的性能与检验 第1部分:仪器.pdf
- GB/T 27664.1-2026无损检测仪器 超声检测设备的性能与检验 第1部分:仪器.pdf
- GB/T 45305.5-2026声学 建筑构件隔声的实验室测量 第5部分:测试设施和设备的要求.pdf
- 中国国家标准 GB/T 45305.5-2026声学 建筑构件隔声的实验室测量 第5部分:测试设施和设备的要求.pdf
原创力文档

文档评论(0)