类库与数据结构 实验课1 运算符重载、封装

课程简介:
基于C++的类库与数据结构学习。

第一次实验课

a)Review the concepts of class, object, inheritance, overriding and overloading in the C++ programming language
复习类、对象、集成、复写、重载等C++面向对象编程概念
b)Learn the Data Abstraction Principle, the Open-Closed Principle, and the Subclass Substitution Rule
学习面向对象编程中的数据抽象原则、开闭原则、子类替代原则
c)Practice C++ programming skills
锻炼C++编程技巧

面向对象编程

网课:黑马程序员

为什么需要运算符重载

链接: c++为什么要进行运算符重载?
一个类两个对象之间成员进行运算必须重新定义,让编译器在遇到对象运算时能按我们要求的进行运算,这就是运算符重载的意义,即重定义运算符。

运算符重载

// 不进行运算符重载的方式
   while (employee.getName() != "*")
   {
      if (employee.compare(bestPaid))
         bestPaid.copy(employee);
      employee.input();
   }
// 函数定义
void Employee::copy(const Employee &otherEmployee)
{
   name = otherEmployee.name;
   grossPay = otherEmployee.grossPay;
} // copy


bool Employee::compare(const Employee &otherEmployee) const
{
   return grossPay > otherEmployee.grossPay;
} // compare
// 进行运算符重载的方式
	while (employee.getName() != "*")
	{
		if (employee>bestPaid)
			bestPaid=employee;
		employee.input();
	} // while
// 重载过程

void Employee::operator=(const Employee& otherEmployee)
{
	//please implement this operator overloading
	name = otherEmployee.name;
	grossPay = otherEmployee.grossPay;
}

bool Employee::operator>(const Employee& otherEmployee) const
{
	//please implement this operator overloading
	return grossPay > otherEmployee.grossPay;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值