C++ 多态

第八章  多态性
注:参考东南大学《C++程序设计教程》视频,主讲老师:何洁月。此内容为自学时所记笔记
多态性:指发出同样的消息被不同类型的对象接受时导致完全不同的行为。
消息:主要指对类的成员函数的调用
实现:函数重载------静态
      运算符重载----静  ---编译时
      虚函数--------动态---运行时

联编:计算机程序自身彼此关联的过程

运算符重载:对已有的运算符赋予多重含义
实现机制:运算表达式->运算符函数的调用,运算符选择遵循函数重载的选择原则。

规则:
只能重载,不改变原运算符的优先级和结合性。
不改变操作数个数,只能根据类型判别。
声明形式:
函数类型 operator 运算符(形参)
{
     ......
}
(1)类成员函数
(2)友元函数。
双目运算符

#includeusing namespace std;
class complex
{
public :
    complex(){}
    complex(double r,double i);
    complex operator +(complex c);
    complex operator -(complex c);
    void print();
private:
    double real,image;
};
complex::complex(double r,double i){
    real=r;
    image=i;
}
complex complex::operator+(complex c){
    complex cc;
    cc.image=c.image+image;  //image real 省略了this.
    cc.real=c.real+real;  //this指向调用对象的地址
    return complex(cc.real,cc.image);
}
void complex::print(){
    cout<<real<<"+i*"<<image<<endl;
}
int main()
{
      complex c1(1,2);
      complex c2(3,4);
      complex c3;
      c3=c1+c2;
      c1.print();
      c2.print();
      c3.print();
      return 0;
}
单目运算符:
oprd++    void operator ++ (int );
++oprd    void operator ++ ();


#includeusing namespace std;
class complex
{
public :
    complex(){}
    complex(double r,double i);
    complex operator +(complex c);
    void operator ++();  //无返回值
    void operator ++(int );  //后加加
    complex operator -(complex c);
    void print();
private:
    double real,image;
};
complex::complex(double r,double i){
    real=r;
    image=i;
}
complex complex::operator+(complex c){
    complex cc;
    cc.image=c.image+image;  //image real 省略了this.
    cc.real=c.real+real;  //this指向调用对象的地址
    return complex(cc.real,cc.image);
}
void complex::operator ++(){
      real++;
      image++;
}
void complex::operator ++(int){
      real++;
      image++;
      cout<<"调用的是a++"<<endl;
}
void complex::print(){
    cout<<real<<"+i*"<<image<<endl;
}
int main()
{
      complex c3(1,2);
      c3.print();
      ++c3;
      c3.print();
      c3++;
      c3.print();
      return 0;
}


运算符友元函数的设计
对于外部函数,可以操作对象的私有成员,要将其重载为该类的友元函数即可
形参为从左之右的各操作数,主函数不变
友元函数的作用:破坏私有性

#includeusing namespace std;
class complex
{
public :
    complex(){}
    complex(double r,double i);
    friend complex operator +(complex c1,complex c2);
    friend complex operator -(complex c1,complex c2);
    void print();
private:
    double real,image;
};
complex::complex(double r,double i){
    real=r;
    image=i;
}
complex operator +(complex c1,complex c2){  //直接的函数,注意区别
  //友元函数,可以直接使c1,c2的私有成员
  return complex(c1.real+c2.real,c1.image+c2.image);
}
void complex::print(){
    cout<<real<<"+i*"<<image<<endl;
}
int main()
{
        complex c1(1,2);
      complex c2(3,4);
      complex c3;
      c3=c1+c2;
      c1.print();
      c2.print();
      c3.print();
      return 0;
}

静态联编:编译阶段就已经确定操作调用
动态联编:程序运行时确定要调用的函数
静态:

#includeusing namespace std;
class Point
{
public :
      Point(float x,float y){X=x;Y=y;}
      void Move() const {cout<<"Point"<<endl;}
private :
      float X,Y;
};

class Rectangle:public Point
{
public :
      Rectangle(float x,float y,float w,float h);
        void Move() const {cout<<"Rectangle"<<endl;}
private:
        float W,H;
};
Rectangle::Rectangle(float x,float y,float w,float h):Point(x,y){
    W=w;H=h;
}
void fun(Point &p){  //编译时系统直接将p.move调用Point的函数
  p.Move();
}
int main()
{
        Rectangle r(1,2,3,4);
        fun(r);
        return 0;
}
动态:
只要在函数之前加上virtual
虚函数:非静态的成员函数
原型之间写virtual,virtual,只用来说明原型,不能用在函数实现
基类声明后,派生类自动为虚函数
调用方式:基类指针或引用,执行时根据指针指向的对象的类决定调用

8.4抽象类
带有纯虚函数的类称为抽象类
抽象类不能声明对象。
class 类名
{
    virtual 类型 函数名(参数表)=0; //虚函数
    ...
}
抽象类为抽象和设计的目的而建立
成员函数没有具体实现,在派生类具体实现
只能作为基类实现
抽象类不可以声明对象,可以声明指针

#includeusing namespace std;
class Point
{
public :
        Point (){cout<<"Point"<<endl;}
        virtual ~Point(){cout<<"xigou Point "<<endl;}
        virtual void Move() const {cout<<"Point move"<<endl;} //纯虚函数
private :
};
class Rectangle:public Point
{
public :
      Rectangle(){cout<<"Rectangle"<<endl;}
      virtual ~Rectangle(){cout<<"xigou Rectangle"<<endl;}
      virtual void Move() const {cout<<"Rectangle move"<<endl;}
private:
};
void fun(Point &p){  //编译时系统直接将p.move调用Point的函数
  p.Move();
}
void test(Point *p){ //动态析构
  delete p;
}
int main()
{
        //Rectangle r;
        //fun(r);
        Rectangle *p=new Rectangle;  
        test(p);
        return 0;
}

开发实例
    沿用继承那一章的例子,我们对employee基类变为抽象类
    继承里构造函数和析构函数不能继承。
    基类里虚函数无法确定,就定义为纯虚函数,形成抽象类。派生类继承之后再具体实现,
即可实现所谓的多态性

class employee  //基类 员工
{
  protected :
        char *name;
        int id;
        int grade;
        float monthpay;
        static  int employno; //编号最大值
          //静态,所有都可以访问
  public :
          employee();
          ~employee();
          virtual void pay()=0;  //纯虚函数
          virtual void promote(int )=0;
          virtual void display()=0;
};

class technician:public employee  //技术人员
{
    private :
          float hourlyrate;
          int workHours;
    public :
          technician();
          void promote(int );
          void pay();
          void display();
};

小结:
多态:同样的消息被不同类型的对象接受时导致完全不同的行为,是对类的
特定成员函数的再抽象。
运算符重载:对已有的运算符赋予多重含义 ,使用已有运算符对用户自定义
类型进行运算操作。成员函数(形参个数-1),友元函数(形参个数不变)
联编:静态,动态
虚函数
纯虚函数,
抽象类



习题(继承,多态)
(1)继承 派生类的函数调用

#include
using namespace std;
class A
{
      public :
            void print(){cout<<"A"<<endl;}
      private:
              int a;
};
class B:public A
{
      public:
          void print(){cout<<"B"<<endl;}
      private :
          int b;
};
int main()
{
      B b;
      b.print();
      b.A::print();  
      A *a=&b;
      a->print();
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值