父类和子类的构造拷贝层级实例

通过这几天的学习,实现了一个父类和子类构造和拷贝构造的层级,方便以后参考使用

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值