| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 460 人关注过本帖
标题:>>的重载
只看楼主 加入收藏
heliujin
Rank: 2
等 级:论坛游民
帖 子:249
专家分:14
注 册:2006-3-14
结帖率:100%
收藏
 问题点数:0 回复次数:3 
>>的重载

我这个程序没实现了,请大家帮忙指教一下如何在MAIN()函数里 把重载>>运算符的功能体现出来啊 写几行代码就可以了 谢谢大家了
#include<iostream.h>

class a
{
public:

a(){}

a(int _m):m(_m){}

friend ostream& operator << (ostream& out,const a& other)
{
out<<other.m;
return out;
}

friend istream& operator >> (istream& in,const a& other)
{
in>>other.m;
return in;
}

private:
int m;
};


int main()
{
a p(3);
cout<<p<<endl;
int i;
cin>>i;
a q;
cin>>q(4);
cout<<q;
return 0;
}

搜索更多相关主题的帖子: 重载 
2006-06-14 20:43
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

#include<iostream.h>

class a
{
public:

a(){}

a(int _m):m(_m){}

friend ostream& operator << (ostream& out,const a& other)
{
out<<other.m;
return out;
}

friend istream& operator >> (istream& in, a& other)//去掉 const
{
in>>other.m;
return in;
}

private:
int m;
};


int main()
{
a p(3);
cout<<p<<endl;
int i;
cin>>i;
a q;
cin>>q;//不要象你那样q(4),要写成q(4)只能在定义时调用构造函数
cout<<q;
return 0;
}


woyaochengshuyidianle 我真的什么也不会
2006-06-14 22:02
heliujin
Rank: 2
等 级:论坛游民
帖 子:249
专家分:14
注 册:2006-3-14
收藏
得分:0 
请问楼上的朋友,为什么要去掉const啊?我有点不明白
2006-06-15 07:52
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
>>
是在写入数据呢
你这里const,对象不能修改.那还能写入了吗

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-06-15 08:59
快速回复:>>的重载
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030065 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved