第2讲--类与对象.ppt

  1. 1、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第2讲--类与对象

*;*;*;抽象;对象;对象;对象;类;*;抽象实例——人类;*;*;*;封装;*;*;继承与派生;继承的分类;;*;;*;*;*;*;*;*;void Clock :: setTime(int NewH, int NewM, int NewS) { Hour=NewH; Minute=NewM; Second=NewS; } void Clock :: showTime() { coutHour:Minute:Second; };*;*;*;;编译器看到inline后,为该函数创建一段代码,以便在后面每次碰到该函数的调用都用相应的一段代码来替换。 内联函数必须在被调用之前声明或定义; 内联函数适合于只有1~5行的小函数; 内联函数与宏定义 C中,常用预处理语句#define来代替一个函数定义,例: #define MAX(a,b) ((a)(b)?(a):(b)) C++中,通过一个内联函数可替换这种宏定义语句: inline int MAX(int a,int b) { return ab?a:b; };*;*;*;*;*;*;*;构造函数的实现: Clock::Clock(int NewH, int NewM, int NewS) { Hour=NewH; Minute=NewM; Second=NewS; } 建立对象时构造函数的作用: void main() { Clock c (0,0,0); //隐含调用构造函数,将初始值作为实参 c.showTime(); };构造函数举例;;;*;*;Point::Point (const Point p) { x=p.x; y=p.y; cout拷贝构造函数被调用endl; };*;;;*;;*;;class Point { public : Point( int xx=0, int yy=0 ) { X=xx ; Y=yy; cout “Object constructed.” endl ; } Point( Point p); ~ Point( ) { coutX “,”Y“Object destroyed.”endl; } int GetX ( ) { return X ; } int GetY ( ) { return Y ; } private : int X , Y ; } ; Point :: Point (Point p ) { X= p.X ; Y=p.Y ; cout “Copy_constructor called.”endl ; } Point fun2 ( ) { Point A ( 1 , 2 ) ; return A ; } main ( ) { Point B ; B = fun2 ( ) ; };class Point { public : Point ( int xx=0, int yy=0 ) { X=xx ; Y=yy; cout “Object constructed.” endl ; } Point ( Point p ) ; ~ Point ( ) { cout X “,” Y “ Object destroyed.” endl ; } int GetX ( ) { return X ; } int GetY ( ) { return Y ; } private : int X , Y ; } ; Point :: Point (Point p ) { X= p.X ; Y=p.Y ; cout “Copy_constructor called.” endl ; } Point fun2( ) { Point A ( 1 , 2 ) ; return A ; } main ( ) { Point B ; B = fun2 ( ) ; };class Point { public : Point ( int xx=0, int yy=0 ) { X=xx ; Y=yy; cout “Objec

文档评论(0)

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

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

1亿VIP精品文档

相关文档