课程简介:
基于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;
}