通过这几天的学习,实现了一个父类和子类构造和拷贝构造的层级,方便以后参考使用
class Birtday;
class Birtday { //父类
public:
Birtday(int year, int month, int day) :_year{ year }, _month{ month }, _day{ day }{}
Birtday(const Birtday& another) {
this->_day = another._day;
this->_year = another._year;
this->_month = another._month;
}
Birtday& operator=(const Birtday& another) {
if (&another == this)
return *this;
this->_day = another._day;
this->_year = another._year;
this->_month = another._month;
return *this;
}
private:
int _year;
int _month;
int _day;
};
class Student { //内嵌类
public:
Student(string name,char sex,float score):_name(name),_sex(sex),_score(score){}
Student(const Student& another) {
this->_name = another._name;
this->_sex = another._sex;
this->_score = another._score;
}
Student& operator=(const Student& another) {
if (&another == this)
return *this;
this->_name = another._name;
this->_sex = another._sex;
this->_score = another._score;
return *this;
}
virtual void show() = 0;
protected:
string _name;
char _sex;
float _score;
};
class Gradute :public Student { //子类
public:
Gradute(string name, char sex, float score, float salary, int year, int month, int day) :Student{name, sex, score},birth(year,month,day){
this->_salary = salary;
}
Gradute(const Gradute& another):Student(another),birth(another.birth){ //要自实现
this->_salary = another._salary;
}
Gradute& operator=(const Gradute& another) {
if (&another == this)
return *this;
Student::operator=(another);
birth = another.birth;
this->_salary = another._salary;
return *this;
}
void show() override {
cout << _name << " " << _sex << " " << _score <<" "<<_salary<<endl;
}
protected:
float _salary;
Birtday birth; //内嵌类
};
class Doctor :public Gradute { //子类
public:
Doctor(string name, char sex, float score, float salary, int year, int month, int day, string title)
:Gradute(name, sex, score, salary, year, month, day) {
this->_title = title;
}
Doctor(const Doctor& another) :Gradute(another) {
this->_title = another._title;
}
Doctor& operator=(const Doctor& another){
if (this == &another)
return *this;
Gradute::operator=(another);
this->_title = another._title;
return *this;
}
void show() override {
cout << _name << " " << _sex << " " << _score << " " << _salary <<" "<<_title<< endl;
}
private:
string _title;
};