// mytime0.h -- Time class before operator overloading#ifndef MYTIME0_H_#define MYTIME0_H_classTime{private:inthours;intminutes;public:Time();// 默认构造函数Time(inth,intm=0);// 构造函数voidAddMin(intm);voidAddHr(inth);voidReset(inth=0,intm=0);constTimeSum(constTime&t)const;voidShow()const;};#endif
// usetime0.cpp -- using the first draft of the Time class// compile usetime0.cpp and mytime0.cpp together#include<iostream>#include"mytime0.h"intmain(){usingstd::cout;usingstd::endl;Timeplanning;Timecoding(2,40);Timefixing(5,55);Timetotal;cout<<"planning time = ";planning.Show();cout<<endl;cout<<"coding time = ";coding.Show();cout<<endl;cout<<"fixing time = ";fixing.Show();cout<<endl;total=coding.Sum(fixing);cout<<"coding.Sum(fixing) = ";total.Show();cout<<endl;// std::cin.get();return0;}
// mytime1.h -- Time class before operator overloading#ifndef MYTIME1_H_#define MYTIME1_H_classTime{private:inthours;intminutes;public:......Timeoperator+(constTime&t)const;};#endif
// usetime1.cpp -- using the second draft of the Time class// compile usetime1.cpp and mytime1.cpp together#include<iostream>#include"mytime1.h"intmain(){usingstd::cout;usingstd::endl;Timeplanning;Timecoding(2,40);Timefixing(5,55);Timetotal;cout<<"planning time = ";planning.Show();cout<<endl;cout<<"coding time = ";coding.Show();cout<<endl;cout<<"fixing time = ";fixing.Show();cout<<endl;total=coding+fixing;重载+的直接使用// operator notationcout<<"coding + fixing = ";total.Show();cout<<endl;Timemorefixing(3,28);cout<<"more fixing time = ";morefixing.Show();cout<<endl;total=morefixing.operator+(total);重载+的原理使用// function notationcout<<"morefixing.operator+(total) = ";total.Show();cout<<endl;// std::cin.get();return0;}
// mytime2.h -- Time class after operator overloading#ifndef MYTIME2_H_#define MYTIME2_H_classTime{private:inthours;intminutes;public:...Timeoperator-(constTime&t)const;Timeoperator*(doublen)const;...};#endif
// usetime2.cpp -- using the third draft of the Time class// compile usetime2.cpp and mytime2.cpp together#include<iostream>#include"mytime2.h"intmain(){usingstd::cout;usingstd::endl;Timeweeding(4,35);Timewaxing(2,47);Timetotal;Timediff;Timeadjusted;cout<<"weeding time = ";weeding.Show();cout<<endl;cout<<"waxing time = ";waxing.Show();cout<<endl;cout<<"total work time = ";total=weeding+waxing;// use operator+()total.Show();cout<<endl;diff=weeding-waxing;// use operator-()cout<<"weeding time - waxing time = ";diff.Show();cout<<endl;adjusted=total*1.5;// use operator+()cout<<"adjusted work time = ";adjusted.Show();cout<<endl;// std::cin.get(); return0;}
// mytime3.h -- Time class with friends#ifndef MYTIME3_H_#define MYTIME3_H_#include <iostreamclassTime{private:inthours;intminutes;public:Time();Time(inth,intm=0);voidAddMin(intm);voidAddHr(inth);voidReset(inth=0,intm=0);Timeoperator+(constTime&t)const;Timeoperator-(constTime&t)const;Timeoperator*(doublen)const;friendTimeoperator*(doublem,constTime&t)// 作为内联函数{returnt*m;}// inline definitionfriendstd::ostream&operator<<(std::ostream&os,constTime&t);};#endif
//usetime3.cpp -- using the fourth draft of the Time class// compile usetime3.cpp and mytime3.cpp together#include<iostream>#include"mytime3.h"intmain(){usingstd::cout;usingstd::endl;Timeaida(3,35);Timetosca(2,48);Timetemp;cout<<"Aida and Tosca:\n";cout<<aida<<"; "<<tosca<<endl;temp=aida+tosca;// operator+()cout<<"Aida + Tosca: "<<temp<<endl;temp=aida*1.17;// member operator*()cout<<"Aida * 1.17: "<<temp<<endl;cout<<"10.0 * Tosca: "<<10.0*tosca<<endl;// std::cin.get();return0;}
Stonewt(doublelbs);// double-object convert to Stonewt-object------------------------------------------------------------------StonewtmyCat;// create a Stonewt objectmyCat=19.6;// use Stonewt(double) to convert 19.6 to Stonewt使用构造函数Stonewt(double)来创建一个临时的Stonewt对象,并将19.6作为初始化值。随后,采用逐成员赋值的方式将该临时对象的内容复制到myCat中。这一过程叫做隐式转换(自动进行)
【2】只有接受一个参数的构造函数才能作为转换函数
C++
1
Stonewt(ints,doublelbs);// not a conversion func!
【3】c++增添了 关键字explicit 用于关闭 “构造函数用作自动类型转换函数”这一特性
C++
1
explicitStonewt(doublehbx);
这将关闭上述的隐式转换,但是仍然允许显式转换,即:显式强制转换
C++
1234
StonewtmyCat;myCat=19.6;// invalid!myCat=Stonewt(19.6);// OK! an explicit conversion [new form]myCat=(Stonewt)19.6;// OK! an explicit conversion [old form]
// stonewt1.h -- revised definition for the Stonewt class#ifndef STONEWT1_H_#define STONEWT1_H_classStonewt{private:enum{Lbs_per_stn=14};// pounds per stoneintstone;// whole stonesdoublepds_left;// fractional poundsdoublepounds;// entire weight in poundspublic:Stonewt(doublelbs);// construct from double poundsStonewt(intstn,doublelbs);// construct from stone, lbsStonewt();// default constructor~Stonewt();voidshow_lbs()const;// show weight in pounds formatvoidshow_stn()const;// show weight in stone format// conversion functions (反转函数!!!)operatorint()const;此函数目的:实现四舍五入功能operatordouble()const;此函数目的:直接返回该浮点数值};#endif