// demo1.cpp : 定义控制台应用程序的入口点。

//通过此例程了解重载

#include "stdafx.h"

#include <iostream>

using namespace std;

 

class CMath

{

public:

 CMath(float a):m_a(a)

 {

 }

 ~CMath()

 {

 }

 double Add(double a,double b);

 double Sub(double a,double b);

 double Mul(double a,double b);

 double Div(double a,double b);

 int Add(int a,int b)

 {

  return a+b;

 }

 int Sub(int a,int b)

 {

  return a-b;

 }

 int Mul(int a,int b)

 {

  return a*b;

 }

 int Div(int a,int b)

 {

  if (b!=0)

  {

   return a/b;

  }

  return 0;

 }

public:

 CMath operator +(CMath& mymath)

 {

  CMath result(0);

  result.m_a=m_a+mymath.m_a;

  return result;

 }

 CMath operator -(CMath& mymath)

 {

  CMath result(0);

  result.m_a=m_a-mymath.m_a;

  return result;

 }

 CMath operator *(CMath& mymath)

 {

  CMath result(0);

  result.m_a=m_a*mymath.m_a;

  return result;

 }

 CMath operator /(CMath& mymath)

 {

  if (mymath.m_a==0)

  {

   mymath.m_a=1;

  }

  CMath result(0);

  result.m_a=m_a/mymath.m_a;

  return result;

 }

 CMath operator ++()//前++

 {

  this->m_a+=1;

  return *this;

 }

 CMath operator ++(int)//后++,加上參数int,无意义

 {

  CMath *t=this;

  ++(t->m_a);

  return *t;

 }

 CMath operator --()//前--

 {

  this->m_a-=1;

  return *this;

 }

 CMath operator --(int)//后--,加上參数int,无意义

 {

  CMath *t=this;

  --(this->m_a);

  return *t;

 }

 CMath operator +=(CMath & mymath)

 {

  m_a+=mymath.m_a;

  return *this;

 }

  CMath operator -=(CMath & mymath)

 {

  m_a-=mymath.m_a;

  return *this;

 }

 bool operator ==(CMath &mymath)

 {

  return (m_a==mymath.m_a);

 }

 friend ostream& operator <<(ostream &os,CMath &t);//必须是友元

 

 

 friend iostream& operator >>(iostream &is,CMath &t);//必须是友元

 

 

 CMath operator ()(const CMath& t)

 {

  return CMath(this->m_a=t.m_a);

 }

 CMath& operator =(CMath &t)

 {

  this->m_a=t.m_a;

  return *this;

 }

private:

 float m_a;

};

 

ostream& operator <<(ostream &os,CMath &t)

{

 os<<t.m_a<<endl;

 return os;

}

iostream& operator >>(iostream &is,CMath &t)

{

 is>>t.m_a;

 return is;

}

 

double CMath::Add(double a,double b)

{

 return a+b;

}

 

double CMath::Sub(double a,double b)

{

 return a-b;

}

 

double CMath::Mul(double a,double b)

{

 return a*b;

}

 

double CMath::Div(double a,double b)

{

 if (b==0)

 {

  b=1;

 }

 return a/b;

}

int _tmain(int argc, _TCHAR* argv[])

{

 CMath mymath(4);

 cout<<mymath.Add(1.1,2.1)<<endl;//调用參数为double类型的Add函数

 cout<<mymath.Add(1,2)<<endl;//调用參数为int类型的Add函数

 cout<<mymath.Sub(1,2)<<endl;//

 cout<<mymath.Mul(1,2)<<endl;

 cout<<mymath.Div(1.3,2.3)<<endl;

 cout<<endl<<endl;

 cout<<"mymath:"<<mymath<<endl;

 mymath++;

 cout<<"mymath++:"<<mymath<<endl;

 mymath--;

 cout<<"mymath--:"<<mymath<<endl;

 cout<<"mymath+mymath:"<<mymath+mymath<<endl;

 cout<<"mymath*mymath:"<<mymath*mymath<<endl;

 cout<<"mymath/mymath:"<<mymath/mymath<<endl;

 CMath mymath1(20);

 mymath=mymath1;

 cout<<"mymath=mymath1:"<<mymath<<endl;

 if (mymath==mymath1)

 {

  cout<<"mymath==mymath1"<<endl;

 }

 mymath1+=mymath;

 cout<<"mymath1:"<<mymath1<<endl;

 mymath1-=mymath;

 cout<<"mymath1:"<<mymath1<<endl;

 mymath--;

 mymath1(mymath);

 cout<<"mymath1(mymath):"<<mymath1<<endl;

 

 return 0;

}

 

c++训练营--重载的更多相关文章

  1. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]

    方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...

  2. PHP类和对象之重载

    PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...

  3. C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱

    一.前言 -孤独的路上有梦想作伴,乘风破浪- 二.页面值传递 (1)C#各页面之间可以进行数据的交换和传递,页面之间可根据获取的数据,进行各自的操作(跳转.计算等操作).为了实现多种方式的数据传递,C ...

  4. new/delete重载

    在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...

  5. java重载与覆写

    很多同学对于overload和override傻傻分不清楚,建议不要死记硬背概念性的知识,要理解着去记忆. 先给出我的定义: overload(重载):在同一类或者有着继承关系的类中,一组名称相同,参 ...

  6. 【C++】多态性(函数重载与虚函数)

    多态性就是同一符号或名字在不同情况下具有不同解释的现象.多态性有两种表现形式: 编译时多态性:同一对象收到相同的消息却产生不同的函数调用,一般通过函数重载来实现,在编译时就实现了绑定,属于静态绑定. ...

  7. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  8. C++重载new和delete运算符

    内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...

  9. Java学习笔记之方法重载

    被重载的方法必须具有不同的参数列表.不能基于不同修饰符或返回值类型来重载方法. package welcome; public class TestMethodOverloading { public ...

随机推荐

  1. linux xampp eclipse xdebug 无法进入断点

    一.xampp 版本 1.8.3-5 xampp安装后会自动集成xdebug,目录一般为 /opt/lampp/lib/php/extensions/***-debug-***目录 关于php 与ph ...

  2. Yii: 设置数据翻页

    一种方法是使用CPagination处理翻页需要的数据如:总数据项数,每页数据项数,当前页,然后在视图中使用CBasePager来绘制. 控制器动作的代码示范: function actionInde ...

  3. js画线

    <body> <div id="main"> </div> <div id="fd" style="filt ...

  4. MySQL修改root密码的几种方法

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  5. selenium python (十五)控制滚动条操作

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般用到操作滚动条的两个场景    #注册时的法律条文的阅读,判断用户 ...

  6. LR之录制SQL脚本

    1.选择协议 ①MS SQL serve ②ODBC 一般情况下选ODBC 2.录制步骤

  7. VC++中内存对齐

    我们经常看到求 sizeof(A) 的值的问题,其中A是一个结构体,类,或者联合体. 为了优化CPU访问和优化内存,减少内存碎片,编译器对内存对齐制定了一些规则.但是,不同的编译器可能有不同的实现,本 ...

  8. <javascript搞基程序设计>笔记2015-9-25

    1.一元加减 var a=28; alert(--a); //27,先减后用 alert(a--); //27,先用后减 alert(a); //26, 2.位操作符 按位非(NOT):按位取反:符号 ...

  9. PHP中mysql_affected_rows()和mysql_num_rows()区别

    mysql_affected_rows -- 取得前一次 MySQL 操作所影响的记录行数mysql_num_rows -- 函数返回结果集中行的数目. config.php <?php hea ...

  10. SQL时间第一期_获取系统年月日时分秒

    select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...