/*c++:封装一个CStudent类,用来描述学生的属性和行为。具体要求如下。
1.学生有姓名,籍贯,学号,年龄,成绩五个成员数据,编写构造函数,,
拷贝构造函数,同时编写Display(),成员函数显示学生的信息。
2.编写“+”运算符重载函数,使CStudent类的
两个对象相加返回两个对象总成绩相加的和。
3.编写主函数,定义两个CStudent类对象,分别调用成员函数Display(),
显示两个对象的学生信息,同时显示两个对象相加的结果。
*/
#include <iostream>
#include <String>
using namespace std;
class CStudent
{
private:
int number;
string name;
string hometown;
int age;
double score;
public:
CStudent(int n=1,string nm="",string hm="",int ag=0,double sc=0)
{
cout<<"structor......\n";
number=n;
name=nm;
hometown=hm;
age=ag;
score=sc;
}
CStudent(CStudent &s)
{
cout<<"copy constructor....\n";
number=s.number;
name=s.name;
hometown=s.hometown;
age=s.age;
score=s.score;
}
void Display()
{
cout<<"number\tname\thometown\tage\tscore\n";
cout<<number<<"\t"<<name<<"\t"<<hometown<<"\t"<<age<<"\t"<<score<<endl;
}
double operator+(CStudent &s)
{
return score+s.score;
}
};
int main()
{
CStudent s1(1,"张三","北京",23,87.5);
CStudent s2(2,"李四","上海",22,91);
s1.Display();
s2.Display();
cout<<"成绩和为"<<s1+s2<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考