/************************************************************************** * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
#ifndef TEST_H
#define TEST_H
class Test
{
public:
explicit Test( int = 0 ); // default constructor
void print() const;
private:
int x;
}; // end class Test
#endif /* TEST_H */
#include "Test.h"
#include <iostream>
using namespace std;
// constructor
Test::Test( int value ) : x( value ){}
// print x using implicit and explicit this pointers;
// the parentheses around *this are required
void Test::print() const
{
// implicitly use the this pointer to access the member x
cout << " x = " << x;
// explicitly use the this pointer and the arrow operator
// to access the member x
cout << "\n this->x = " << this->x;
// explicitly use the dereferenced this pointer and
// the dot operator to access the member x
cout << "\n(*this).x = " << ( *this ).x << endl;
} // end function print
#include "Test.h"
int main()
{
Test testObject( 12 ); // instantiate and initialize testObject
testObject.print();
return 0;
} // end main
class Time
{
public:
//...此处省略若干行非重点部分
Time &setHour( int ); // set hour
Time &setMinute( int ); // set minute
Time &setSecond( int ); // set second
//...此处省略若干行非重点部分
private:
unsigned int hour; // 0 - 23 (24-hour clock format)
unsigned int minute; // 0 - 59
unsigned int second; // 0 - 59
}; // end class Time
// set hour value
Time &Time::setHour( int hour ) // note Time & return
{
if ( hour >= 0 && hour < 24 )
this->hour = hour;
else
throw invalid_argument( "hour must be 0-23" );
return *this; // enables cascading
} // end function setHour
// set minute 和 set second写法类似
//省略非重要的预处理指令和using命令
#include "Time.h" // Time class definition
int main()
{
Time t; // create Time object
// cascaded function calls
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
//省略其余非重要部分
} // end main
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有