栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++调用父类的重载运算符

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++调用父类的重载运算符

有关运算符重载部分https://blog.csdn.net/qq_58665528/article/details/122420553 

#include 

class Person {
private:
	int m_age;

public:
	Person(int age) : m_age(age){}

	Person& operator=(const Person& person) {
		this->m_age = person.m_age;

		return *this;
	}

	void getAge() {
		std::cout << this->m_age << std::endl;
	}
};

class Student : public Person {
private:
	int m_id;

public:
	Student(int age = 0, int id = 0) : Person::Person(age), m_id(id){}	//初始化父类和子类的私有变量

	Student& operator=(const Student& student) {
		Person::operator=(student);	//调用父类的重载运算符,这样父类的私有变量也能赋值了
		this->m_id = student.m_id;

		return *this;
	}

	void getIdAge() {
		Person::getAge();
		std::cout << this->m_id << std::endl;

	}
};

int main(void) {
	Student s = Student(10, 2001);
	Student ss = s;

	ss.getIdAge();
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号