C++ 派生于继承const string& name=string(),中const 作用是什么?name前面为什么加&;源代码如下:谢谢
#include<iostream>#include<string>usingnamespacestd;classCPerson{private:stringname;c...
#include <iostream>
#include <string>
using namespace std;
class CPerson{
private:
string name;
char sex;//f-female,m-male
int age;
string work;
public:
CPerson(const string& name=string(),
const char& sex=char(),
const int& age=int(),
const string& work=string()):name(name),
sex(sex),
age(age),
work(work){}
virtual ~CPerson(){};
void showName()const{
cout<<"Name:\t"<<name<<endl;
}
void showSex()const{
cout<<"Sex:\t"<<sex<<endl;
}
void showAge()const{
cout<<"Age:\t"<<age<<endl;
}
void showWork()const{
cout<<"Work:\t"<<work<<endl;
}
virtual void showAllInfo()const{
showName();
showSex();
showAge();
showWork();
}
};
class CStudent : public CPerson{
string school;
string subject;
int grade;
public:
CStudent(const string& name=string(),
const char& sex=char(),
const int& age=int(),
const string& work=string(),
const string& school=string(),
const string& subject=string(),
const int& grade=int()
):CPerson(name,sex,age,work),school(school),
subject(subject),
grade(grade){}
void showSchool()const{
cout<<"School:\t"<<school<<endl;
}
void showSubject()const{
cout<<"Subject:"<<subject<<endl;
}
void showGrade()const{
cout<<"Grade:\t"<<grade<<endl;
}
void showAllInfo()const{
CPerson::showAllInfo();
showSchool();
showSubject();
showGrade();
}
};
int main()
{
CPerson cp("Green",'f',18,"teacher");
cp.showAllInfo();
cout<<"==================="<<endl;
CStudent cs("Tom",'m',15,"student","Qinhua University","Math",2011);
cs.showAllInfo();
return 0;
}
void showAllInfo()const{
CPerson::showAllInfo();
showSchool();
showSubject();
showGrade();}的作用与意义是什么???? 展开
#include <string>
using namespace std;
class CPerson{
private:
string name;
char sex;//f-female,m-male
int age;
string work;
public:
CPerson(const string& name=string(),
const char& sex=char(),
const int& age=int(),
const string& work=string()):name(name),
sex(sex),
age(age),
work(work){}
virtual ~CPerson(){};
void showName()const{
cout<<"Name:\t"<<name<<endl;
}
void showSex()const{
cout<<"Sex:\t"<<sex<<endl;
}
void showAge()const{
cout<<"Age:\t"<<age<<endl;
}
void showWork()const{
cout<<"Work:\t"<<work<<endl;
}
virtual void showAllInfo()const{
showName();
showSex();
showAge();
showWork();
}
};
class CStudent : public CPerson{
string school;
string subject;
int grade;
public:
CStudent(const string& name=string(),
const char& sex=char(),
const int& age=int(),
const string& work=string(),
const string& school=string(),
const string& subject=string(),
const int& grade=int()
):CPerson(name,sex,age,work),school(school),
subject(subject),
grade(grade){}
void showSchool()const{
cout<<"School:\t"<<school<<endl;
}
void showSubject()const{
cout<<"Subject:"<<subject<<endl;
}
void showGrade()const{
cout<<"Grade:\t"<<grade<<endl;
}
void showAllInfo()const{
CPerson::showAllInfo();
showSchool();
showSubject();
showGrade();
}
};
int main()
{
CPerson cp("Green",'f',18,"teacher");
cp.showAllInfo();
cout<<"==================="<<endl;
CStudent cs("Tom",'m',15,"student","Qinhua University","Math",2011);
cs.showAllInfo();
return 0;
}
void showAllInfo()const{
CPerson::showAllInfo();
showSchool();
showSubject();
showGrade();}的作用与意义是什么???? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询