pre{
line-height:1;
color:#1e1e1e;
background-color:#d2d2d2;
font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-weight:bold;}
.selfFuc{color:#800080;}
.bool{color:#d2576f;}
.condition{color:#000080;font-weight:bold;}
.key{color:#000080;}
.var{color:#800000;font-style:italic;}
.Digit{color:#ff00ff;font-weight:bold;}
.includePre{color:#1e1e1e;}
.operator {color:#008000;font-weight:bold;}

操作符重载

 
#include <iostream>
#include <string>
using namespace std;
 
//定义水果类
 class Fruit
{
  string name;
  string colour;
public:
         friend istream& operator>>(istream&,Fruit&);                    // 输入流
         friend ostream& operator<<(ostream& out,const Fruit& fruit);       // 输出流
         void print()
         {
                   cout<<colour<<" "<<name<<endl;
         }
 
         Fruit(const string nst="apple",const string cst="green"):name(nst),colour(cst)
         {
         }
 
         ~Fruit()
         {
         }
};
 
 
//重载操作符
ostream& operator<<(ostream& out, const Fruit &s)
{
         out<<s.colour<<" "<<s.name<<endl;
         return out;
 
}
 
istream& operator>>(istream& in,Fruit &s)
{
         in>>s.colour>>s.name;
         if (!in)
         {
                   cerr<<"Wrong input!"<<endl;
         }
         return in;
}
 
 
 

主程序:

         Fruit apple;
         cin>>apple;
         cout<<apple;
 

本文使用 书画小说软件 发布,内容与软件无关,书画小说软件 更惬意的读、更舒心的写、更轻松的发布。

 

操作符重载.xml的更多相关文章

  1. c++ 操作符重载和友元

    操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...

  2. paip.操作符重载的缺失 Java 的一个大缺点

    paip.操作符重载的缺失 Java 的一个大缺点 #----操作符重载的作用 1.提升用户体验 操作符重载..可以让代码更加自然.... 2.轻松实现代码代码移植 例如   java代码会直接移植到 ...

  3. C#中如何利用操作符重载和转换操作符

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  4. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  5. C++一些注意点之操作符重载

    重载操作符需要注意 (1)重载操作符必须具有一个类类型操作数.不能重载内建类型的操作符. operator +(int,int);//这个是错误的,都为内建类型 operator +(int,clas ...

  6. C++基础学习笔记----第十三课(操作符重载-下)

    本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...

  7. 再议Swift操作符重载

    今天我们来谈一谈Swift中的操作 符重载,这一功能非常实用,但是也相当有风险.正所谓“能力越大责任越大”,这句话用来形容操作符重载最合适不过了.它可以令你的代码更加简洁,也可以让 一个函数调用变得又 ...

  8. C++中的操作符重载

    一.什么是操作符重载 操作符重载可以分为两部分:“操作符”和“重载”.说到重载想必都不陌生了吧,这是一种编译时多态,重载实际上可以分为函数重载和操作符重载.运算符重载和函数重载的不同之处在于操作符重载 ...

  9. C++ operator overload -- 操作符重载

    C++ operator overload -- 操作符重载 2011-12-13 14:18:29 分类: C/C++ 操作符重载有两种方式,一是以成员函数方式重载,另一种是全局函数. 先看例子 # ...

随机推荐

  1. MAVEN ERROR: unable to find valid certification path to requested target 解决办法

    第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central ...

  2. php监听客户端连接状态

    http://bbs.csdn.net/topics/390661022 http://www.poluoluo.com/jzxy/201207/169977.html http://zhidao.b ...

  3. ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 【转】

    转自:http://blog.chinaunix.net/uid-28458801-id-3494646.html ARM处理器工作模式一共有 7 种 : USR  模式    正常用户模式,程序正常 ...

  4. (5)XML转化成TXT

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  5. ffmpeg mp4转yuv

    mp4转yuv ffmpeg -i test.mp4 test.yuv 播放yuv ffplay.exe -f rawvideo -video_size 1280x720 -i test.yuv

  6. Eclipse 修改debug当前行的颜色

    window --preferences--general--editors--text editors--annotations--debug current instruction pointer

  7. urllib.request ProxyHandler

    import urllib.request proxy_support = urllib.request.ProxyHandler({}) opener = urllib.request.build_ ...

  8. HDU 4757 Tree(可持久化trie)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4757 题意:给出一棵树,节点有权值.每次询问x到y的路径上与z抑或的最大值. 思路:可持久化trie. ...

  9. java 文件text的写入

    日志文件 OutputStream out = new FileOutputStream(f, true); if (list1.size() > 0) { int h1 = 0; for (i ...

  10. android 使用代码实现 RelativeLayout布局

    只需把代码copy进入oncreate方法即可运行 RelativeLayout rl = new RelativeLayout(this); Button btn1 = new Button(thi ...