www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第6章/s6_16/smain6_16.cpp
//Download by http://www.NewXing.com //文件名:smain6_16.cpp //构造函数类型转换 #include <iostream> #include <assert.h> using namespace std; class CDouble { double m_double; public: CDouble() { m_double = 0; } CDouble(double d) // 构造函数(1) { m_double = d; } CDouble(const char *pChar, double d=0) // 构造函数(2) { char *endptr; //函数参数需要 m_double = d; assert( pChar != NULL ); //断言 // strtod()是一个将字符串转化为双精度型数值的函数 m_double = strtod(pChar, &endptr); } void print() { cout << m_double << endl; } }; //测试函数。 void main() { CDouble dCDouble(5); dCDouble = 10; dCDouble.print(); //输出:10 dCDouble = "1234.56"; dCDouble.print(); //输出:1234.56 }