重载运算符
//重载+运算符
const Integer operator+(const Integer & other) const;
const Integer operator-(const Integer & other) const;
const Integer operator*(const Integer & other) const;
const Integer operator/(const Integer & other) const;
const Integer operator%(const Integer & other) const;
//重载赋值运算符,需要const修饰函数
//num4.operator = (num3)
const Integer & operator = (const Integer & other);
//二元运算符建议采用友元得方式重载
friend const Integer operator + (int intvalue, const Integer & other)
//流运算符一般智能使用友元函数进行重载
friend ostream & operator << (ostream & out , const Integer & num);
//友元函数不需要使用域运算符了
const Integer operator +( int intvalue , const Integer & other)
{
return Integer(intvalue + other.m_value);
}
//流运算符友元函数重载
继承和派生
在C++中 代码重用通过继承机制实现
- 继承就是在一个已有类得基础上,建立一个新类
- 类可升级可维护
- 基类定义了公共得内容,方便统一修改
- 代码得可重用性:类库
- 添加新类,新成员方便
注意:
- 派生类对象包含了基类得数据成员,派生类继承了积累得实现
- 派生类对象可以使用基类得非私有函数
- 派生类需要自己的构造函数
- 派生类可以添加额外的数据成员和函数
1、公有继承
//hero.h文件 #ifndef HER0_H #define HER0_H #include#include using namespace std; class Hero { private: string m_NickName; int m_lever; int m_MaxLife; int m_CurrLife public: Hero(); void Move(); } class Warrior :public Hero { //战士特有的成员和函数 } class Archmage :public Hero { //法师特有的成员和函数 } #endif //HER0_H



