C++第五次作业

重载与多态


多态:

一、概念:指同样的消息被不同类型的对象接收时导致的不同行为。消息是指对类的成员函数的调用,不同的行为指不同的实现,也是调用了不同的函数。

二、类型:重载多态、强制多态(专用多态)包含多态、参数多态(通用多态)

三、多态的实现:

1、编译时的多态是在编译过程中确定了同名操作的具体对象。

2、运行时的多态是在程序运行过程中才动态地确定操作所针对的具体对象。

 

运算符重载:

是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同行为。

实质就是函数重载!!!

一、运算符的重载形式:

1、重载为类的非静态成员函数。

2、重载为非成员函数。

二、语法形式:

返回类型   operator   运算符(形参表)
{
    函数体   
}

例:通过运算符重载实现复数类加减法。

#include<iostream>
using namespace std;
class Complex {
public:
    Complex(double r = 0.0, double i = 0.0) :real(r), imag(i) {}
    Complex operator+(const Complex& c2)const;
    Complex operator-(const Complex& c2)const;
    void display()const;
private:
    double real;
    double imag;
};

Complex Complex::operator+(const Complex& c2)const {
    return Complex(real + c2.real, imag + c2.imag);
}

Complex Complex::operator-(const Complex& c2)const {
    return Complex(real - c2.real, imag - c2.imag);
}

void Complex::display()const {
    cout << "(" << real << "," << imag << ")" << endl;
}

int main()
{
    Complex c1(5, 4), c2(2, 10), c3;
    cout << "c1="; c1.display();
    cout << "c2="; c2.display();
    c3 = c1 - c2;
    cout << "c3=c1-c2="; c3.display();
    c3 = c1 + c2;
    cout << "c3=c1+c2="; c3.display();
    return 0;
}

运行结果:

 

注:由上述例子可以看出,在使用的时候,直接通过运算符,操作数的方式来完成函数调用,运算符“+”、“-”原功能都不变,同时添加了新的针对复数运算的功能。

单目运算符++(自增)与--(自减):

语法规定:前置单目运算符重载为成员函数时没有形参,而后置单目运算符重载为成员函数时需要有一个int型形参。

Eg:对类Point重载“++”(自增)“--”(自减)运算符,要求同时重载前缀和后缀的形式。

 

#include<iostream>
using namespace std;
class Point
{
public:
    Point& operator++();
    Point operator++(int);

    Point& operator--();
    Point operator--(int);

    Point() { _x = _y = 0; }
    int x() { return _x; }
    int y() { return _y; }
private:
    int _x, _y; 
};
Point& Point::operator++()
{
    _x++;
    _y++;
    return *this;
}

Point Point::operator++(int)
{
    Point temp = *this;
    ++* this;
    return temp;
}

Point& Point::operator--()
{
    _x--;
    _y--;
    return *this;
}

Point Point::operator--(int)
{
    Point temp = *this;
    --* this;
    return temp;
}

int main()
{
    Point A;
    cout << "A的值为:" << A.x() << "," << A.y() << endl;
    A++;
    cout << "A的值为:" << A.x() << "," << A.y() << endl;
    ++A;
    cout << "A的值为:" << A.x() << "," << A.y() << endl;
    A--;
    cout << "A的值为:" << A.x() << "," << A.y() << endl;
    --A;
    cout << "A的值为:" << A.x() << "," << A.y() << endl;
}

 

运行结果:


 

虚函数:

一般虚函数成员的声明语法是:

  virtual 函数类型 函数名(形参名);

虚函数声明只能出现在类的定义中的函数原型声明中,而不能在成员函数实现的时候。

eg:

#include<iostream>
using namespace std;
class Base1 {
public:
    virtual void display()const;
};

void Base1::display()const {
    cout << "Base1::display()" << endl;
}

class Base2 :public Base1 {
public:
    void display()const;
};

void Base2::display()const {
    cout << "Base2::display()" << endl;
}

class Derived :public Base2 {
public:
    void display()const;
};

void Derived::display() const {
    cout << "Derived::display()" << endl;
}

void fun(Base1* ptr) {
    ptr->display();
}
int main()
{
    Base1 base1;
    Base2 base2;
    Derived derived;
    fun(&base1);
    fun(&base2);
    fun(&derived);
    return 0;
}

运行结果:

 

注:Base1,Base2类,和Derived属于同一个类族,通过共有派生而来,所以满足赋值兼容规则。Base1 的函数成员display()声明为虚函数,用对象指针来访问函数成员,实现了运行中的多态。若不适用虚函数,则程序将会静态绑定,如下图所示:

 

 

 

posted @ 2019-10-26 17:22  R丶f  阅读(240)  评论(1编辑  收藏  举报