聊城大学计算机学院C++程序设计教学课件--继承.ppt

聊城大学计算机学院C++程序设计教学课件--继承.ppt

  1. 1、本文档共129页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
多文件程序 test.h的内容 #ifndef TEST_H #define TEST_H class Test { int a; public: Test( int n ) : a( n ) { } }; #endif main.cpp的内容 #include test.h #include test.h int main() { Test t( 10 ); return 0; } 多文件程序 main.cpp的内容 #include test.h #include test.h int main() { Test t( 10 ); return 0; } #ifndef TEST_H #define TEST_H class Test { int a; public: Test( int n ) : a( n ) {} }; #endif #ifndef TEST_H #define TEST_H class Test { int a; public: Test( int n ) : a( n ) {} }; #endif ……… 多文件程序 任何一个头文件都应该避免重复包含,避免重复包含的办法如下:(对于头文件abc.h) 在头文件的开头加上 #ifndef ABC_H_ #define ABC_H_ 在头文件的最后加上 #endif shape.h文件的内容 class Shape { //… }; circle.h文件的内容 #include shape.h class Circle : public Shape { //… }; square.h文件的内容 #include shape.h class Square : public Shape { //… }; main.cpp文件 #include circle.h #include square.h int main() { //用到了Circle 类 //用到了Square类 return 0; } 5.5 对象成员 对象成员 类:线段 属性 顶点1坐标、顶点2坐标 行为: 返回长度 对象成员 class Line { private: double x1; double y1; double x2; double y2; public: Line(double a, double b, double c, double d) : x1(a), y1(b), x2(c), y2(d) {} double GetLenth() const {return sqrt( (x1-x2)*(x1-x2) + (y1-y2)* (y1-y2) );} }; 对象成员 class Point { double m_nX; double m_nY; public: Point( double a, double b ) : m_nX( a ), m_nY( b ) { coutCon Point:m_nXm_nYendl; } double GetX() const { return m_nX; } double GetY() const { return m_nY; } ~Point() { coutDes Point:m_nXm_nY endl;} }; 对象成员 class Line { Point m_p1; Point m_p2; public: Line( double a, double b, double c, double d ) : m_p1( a, b ), m_p2( c , d ) { cout Con Line endl; } double GetLenth() const; ~ Line() { cout Des Line endl; } }; m_p1 m_nY: m_nX: m_p2 m_nY: m_nX: L1 2 1 4 3 int main() { Line L1(1,2,3,4); return 0; } m_p2( c , d ), m_p1( a, b ) Line(double a, double b, double c, double d ) 对象成员 内嵌对象的初始化由内嵌对象的构造函数来完成。 当没有显示指定内嵌对象的初始化方式时,将调用内嵌对象的默认构造函数 构造函数执行顺序 先按内嵌对象声明的顺序的调用内嵌对象的构造函数 再调用外围类的构造函数 析构函数执行顺序 先调用外围类的析构函数 再按内嵌对

文档评论(0)

moon8888 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档