码迷,mamicode.com
首页 > 编程语言 > 详细

c++训练营--重载

时间:2014-05-31 16:48:33      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   a   http   

// 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;
}

 bubuko.com,布布扣

c++训练营--重载,布布扣,bubuko.com

c++训练营--重载

标签:c   style   class   blog   a   http   

原文地址:http://www.cnblogs.com/mengfanrong/p/3761038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!