我在百度上回答的一个关于类的问题

来源:互联网 发布:java 解析json 编辑:程序博客网 时间:2024/04/28 10:19

首先声明是很简单的一个问题,就是关于类的基本问题。只是我觉得有点经验就应当赶紧积累记录一下。

问题地址:http://zhidao.baidu.com/question/196106132.html

以下是一个学生类(Student)的类定义部分,请完成相应的类的实现部分,并编写相应的main函数测试相应的功

悬赏分:10 - 离问题结束还有 14 天 21 小时
#include<iostream.h>class Student{private: int number;//学号 char name[20];//姓名 Date birth;//出生日期,为日期类的对象public: Student(); Student(int n,char *m,Date d); Student(const Student &s); ~Student(); void setnumber(int *n);//设置学号 void setname(char *s);//设置姓名 void setbirth(Date d);//设置出生日期 void printstudent();//输出各成员的值}; 
下面是我的回答:
1.我建议楼主用string类型。我写的代码就是用string类型的,个人觉得比较方便使用,如果你想减少内存开销的话可以用你用的那几种数据类型,因为有些学号里面有字母,而名字长度也是不确定的,char name[20]只能接纳9个汉字,当然这已经足够用了,我懂得,有了string这种类型就不必再为生日定义另外一种数据类型的。2.以下是我写的一些代码和测试用例,希望对你有些帮助和启发:#include<iostream>#include<string>using namespace std;class Student{private: string NO;//学号 string Name;//姓名 string Birthday;//出生日期,为日期类的对象public: Student(){};//string 型变量默认值为空所以不需要构造函数对其进行初始化了,如果有其他类型变量请自行添加 Student(const string& NO,const string& Name,const string& Birthday); Student(const Student& Obj); ~Student(); string& SetNO(const string& NO) ;//设置学号 string& SetName(const string& Name);//设置姓名 string& SetBirthday(const string& Birthday);//设置出生日期 void DisplayStu();//输出各成员的值}; Student::Student(const string& NO,const string& Name,const string& Birthday){ this->NO=NO; this->Name=Name; this->Birthday=Birthday;}Student::Student(const Student& Obj){ this->NO=Obj.NO; this->Name=Obj.Name; this->Birthday=Obj.Birthday;}Student::~Student(){ NO=Name=Birthday=""; }string& Student::SetNO(const string& NO) //设置学号{ return this->NO=NO;}string& Student::SetName(const string& Name)//设置姓名{ return this->Name=Name;}string& Student::SetBirthday(const string& Birthday) //设置出生日期{ return this->Birthday=Birthday;}void Student::DisplayStu() //输出各成员的值{ cout<<"学生学号:"<<NO<<endl; cout<<"学生姓名:"<<Name<<endl; cout<<"学生生日:"<<Birthday<<endl;}int main(){ Student stu; stu.DisplayStu(); Student a("110119120", "RapeQQ(SBQQ)","2012.01.01"); a.DisplayStu(); Student b(a); b.DisplayStu(); b.SetNO("10086"); b.SetName("My Father Is LiGang, You Know!"); b.SetBirthday("1111.11.11(光棍之年的光棍节!)"); b.DisplayStu(); return 0;} 
/////////////////////////////////////////////////////////////////////////////////////////
Student::Student(const string& NO,const string& Name,const string& Birthday){ this->NO=NO; this->Name=Name; this->Birthday=Birthday;}Student::Student(const Student& Obj){ this->NO=Obj.NO; this->Name=Obj.Name; this->Birthday=Obj.Birthday;}这里引起了我的注意:
Student::Student(const Student& Obj)我本想{Student(Obj.NO,Obj.Name,Obj.Birthday);}来实现可视发现编译通不过,
我想了一下应该是这样的,由于这两个函数都属于构造函数因此编译的时候优先级是相同的,即只能执行一个,
执行了Student(const Student& Obj)之后就对构造函数进行了重载默认就不执行
Student(const string& NO,const string& Name,const string& Birthday)了,于是就会在编译的时候通不过,
就像一件事情A、B、C都能单独完成,我把一个任务交给A去单独执行,那么当A去做这件事的时候就不可能命令B去做这件事,
因为他们的关系是同事关系,是平等的,而我是他们的BOSS有权指派谁来做什么工作。
原理大概就是这样,小记一下。人来的不少,我很欣慰!
////////////////////////////////////////////////////////////////////////////////////////
原创粉丝点击