Chinaunix首页 | 论坛 | 博客
  • 博客访问: 24074
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-13 09:16
文章分类

全部博文(11)

文章存档

2015年(2)

2014年(9)

我的朋友

分类: 信息化

2014-05-13 10:20:38

// demo1.cpp : 定义控制台应用程序的入口点。
//通过此例程了解重载

#include "stdafx.h"
#include
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<  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<调用参数为double类型的Add函数
 cout<调用参数为int类型的Add函数
 cout<  cout<  cout<


 cout<  cout<<"mymath:"<  mymath++;
 cout<<"mymath++:"<  mymath--;
 cout<<"mymath--:"<  cout<<"mymath+mymath:"<  cout<<"mymath*mymath:"<  cout<<"mymath/mymath:"<

 CMath mymath1(20);
 mymath=mymath1;
 cout<<"mymath=mymath1:"<

 if (mymath==mymath1)
 {
  cout<<"mymath==mymath1"<  }

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

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

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

  
 return 0;
} 来源免费Q币资源网:http://blog.sina.com.cn/bugsqb

阅读(197) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:sbt配置nexus仓库

给主人留下些什么吧!~~