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 ...
随机推荐
- Sciter使用心得
1. div双击事件 $(div).onMouse = function(evt) { switch(evt.type) { case Event.MOUSE_DCLI ...
- 将cocos2dx项目从Visual Studio 迁移到 xcode
因为Visual Studio和XCode的巨大差异性,一开始选择任何一个IDE,都会有一个迁移的过程,XCode的迁移到Visual Studio相对非常简单,不用再介绍.将项目从Visual St ...
- Mysql字符串截取函数SUBSTRING的用法说明
感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ...
- 20+非常棒的Photoshop卡通设计教程
现在把一个人的脸变成卡通图案再用它来当头像这种现象使非常常见的,同样的卡通插图可以用于多种渠道的设计.网上有很多公司都会创立一种吉祥物并把它应用到市场营销中.因为有了类似于photoshop这样强大的 ...
- Tachyon框架的Worker心跳及Master高可用性分析
0 概述 分布式框架中的Master-Slave类型,Slave节点负责工作的具体执行,Master负责任务的分发或者相关元数据的存储等.一般情况下,一个Master节点都会对应多个Slave节点,M ...
- 【openstack报错】【因更新包而致】IncompatibleObjectVersion: Version 1.9 of Instance is not supported
[时间]2014年2月18日 [平台]ubuntu 12.04.3 openstack havana [日志]/var/log/upstart/nova-compute.log 内容如下: ERRO ...
- URAL-1981 Parallel and Perpendicular 水题
题目链接:http://www.cnblogs.com/zhsl/p/3395868.html 水题,注意细节. //STATUS:C++_AC_31MS_333KB #include <fun ...
- Matlab 图像画在坐标轴下
>> x=linspace(,*pi,); >> y=sin(x); >> figure;plot(x,y,'r-') >> set(gca,'xaxi ...
- Nginx下Redmine2.6配置
Apache下Redmine2.6配置,请参考:http://blog.linuxeye.com/405.html CentOS6.6下安装Redmine2.6+MySQL5.6+Nginx,教程如下 ...
- cocos2d-x3.2下获取文件夹下所有文件名的方法
这里提供一个函数获取文件夹下所有文件名的方法,直接上代码了. 原文地址:http://blog.csdn.net/qqmcy/article/details/36184733 // // Visib ...