救命啊, 关于重载的,高分奖励
#include<stdio.h>#include<stdlib.h>
#include<iostream>
#include<string>
#include<conio.h>
#include<vector>
#include<stdlib.h> // exit
using namespace std;
//重载+,-,=,+=,<<,>>
class Text
{
public:
Text(){}
Text(string name);
friend Text operator +(const Text &,const Text &);
void input(istream &ins);
void output(ostream &outs);
private:
string name;
double score;
};//友元函数能像一个成员函数那样访问雷的私有成员,简化函数定义,提高效率
//执行的任务涉及多个对象,使用非成员函数!!!(友元)
int main()
{
Text a,b,c,d;
a.input(cin);
b.input(cin);
c = a + b;
c.output(cout);
system("pause");
return 0;
}
Text::Text(string name): name(name), score(0.0)
{
}
void Text::input(istream &ins)
{
cout<<"input the name:"<<endl;
ins>>name;
cout<<"input the score:"<<endl;
ins>>score;
}
Text operator +(const Text &first,const Text &second)
{
Text temp;
temp.score = first.score + second.score;
return temp;
}
void Text::output(ostream &outs)
{
outs<<name<<endl
<<score;
}
[此贴子已经被作者于2005-8-28 10:27:56编辑过]