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基类变为抽象类
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章