C++【多态】和【覆盖】

本文探讨了C++中的多态性,重点介绍了虚函数和覆盖(override)的概念。多态依赖于虚函数,使得通过基类指针或引用调用子类的方法成为可能。覆盖要求子类函数与基类虚函数有相同的名称、参数列表和返回类型。示例代码展示了多态的使用,输出了不同形状的绘制信息。

多态=虚函数+指针/引用

关于多态:
形状:位置,绘制
矩形:宽度、高度,绘制
圆形:半径,绘制
Shape
/ \
Rect Circle
如果将基类中的某个成员函数声明为虚函数,那么其子类中与该函数具有相同原型的成员函数就也成为虚函数,并对基类中的版本构成覆盖(override)。通过一个指向子类对象的基类指针,或者引用子类对象的基类引用,调用这个虚函数时,实际被调用的将是子类中的覆盖版本。这种特性被称为多态。

关于覆盖
1.基类中成员函数必须是虚函数。
2.子类中成员函数必须与基类中的虚函数拥有完全相同的函数名、形参表和常属性。
3.如果基类中的虚函数返回基本类型,那么子类覆盖版本的返回类型必须与基类完全相同。如果基类中的虚函数返回类类型的指针或者引用,那么子类覆盖版本的返回类型可以是基类返回类型的子类。
class X { … };
class Y : public X { … };
class A {
virtual int foo (void) { … }
virtual X* bar (void) { … }
};
class B : public A {
int foo (void) { … }
Y* bar (void) { … }
};
4.子类中的覆盖版本不能比基类版本抛出更多的异常。
5.子类中覆盖版本与基类版本的访控属性无关。
class A {
public:
virtual int foo (void) { … }
};
class B : public A {
private
int foo (void) { … }
};
B b;
b.foo (); // ERROR !
A* p = &b;
p -> foo (); // OK !

多态的实现离不开基类与子类之间的覆盖关系

示例代码:

#include <iostream>
using namespace std;
class Shape {
public:
    Shape (int x, int y) : m_x (x), m_y (y) {}
    virtual void draw (void) const = 0;
protected:
    int m_x;
    int m_y;
};
class Rect : public Shape {
public:
    Rect (int x, int y, int w, int h) :
        Shape (x, y), m_w (w), m_h (h) {}
    void draw (void) const {
        cout << "矩形:" << m_x << "," << m_y
            << "," << m_w << "," << m_h << endl;
    }
private:
    int m_w;
    int m_h;
};
class Circle : public Shape {
public:
    Circle (int x, int y, int r) :
        Shape (x, y), m_r (r) {}
    void draw (void) const {
        cout << "圆形:" << m_x << "," << m_y
            << "," << m_r << endl;
    }
private:
    int m_r;
};
void render (Shape* shapes[]) {
    for (size_t i = 0; shapes[i]; i++)
        shapes[i] -> draw ();
}
int main (void) {
    size_t i = 0;
    Shape* shapes[1024] = {NULL};
    shapes[i++] = new Rect (1, 2, 3, 4);
    shapes[i++] = new Rect (5, 6, 7, 8);
    shapes[i++] = new Circle (9, 10, 11);
    shapes[i++] = new Circle (12, 13, 14);
    shapes[i++] = new Rect (15, 16, 17, 18);
    render (shapes);
//  Shape shape (0, 0);
//virtual 返回类型 函数名 (形参表) = 0;
//的虚函数称为纯虚函数。
//至少含有一个纯虚函数的类,称为抽象类。抽象类不能实例化对象。
    return 0;
}

下面是运行结果:

矩形:1,2,3,4
矩形:5,6,7,8
圆形:9,10,11
圆形:12,13,14
矩形:15,16,17,18

Applies basic field behavior in circuit design and demonstrates how it relates to grounding and shielding requirements and techniques in circuit designThis book connects the fundamentals of electromagnetic theory to the problems of interference in all types of electronic design. The text covers power distribution in facilities, mixing of analog and digital circuitry, circuit board layout at high clock rates, and meeting radiation and susceptibility standards. The author examines the grounding and shielding requirements and techniques in circuit design and applies basic physics to circuit behavior. The sixth edition of this book has been updated with new material added throughout the chapters where appropriate. The presentation of the book has also been rearranged in order to reflect the current trends in the field.Grounding and Shielding: Circuits and Interference, Sixth Edition: Includes new material on vias and field control, capacitors as transmission lines, first energy sources, and high speed designs using boards with only two layersDemonstrates how circuit geometry controls performance from dc to gigahertzExamines the use of multi-shielded transformers in clean-power installationsProvides effective techniques for handling noise problems in analog and digital circuitsDiscusses how to use conductor geometry to improve performance, limit radiation, and reduce susceptibility to all types of hardware and systemsGrounding and Shielding: Circuits and Interference, Sixth Edition is an updated guide for circuit design engineers and technicians. It will also serve as a reference for engineers in the semiconductor device industry.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值