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; }