介绍了C++类和对象的一些用法.ppt

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

C++程序设计基础 王海涛 汇文教育 2011.07 Time类实例研究 // Time.h #ifndef TIME_H #define TIME_H class Time { public: Time(); void setTime( int, int, int ); void printUniversal(); void printStandard(); private: int hour; // 0 - 23 (24-hour clock format) int minute; // 0 - 59 int second; // 0 - 59 }; #endif // Time.cpp #include iostream #include iomanip #include Time.h using namespace std; Time::Time() { hour = minute = second = 0; } void Time::setTime( int h, int m, int s ) { hour = ( h = 0 h 24 ) ? h : 0; minute = ( m = 0 m 60 ) ? m : 0; second = ( s = 0 s 60 ) ? s : 0; } void Time::printUniversal() { cout setfill( 0 ) setw( 2 ) hour : setw( 2 ) minute : setw( 2 ) second; } void Time::printStandard() { cout ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) : setfill( 0 ) setw( 2 ) minute : setw( 2 ) second ( hour 12 ? AM : PM ); } #include iostream #include Time.h using namespace std; int main() { Time t; cout The initial universal time is ; t.printUniversal(); // 00:00:00 cout \nThe initial standard time is ; t.printStandard(); // 12:00:00 AM t.setTime( 13, 27, 6 ); cout \n\nUniversal time after setTime is ; t.printUniversal(); // 13:27:06 cout \nStandard time after setTime is ; t.printStandard(); // 1:27:06 PM t.setTime( 99, 99, 99 ); // attempt invalid settings cout \n\nAfter attempting invalid settings: \nUniversal time: ; t.printUniversal(); // 00:00:00 cout \nStandard time: ; t.printStandard(); // 12:00:00 AM cout endl; } 预处理器封套:阻止重复定义 类的作用域和类成员的访问 #include iostream using namespace std; class Count { public: void setX( int value ) { x = value; } void print() { cout x endl; } private: int x; }; 类的作用域和类成员的访问 int main() { Count counter; Count *counterPtr = counter; Count counterRef = counter; cout Set x to 1 and print using the objects name: ; counter.setX( 1 );

文档评论(0)

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

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

1亿VIP精品文档

相关文档