一.运算符重载 operator:
* 本质:运算符 operator 就是函数名
* 单目运算符: ++,--
* 前置: 先自增,后输出(执行) 注意:(引用方式返回类)
* 后置: 先输出(执行),后自增 注意:(返回一个 const 修饰的局部类)
* 双目运算符: +,-,*,/,% 和 < 、 > 、 <= 、 >= 、 == 和 =
* 位运算符: << >>
* 其他运算符: [] , () , ->
* 实现方式:
* 1.全局运算符重载
* 2.类内运算符重载
* 运算符重载难点: 运算符太多,实现方法不同,照成内容记忆混乱
* 记忆重点:双目运算符,关系运算符,<<运算符,()仿函数运算符
单目运算符重载: ++(前置,后置(int占位)) , --(前置,后置(int占位))
注意:前置返回引用 , 后置返回 const 修饰的临时变量
二.继承:
* 作用:提高代码的复用性
* 语法: 在定义类名后加上 : 权限(通常public) 继承的类
* 如果多继承则使用逗号隔开
* 构造顺序: 继承 + 成员变量 他们的构造顺序又是什么样的?
私有权限 派生类继承了 基类的所有成员,但是有部分无法使用(私有,构造,析构,友元,运算符)
但是有部分无法使用(私有,构造,析构,友元,运算符)
派生类可以 覆盖或重写 父类的成员
默认调用父类无参构造
重点:初始化列表的时候最好调用一下父类的构造,否则默认调用父类无参构造
三.多继承:
* 1.非同源多继承出现同名问题?(二义性)
* 解决方案: 通过域方式调用
* 2.继承的构造顺序是什么顺序?
* 答: 构造顺序: 从左往右 ->
* 3.继承 + 成员属性 构造顺序是什么顺序?
* 答: “至上而下,从左往右,初始化列表无关”
* B -> A -> My -> AB
* 4.同宗同源多继承出现的问题?(菱形继承)
* 造成:空间多余浪费
* 答:使用 虚继承(virtual),减少内存占用,解决调用二义性
* virtual 会形成一个虚表,而类中的virtual是指向虚表的指针
* virtual 实质是一个指针(占 4字节)
四,多态 :
* 定义条件:
* 1.必须是继承关系
* 2.父类中函数虚函数,而子类必须重写父类虚函数
* 使用条件:
* 1.父类指针/引用指向子类对象
*
* 本质: 多态实现使用的是 virtual 虚函数指针,指向一个虚函数列表
*
* 问题:按照逻辑来说,我们的动物类是无法实例化的。
* 解决方案:形成抽象类,使用纯虚函数 virtual 函数() = 0;
* 该类无法实例化,必须子类实现父类纯虚函数
* 才能实例化,否则它还是一个抽象类
* 抽象类作用:提供一个框架接口,派生类根据需求制定不同的方案
文件名:Student.h
创建者:文云龙
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
#include "score.h"
class Student
{
public:
Student(string name = "无名",int age = 18);
~Student();
/********** 运算符重载 operator *********************************
* 本质:运算符 operator 就是函数名
* 单目运算符: ++,--
* 前置: 先自增,后输出(执行) 注意:(引用方式返回类)
* 后置: 先输出(执行),后自增 注意:(返回一个 const 修饰的局部类)
* 双目运算符: +,-,*,/,% 和 < 、 > 、 <= 、 >= 、 == 和 =
* 位运算符: << >>
* 其他运算符: [] , () , ->
* 实现方式:
* 1.全局运算符重载
* 2.类内运算符重载
* 运算符重载难点: 运算符太多,实现方法不同,照成内容记忆混乱
* 记忆重点:双目运算符,关系运算符,<<运算符,()仿函数运算符
* ****************************************************/
public: //单目运算符重载: ++(前置,后置(int占位)) , --(前置,后置(int占位))
//注意:前置返回引用 , 后置返回 const 修饰的临时变量
/**************** 前置 ******************/
Student &operator ++();
Student &operator --();
/**************** 后置 ******************/
const Student operator ++(int);
const Student operator --(int);
public: //双目运算符: +,-,*,/,%
Student operator +(const Student &value);
Student operator -(const Student &value);
Student operator *(const Student &value);
Student operator /(const Student &value);
Student operator %(const Student &value);
public://双目运算符: < 、 > 、 <= 、 >= 、 ==
bool operator ==(const Student &value);
bool operator !=(const Student &value);
bool operator >(const Student &value);
bool operator >=(const Student &value);
bool operator <(const Student &value);
bool operator <=(const Student &value);
public://赋值运算符 =
Student &operator =(const Student &value);
public: //位运算符 << >>
friend ostream & operator <<(ostream &out,const Student &value);
friend istream & operator >>(istream &in,Student &value);
public: //高级运算符重载 []下标操作 ->指针操作 ()仿函数
string &operator [](int index);
bool operator [](const string &goods); //判断该人是否有香包
public://高级运算符重载 ->指针操作
Score *operator ->();
// Score operator ->(); //普通变量不成立
public://高级运算符重载 ()仿函数 该类函数通常做策略使用
bool operator ()();
/*** 谓词 返回值是bool 类型 , 参数是 1 个(一元) 或者 2个(一元) ***/
bool operator ()(string goods); //一元谓词
bool operator ()(string goods,int index);//二元谓词
private:
string m_name; //姓名
int m_age; //年龄
string m_goods[10]; //物品
Score *m_score; //成绩
// Score mm_score; //成绩附属品
};
#endif // STUDENT_H
文件名:Sudent.cpp文件
创建者:文云龙
#include "student.h"
/********* 构造函数 *****************/
Student::Student(string name, int age)
:m_name(name),m_age(age),m_score(new Score(50,60,70))
{
}
Student::~Student()
{
delete m_score;
}
/**************** 前置 ++******************/
Student &Student::operator ++()
{
++this->m_age;
return *this;
}
/**************** 前置 --******************/
Student &Student::operator --()
{
--this->m_age;
return *this;
}
/**************** 后置 ++******************/
const Student Student::operator ++(int)
{
Student temp(*this);//拷贝构造(定义一个Student类对象拷贝this的值)
this->m_age++;//因为是后加加的所有Student的值改变但temp输出的值不改变(内部做了,但外面输出看不到他做了)
return temp;
}
/**************** 后置 --******************/
const Student Student::operator --(int)
{
Student temp(*this);
this->m_age--;
return temp;
}
/**************** +号运算符重载 ******************/
Student Student::operator +(const Student &value)
{
Student temp("无名",0);
temp.m_age = this->m_age + value.m_age;
return temp;
}
/**************** -号运算符重载 ******************/
Student Student::operator -(const Student &value)
{
Student temp("无名",0);
temp.m_age = this->m_age - value.m_age;
return temp;
}
/**************** *号运算符重载 ******************/
Student Student::operator *(const Student &value)
{
Student temp("无名",0);
temp.m_age = this->m_age * value.m_age;
return temp;
}
/**************** /号运算符重载 ******************/
Student Student::operator /(const Student &value)
{
Student temp("无名",0);
temp.m_age = this->m_age / value.m_age;
return temp;
}
/**************** %号运算符重载 ******************/
Student Student::operator %(const Student &value)
{
Student temp("无名",0);
temp.m_age = this->m_age % value.m_age;
return temp;
}
/**************** ==号运算符重载 ******************/
bool Student::operator ==(const Student &value)
{
return this->m_age == value.m_age; //年龄
// return this->m_name == value.m_name; //姓名
// return this->m_age == value.m_age && this->m_name == value.m_name; //联合(姓名 和 年龄)
}
/**************** !=号运算符重载 ******************/
bool Student::operator !=(const Student &value)
{
return this->m_age != value.m_age; //年龄
// return this->m_name != value.m_name; //姓名
// return this->m_age != value.m_age && this->m_name != value.m_name; //联合(姓名 和 年龄)
}
/**************** >号运算符重载 ******************/
bool Student::operator >(const Student &value)
{
return this->m_age > value.m_age; //年龄
// return this->m_name > value.m_name; //姓名
// return this->m_age > value.m_age && this->m_name > value.m_name; //联合(姓名 和 年龄)
}
/**************** >=号运算符重载 ******************/
bool Student::operator >=(const Student &value)
{
return this->m_age >= value.m_age; //年龄
// return this->m_name >= value.m_name; //姓名
// return this->m_age >= value.m_age && this->m_name >= value.m_name; //联合(姓名 和 年龄)
}
/**************** <号运算符重载 ******************/
bool Student::operator <(const Student &value)
{
return this->m_age < value.m_age; //年龄
// return this->m_name < value.m_name; //姓名
// return this->m_age < value.m_age && this->m_name < value.m_name; //联合(姓名 和 年龄)
}
/**************** <=号运算符重载 ******************/
bool Student::operator <=(const Student &value)
{
return this->m_age <= value.m_age; //年龄
// return this->m_name <= value.m_name; //姓名
// return this->m_age <= value.m_age && this->m_name <= value.m_name; //联合(姓名 和 年龄)
}
/**************** =号运算符重载 ******************/
Student &Student::operator =(const Student &value)
{
this->m_age = value.m_age;
this->m_name = value.m_name;
cout << "=号 赋值运算符" << endl;
/*******************************************
*深拷贝 申请空间 new 例如 addr 是一个指针
* this->addr = new char[20]; //申请空间
* strcpy(this->addr,value.addr);
* ****************************************/
return *this;
}
/*************** 高级运算符重载 [] **************/
string &Student::operator [](int index)
{
return this->m_goods[index];
}
/*************** []判断该人是否有香包 ************/
bool Student::operator [](const string &goods)
{
for(int i = 0; i < sizeof(this->m_goods)/sizeof(this->m_goods[0]);i++)
{
if(goods == this->m_goods[i])
{
return true;
}
}
return false; //没有该物品
}
/*************** 高级运算符重载 -> ************/
Score *Student::operator ->()
{
return this->m_score; //指针
}
/*************** 高级运算符重载 () ************/
bool Student::operator ()()
{
cout << "仿函数()方法" << endl;
return true;
}
bool Student::operator ()(string goods)
{
cout << "仿函数()一元谓词 方法" << endl;
return goods == "蓝牙耳机";
}
bool Student::operator ()(string goods, int index)
{
cout << "仿函数()二元谓词 方法" << endl;
return goods == "蓝牙耳机" && index == 1;
}
//Score Student::operator ->() //普通变量不成立
//{
// return this->mm_score;
//}
/**************** <<号运算符重载 ******************/
ostream & operator <<(ostream &out,const Student &value)
{
out << "\t姓名:" << value.m_name
<< "\t年龄:" << value.m_age;
/************* 增加部分 ***********/
out << "物品: ";
for(int i = 0; i < sizeof(value.m_goods)/sizeof(value.m_goods[0]);i++)
{
out << value.m_goods[i] << " ";
}
out << endl;
/***********************************/
return out;
}
/**************** >>号运算符重载 ******************/
istream & operator >>(istream &in,Student &value)
{
cout <<"请输入姓名:";
in >> value.m_name;
cout <<"请输入年龄:";
in >> value.m_age;
return in;
}