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 ...
随机推荐
- makefile实例(1)-helloworld
简单makefile实例 1,源文件: main.cpp #include <stdio.h> int main() { printf("Hello World\n") ...
- nginx upstream的几种配备方式
nginx upstream的几种配置方式 nginx 的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔 ...
- Go 语言做的几个验证码
1.http://www.oschina.net/code/snippet_173630_12006 : 效果: 源代码: 1: package main 2: 3: import ( 4: cra ...
- Win7安装IDL8.0以及破解
参考:http://bbs.06climate.com/forum.php?mod=viewthread&tid=5230 1.下载IDL8.1和证书 下载地址:http://yunpan.c ...
- MVC用户登录方法(lamda表达式)
public bool ValidateUser(account model) { using (assertEntities db = new assertEntities()) { acc ...
- 3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频
3Com Network Supervisor与IBM Tivoli NetView两款网管软件操作视频 网管软件必须能够实实在在的给我们带来好处,对于企业网络管理来说,其作用体现在以下几个方面: ...
- 第三百二十八天 how can I 坚持
今天电脑快把我搞疯了,一天得死机快十次,不知道怎么回事,最后升级了win10,感觉就是比较好. 哎,成了这个样子,当初为什么又让我抽中了那个签,搞不懂啊,这都是为啥. 我哪里错了,还是冥冥中自有天意, ...
- MYSQL数据库性能调优之三:explain分析慢查询
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句.使用方法,在select语句前加上explain就可以了. 一.explain ...
- Wisdombud.CommonTool及其应用
@(编程) 1. 用法 student类 using System.ComponentModel; namespace WindowsFormsApplication1 { public class ...
- Gradle 1.3之前的Publishing artifacts
在Gradle1.3之前,Publishing artifacts是使用uploadConfigurationName来publish 声明artifacts是靠使用 build.gradle art ...