当前位置: 首页 > news >正文

首都医科大学网站建设嘉兴建站服务

首都医科大学网站建设,嘉兴建站服务,dw制作网页的步骤,成都新闻最新消息我们知道C可以对函数进行重载#xff0c;让同名的函数来完成相同的基本操作。其实运算符也是可以重载的#xff0c;而且有的运算符已经在使用了#xff0c;就像*#xff0c;既可以用于地址#xff0c;又可以用于乘法。 下面是一个计算时间的类 #ifndef MYTIME_H #define…我们知道C可以对函数进行重载让同名的函数来完成相同的基本操作。其实运算符也是可以重载的而且有的运算符已经在使用了就像*既可以用于地址又可以用于乘法。 下面是一个计算时间的类 #ifndef MYTIME_H #define MYTIME_H#include iostreamusing namespace std;class Time { private:int hours;int minutes;public:Time();Time(int h, int m 0);void AddMin(int m);void AddHr(int h);void Reset(int h 0, int m 0);Time Sum(const Time t) const;void Show() const; };#endif // MYTIME_H#include mytime.hTime::Time() {hours minutes 0; }Time::Time(int h, int m) {hours h;minutes m; }void Time::AddMin(int m) {minutes m;hours minutes/60;minutes % 60; }void Time::AddHr(int h) {hours h; }void Time::Reset(int h, int m) {hours h;minutes m; }Time Time::Sum(const Time t) const {Time sum;sum.minutes minutes t.minutes;sum.hours hours t.hours sum.minutes/60;sum.minutes % 60;return sum; }void Time::Show() const {cout hours hours, minutes minutes.\n; } #include iostream #include mytime.husing namespace std;int main() {Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;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;return 0; } 输出结果 planning time 0 hours, 0 minutes.coding time 2 hours, 40 minutes.fixing time 5 hours, 55 minutes.coding.Sum(fixing) 8 hours, 35 minutes. 添加加法运算符 将Time类转换为重载的加法运算法很容易只要将Sum()的名称改为operator ()即可。只要把运算符放在operator的后面 #ifndef STOCK_H #define STOCK_H #include stringusing namespace std;class stock { private:string company;long shares;double share_val;double total_val;void set_tot() {total_val shares * share_val;}public:void buy(long num, double price);void sell(long num, double price);void update(double price);void show() const;const stock topval(const stock s) const;stock(const string co, long n, double pr);stock();~stock(); };#endif // STOCK_H#include mytime.hTime::Time() {hours minutes 0; }Time::Time(int h, int m) {hours h;minutes m; }void Time::AddMin(int m) {minutes m;hours minutes/60;minutes % 60; }void Time::AddHr(int h) {hours h; }void Time::Reset(int h, int m) {hours h;minutes m; }//运算符重载 Time Time::operator(const Time t) const {Time sum;sum.minutes minutes t.minutes;sum.hours hours t.hours sum.minutes/60;sum.minutes % 60;return sum; }void Time::Show() const {cout hours hours, minutes minutes.\n; } operator(const Time t) const与Sum(const Time t) const效果完全一样可以理解operator()就是一个函数也可以被调用。就像下面的语句一样。 total coding.operator(fixing); 但是可以进一步简化 total coding fixing; 这两种方法都讲使用operator()函数运算符左侧的coding是调用对象右边的fixing是参数被传递的对象。 #include iostream #include mytime.husing namespace std;int main() {Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;cout planning time ;planning.Show();cout endl;cout coding time ;coding.Show();cout endl;cout fixing time ;fixing.Show();cout endl;//total coding.operator(fixing);total coding fixing;cout coding.Sum(fixing) ;total.Show();cout endl;return 0; }输出结果没有变化 planning time 0 hours, 0 minutes.coding time 2 hours, 40 minutes.fixing time 5 hours, 55 minutes.coding.Sum(fixing) 8 hours, 35 minutes. 运算符重载的限制 1、重载后的运算符必须至少有一个操作数是用户定义的类型 2、使用运算符使不能违反运算符原来的句法规则 3、不能创建新的运算符 4、有些运算符不能被重载如sizeof, ?:, ::等 5、大多数运算符都可以通过成员或者非成员函数进行重载但、、[]、-只能通过成员函数进行重载。 、-、 *重载的应用 #ifndef MYTIME_H #define MYTIME_H#include iostreamusing namespace std;class Time { private:int hours;int minutes;public:Time();Time(int h, int m 0);void AddMin(int m);void AddHr(int h);void Reset(int h 0, int m 0);Time operator(const Time t) const;Time operator-(const Time t) const;Time operator*(double mult) const;void Show() const; };#endif // MYTIME_H #include mytime.hTime::Time() {hours minutes 0; }Time::Time(int h, int m) {hours h;minutes m; }void Time::AddMin(int m) {minutes m;hours minutes/60;minutes % 60; }void Time::AddHr(int h) {hours h; }void Time::Reset(int h, int m) {hours h;minutes m; }//运算符重载 Time Time::operator(const Time t) const {Time sum;sum.minutes minutes t.minutes;sum.hours hours t.hours sum.minutes/60;sum.minutes % 60;return sum; }Time Time::operator-(const Time t) const {Time diff;int tot1, tot2;tot1 t.minutes 60 * t.hours;tot2 minutes 60 * hours;diff.minutes (tot2 - tot1) % 60;diff.hours (tot2 - tot1) / 60;return diff; }Time Time::operator*(double mult) const {Time result;long totalminutes hours * mult *60 minutes * mult;result.hours totalminutes / 60;result.minutes totalminutes % 60;return result; }void Time::Show() const {cout hours hours, minutes minutes.\n; } #include iostream #include mytime.husing namespace std;int main() {Time weeding(4, 35);Time waxing(2, 47);Time total;Time diff;Time adjusted;cout weeding time ;weeding.Show();cout endl;cout waxing time ;waxing.Show();cout endl;cout total working time ;total weeding waxing;total.Show();cout endl;diff weeding - waxing;cout weeding time - waxing time ;diff.Show();cout endl;adjusted total * 1.5;cout adjusted work time ;adjusted.Show();cout endl;return 0; }输出结果 weeding time 4 hours, 35 minutes.waxing time 2 hours, 47 minutes.total working time 7 hours, 22 minutes.weeding time - waxing time 1 hours, 48 minutes.adjusted work time 11 hours, 3 minutes.
文章转载自:
http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn
http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn
http://www.morning.jlqn.cn.gov.cn.jlqn.cn
http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn
http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn
http://www.morning.kxltf.cn.gov.cn.kxltf.cn
http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn
http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn
http://www.morning.xhftj.cn.gov.cn.xhftj.cn
http://www.morning.4q9h.cn.gov.cn.4q9h.cn
http://www.morning.hkpn.cn.gov.cn.hkpn.cn
http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn
http://www.morning.ttfh.cn.gov.cn.ttfh.cn
http://www.morning.tftw.cn.gov.cn.tftw.cn
http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn
http://www.morning.ampingdu.com.gov.cn.ampingdu.com
http://www.morning.dighk.com.gov.cn.dighk.com
http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn
http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn
http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn
http://www.morning.xrnh.cn.gov.cn.xrnh.cn
http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn
http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com
http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn
http://www.morning.frtb.cn.gov.cn.frtb.cn
http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn
http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn
http://www.morning.pcngq.cn.gov.cn.pcngq.cn
http://www.morning.fglzk.cn.gov.cn.fglzk.cn
http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com
http://www.morning.lgznf.cn.gov.cn.lgznf.cn
http://www.morning.oioini.com.gov.cn.oioini.com
http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn
http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn
http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn
http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn
http://www.morning.hkpn.cn.gov.cn.hkpn.cn
http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn
http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn
http://www.morning.qwfq.cn.gov.cn.qwfq.cn
http://www.morning.tqklh.cn.gov.cn.tqklh.cn
http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn
http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn
http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn
http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn
http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn
http://www.morning.rfgc.cn.gov.cn.rfgc.cn
http://www.morning.qgghr.cn.gov.cn.qgghr.cn
http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn
http://www.morning.hpkr.cn.gov.cn.hpkr.cn
http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn
http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn
http://www.morning.drwpn.cn.gov.cn.drwpn.cn
http://www.morning.jxltk.cn.gov.cn.jxltk.cn
http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn
http://www.morning.kxypt.cn.gov.cn.kxypt.cn
http://www.morning.kgslc.cn.gov.cn.kgslc.cn
http://www.morning.shxrn.cn.gov.cn.shxrn.cn
http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn
http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn
http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn
http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn
http://www.morning.pinngee.com.gov.cn.pinngee.com
http://www.morning.jytrb.cn.gov.cn.jytrb.cn
http://www.morning.rcntx.cn.gov.cn.rcntx.cn
http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com
http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn
http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn
http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn
http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn
http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn
http://www.morning.yznsx.cn.gov.cn.yznsx.cn
http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn
http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn
http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn
http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn
http://www.morning.gwmny.cn.gov.cn.gwmny.cn
http://www.morning.wklhn.cn.gov.cn.wklhn.cn
http://www.morning.fphbz.cn.gov.cn.fphbz.cn
http://www.morning.qbtj.cn.gov.cn.qbtj.cn
http://www.tj-hxxt.cn/news/245558.html

