类重载
来源:互联网 发布:淘宝网红店铺排名2016 编辑:程序博客网 时间:2024/05/05 16:22
//输入输出运算符只能用友元函数重载
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
class Complex
{
double re,im;
public:
Complex(double r,double i):re(r),im(i)
{
}
Complex()
{
re = 0;
im = 0;
}
Complex operator!();
Complex operator+(const Complex &obj);
//重载输入输出运算符,只能用友元函数
friend ostream &operator<<(ostream &os,const Complex &c);
friend istream &operator>>(istream &is,Complex &c);
};
Complex Complex::operator +(const Complex &obj)
{
Complex temp;
temp.re = re + obj.re;
temp.im = im + obj.im;
return temp;
}
Complex Complex::operator !()
{
Complex temp;
temp.re = -re;
temp.im = -im;
return temp;
}
ostream & operator<<(ostream &os,const Complex &c)
{
os << c.re;
if(c.im > 0)
os << "+" << c.im << "i" << endl;
else
os << c.im << "i" << endl;
return os;
}
istream & operator>>(istream &is,Complex &c)
{
is >> c.re >> c.im;
return is;
}
int main(int argc,char *argv[])
{
Complex obj1(1,2),obj2(3,4);
Complex obj3 = obj1 + !obj2;
cout << obj3;
cin >> obj3;
cout << obj3;
return 0;
}
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
class Complex
{
double re,im;
public:
Complex(double r,double i):re(r),im(i)
{
}
Complex()
{
re = 0;
im = 0;
}
Complex operator!();
Complex operator+(const Complex &obj);
//重载输入输出运算符,只能用友元函数
friend ostream &operator<<(ostream &os,const Complex &c);
friend istream &operator>>(istream &is,Complex &c);
};
Complex Complex::operator +(const Complex &obj)
{
Complex temp;
temp.re = re + obj.re;
temp.im = im + obj.im;
return temp;
}
Complex Complex::operator !()
{
Complex temp;
temp.re = -re;
temp.im = -im;
return temp;
}
ostream & operator<<(ostream &os,const Complex &c)
{
os << c.re;
if(c.im > 0)
os << "+" << c.im << "i" << endl;
else
os << c.im << "i" << endl;
return os;
}
istream & operator>>(istream &is,Complex &c)
{
is >> c.re >> c.im;
return is;
}
int main(int argc,char *argv[])
{
Complex obj1(1,2),obj2(3,4);
Complex obj3 = obj1 + !obj2;
cout << obj3;
cin >> obj3;
cout << obj3;
return 0;
}
0 0
- 类重载
- 类重载
- override/重载 接口/类
- 重载TCLCL中的类
- CTreeCtrl重载类
- Point类重载
- 抽象类的重载
- 类 操作符 重载
- python 类 成员,重载
- 时间类输出重载
- 时间类输出重载
- 类运算符重载
- oop_day02_类、重载_20150810
- 分数类重载
- 类和对象&重载
- 重载 QApplication 类
- Python 类,函数重载
- 类中的函数重载
- 在Win7系统上利用光盘启动盘安装Ubuntu 14.04(双系统、各自独立)
- 屏蔽右键菜单(JS)
- TF-IDF算法:自动提取关键字
- Web导出excel的几种方法
- 二次事故预警系统
- 类重载
- 数据库的范式化和反范式化模型
- oc基本动画实现
- trie树——字典树
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- SVN Unable to connect to a repository at URL
- java语言实现号码归属地查询
- maven的详细讲解----学习笔记
- 打开MySQL数据库远程访问的权限