c++训练营--重载
// 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++训练营--重载的更多相关文章
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
- PHP类和对象之重载
PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的.属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值.读取.判断属性是否设置.销毁属性. ...
- C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
一.前言 -孤独的路上有梦想作伴,乘风破浪- 二.页面值传递 (1)C#各页面之间可以进行数据的交换和传递,页面之间可根据获取的数据,进行各自的操作(跳转.计算等操作).为了实现多种方式的数据传递,C ...
- new/delete重载
在c++中,有时我们需要在运行阶段为一个变量分配未命名的内存,并使用指针来访问它,这里就可以用到new关键字.另外需要指出的是,new分配的内存块通常与常规变量分配的内存块不同,常规变量的值都储存在被 ...
- java重载与覆写
很多同学对于overload和override傻傻分不清楚,建议不要死记硬背概念性的知识,要理解着去记忆. 先给出我的定义: overload(重载):在同一类或者有着继承关系的类中,一组名称相同,参 ...
- 【C++】多态性(函数重载与虚函数)
多态性就是同一符号或名字在不同情况下具有不同解释的现象.多态性有两种表现形式: 编译时多态性:同一对象收到相同的消息却产生不同的函数调用,一般通过函数重载来实现,在编译时就实现了绑定,属于静态绑定. ...
- C++ 运算符重载时,将运算符两边对象交换问题.
在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...
- C++重载new和delete运算符
内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...
- Java学习笔记之方法重载
被重载的方法必须具有不同的参数列表.不能基于不同修饰符或返回值类型来重载方法. package welcome; public class TestMethodOverloading { public ...
随机推荐
- C#对Excel打印时,PageSetup 对象详解
PageSetup 对象包含所有页面设置的属性(左边距.底部边距.纸张大小等).下面按“页面”.“页边距”.“页眉/页脚”.“工作表”和“无对应选项卡”五个类别,逐一介绍. 一.页面 与“页面”选项卡 ...
- ylb:SQL Server中的时间函数
ylbtech-SQL Server:SQL Server-SQL Server中的时间函数 SQL Server中的时间函数. 1,SQL Server中的时间函数 返回顶部 1. 当前系统日期 ...
- 5、NFC概述
什么是NFC NFC(Near Field Communication,近场通信),是一种数据传输技术.但与Wi-Fi.蓝牙.红外线等数据传输技术的一个主要差异就是有效距离一般不能超过4厘米. NFC ...
- 长轮询和Comet
长轮询方式是由前端定时发起AJAX请求,若请求到数据则把数据显示出来. comet方式是由客户端与服务器端发起一个长连接,然后客户端通过监听事件的方式,来对服务器端返回的数据作出响应和处理. 实时性要 ...
- 十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)
//注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版得到的字符串须经过Utf8ToAnsi()转码才能跟C#版得到的字符串显示结果一致. ...
- HDU5804 Price List (BestCoder Round #86 A)水题
分析:大于总和输出1 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...
- saltstack配置安装的一些关键步骤及安装时各种报错的分析
以下其他仅做参考,官方网址才是安装重点:http://docs.saltstack.cn/topics/installation/rhel.html 与安装相关的一些文档或资料: 一.linux服务器 ...
- 在VS2103环境中集成Doxygen工具
自己已将学习了两三次了吧,差不多这次该总结一下: Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C++.Java.Objective-C和IDL语言,部分支持P ...
- WCF基礎
參考:http://www.cnblogs.com/MeteorSeed/archive/2012/04/24/2399455.html http://www.cnblogs.com/scy25114 ...
- (转载)OC学习篇之---类的延展
前一篇文章我们介绍了类的类目概念和使用,那么这篇文章我们继续来介绍一下OC中的一个特性:延展. 其实说白了,延展就是弥补C语言中的前向申明,我们知道,在C语言中,如果你想调用一个函数的话,那么在此之前 ...