相关文章:

  • 深圳做网站报价网站和浏览器不兼容
  • 深圳中国网站制作哪家公司好建设银行申请信用卡网站首页
  • 东莞网站建设十大品牌360全景地图下载安装
  • 怎样做视频网站的外链电影下载网站如何做
  • 怎么把网页放到网站上hao123网址导航官网
  • 新网站提交百度收录网站做照片
  • 域名网站打开慢seo教程正规化岚鸿
  • 在北京大学生做家教的网站新型建筑模板样品图片
  • 平面设计师长逛的网站有哪些中国计算机软考网
  • 网站怎么能在百度搜到网站做好了每年都要续费吗
  • 做网站建设工资高吗国外的设计网站
  • 网站icp不备案有关系吗58同城泰安
  • 云南做网站费用智能网站排名优化
  • 上海网站建设广告语下载网站php源码
  • 网站开发商业秘密保密协议珠海市网站建设制作设计平台
  • 做国产免费视频网站电商网站建设基本流程
  • vs网站模板成都好玩的地方排行榜
  • 个人网站设计分类h5页面制作app
  • 无锡做网站优化山东网架公司
  • 池州哪里有做网站镇江网站制作优化
  • 做网站推广那家好灰色 网站
  • 企业网站程序下载舆情监测软件免费版
  • 营销型网站建设实战》营销型网站建设培训
  • 外国旅游网站建设现状wordpress卢松松主题
  • 建立一个网站需要人员企业网站诊断与优化方案
  • 网站优化三要素阳江网梁国燊事件
  • 只放一个图片做网站第三方公司做网站价格
  • 网站需要更新的频率搜索引擎优化实验报告
  • 网站开发简介浙江联科网站建设
  • 沧州网站建设的技术方案网站推广方法素材