给个operator<< 和 operator>>这两个重载函数的例子 编译器VC++

woods2001 2009-06-20 09:47:18
没啥说的
来了都有分~
...全文
376 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgw26046044 2009-06-28
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<iostream>
#include<fstream.h>
#include<string.h>
//using namespace std;
class Student
{
public:
Student(char *n,char *s,int a)
{strcpy(name,n);strcpy(sex,s);age=a;}
friend ofstream& operator <<(ofstream&,Student const &);
friend ostream& operator <<(ostream&,Student const &);
friend istream& operator >>(istream&,Student&);
friend ifstream& operator >>(ifstream&,Student&);
private:
char name[20];
char sex[10];
int age;
};

ofstream& operator <<(ofstream&out,Student const & stud)
{
out<<stud.name<<endl;
out<<stud.sex<<endl;
out<<stud.age<<endl;
return out;
}

ostream& operator <<(ostream&out,Student const &stud)
{
out<<"学生姓名:"<<stud.name<<endl;
out<<"学生性别:"<<stud.sex<<endl;
out<<"学生年龄:"<<stud.age<<endl;
return out;
}

istream& operator >>(istream&in,Student&stud)
{
in>>stud.name>>stud.sex>>stud.age;
return in;
}


ifstream& operator >>(ifstream&in,Student&stud)
{
in>>stud.name>>stud.sex>>stud.age;
return in;
}

int main()
{
typedef Student * studpoincrs;
cout<<"输入你要输入的学生人数:"<<endl;
int temp;
cin>>temp;
studpoincrs *p=new studpoincrs[temp];
for(int i=0;i<temp;i++)
{
cout<<"请输入学生信息:"<<endl;
p[i]=new Student ("","",0);
cin>>*p[i];
}
for(i=0;i<temp;i++)
{
cout<<"第"<<i+1<<"名学生信息:\n"<<*p[i]<<endl;
}
system("pause");
system("cls");
fstream ifile("E:\\text.txt",ios::in);
fstream ofile("E:\\text.txt",ios::out);
if(!ifile||!ofile)
{
cerr<<"文件text.txt打开未成功。"<<endl;
exit(0);
}
for(i=0;i<temp;i++)
{
ofile<<*p[i];
/* 注意:在这里出现了错误,编者的意图是要调用
* ofstream& operator <<(ofstream&,Student const&);
* 这个重载函数,但实际上在这里它调用了
* ostream& operator <<(ostream&,Student const &);
* 这个重载函数,原因是编者并没有注意到ofile的类型是
* fstream类型,而不是ofstream类型。所以当编译系统在解释这条语句的时候,
* 它并没有找到fstream& operator <<(fstream&,Student const&);
* 这样的重载函数,又由于子类对象可以赋值给父类的引用,所以编译系统
* 只好调用:ostream& operator <<(ostream&,Student const &);
* 这个重载函数。那么,实际上程序向文件中写入的就不光是stud.name,
* stud.sex,stud.age这几个数据,还有"学生姓名","学生性别",
* "学生年龄"这几个字符串。(注:系统在调用函数时,首先检测有没有哪个函数的
* 形参符合自己类型,如果有就直接调用,如果没有它才会检测,有没有哪个函数的
* 形参符合其父类的类型,如果有它会通过子类对象向父类的引用(对象、指针)赋值
* 的方法来调用那个函数,如果仍然没有那么它才会报错。所以当
* fstream& operator <<(fstream&,Student const&);和
* ostream& operator <<(ostream&,Student const &);同时存在是系统会调用
* fstream& operator <<(fstream&,Student const&);并不会发生调用歧义。)
*/
}
cout<<"将文件text.txt中的学生信息输入到程序中。"<<endl;
system("pause");
for(i=0;i<temp;i++)
{
ifile>>*p[i]; // 这里出现了与上面同样的错误。
}
cout<<"输出从text.txt中导入的学生信息。"<<endl;
system("pause");
system("cls");
for(i=0;i<temp;i++)
{
cout<<"第"<<i+1<<"名学生信息(text文件中导入的):\n"<<*p[i]<<endl;
}
ifile.close();
ofile.close();
return 0;
}

//这个程序的运行结果出现不正确的原因,就是由于上面两处,调用<<和>>的重载函数时,出现了错误调用所导致的
//所以要使程序运行正确只需将fstream ifile("E:\\text.txt",ios::in);
// fstream ofile("E:\\text.txt",ios::out);
//中的fstream改成ifstream和ofstream就可以了。
信念 2009-06-26
  • 打赏
  • 举报
回复
很基础很实用的东西,支持
Sou2012 2009-06-21
  • 打赏
  • 举报
回复
JFF,我来接分了
yang_e_2009 2009-06-21
  • 打赏
  • 举报
回复
真的都有?

class A{
int m;
friend ostream& operator <<(ostream& os,const A& a);
};
ostream& operator <<(ostream& os,const A& a){
os << a.m;
return os;
}
ChRise11119 2009-06-21
  • 打赏
  • 举报
回复
顶一下!
刚遇到这方面的问题,正想发帖子问一下呢。
mm6688 2009-06-21
  • 打赏
  • 举报
回复
其实 楼主是来散分的
zizhuchuangxin 2009-06-20
  • 打赏
  • 举报
回复
百度一下就知道啦
Jalien 2009-06-20
  • 打赏
  • 举报
回复
来了 可没得说了
ameyume 2009-06-20
  • 打赏
  • 举报
回复
顶一下,以上所述,很全了
gleen 2009-06-20
  • 打赏
  • 举报
回复
顶一下
lambert_s 2009-06-20
  • 打赏
  • 举报
回复
来晚了。。。
pathuang68 2009-06-20
  • 打赏
  • 举报
回复
来了
老邓 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hikaliv 的回复:]
我来了。
[/Quote]
来了就有分?呵呵
2楼比较典型了
光宇广贞 2009-06-20
  • 打赏
  • 举报
回复
我来了。
woods2001 2009-06-20
  • 打赏
  • 举报
回复
主要是 VC编译器不支持
我用的是VC6.0
用VS2005报的错还更多
用G++ 可以通过
KudoCC 2009-06-20
  • 打赏
  • 举报
回复
给你个重载<<的例子,另一个就顺藤摸瓜吧,哈哈~
// class1.h
#include<iostream>
class class1
{
private:
int age;
int number;
public :
class1();
friend std::ostream& operator<<(std::ostream&,class1);
int getAge();
};

#include"class1.h"
class1::class1():age(21),number(10)
{
}
int class1::getAge()
{
return age;
}

std::ostream& operator<< (std::ostream& os,class1 c1)
{
os<<c1.number<<"\t"<<c1.age;
return os;
}
superbtl 2009-06-20
  • 打赏
  • 举报
回复
自己GOOGLE研究下吧 把所有的重载操作符都研究下啊 触类旁通 JAVA没有
pengzhixi 2009-06-20
  • 打赏
  • 举报
回复
google下
HelloDan 2009-06-20
  • 打赏
  • 举报
回复
这些网上找不知多少。
Walf_ghoul 2009-06-20
  • 打赏
  • 举报
回复
ls的给的很全了,不过依然顶起
加载更多回复(3)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