reference 引用
 c++复杂在提供了太多的两种东西
     1. 太多的可以放对象的地方。c++ 的对象可以放在堆栈、堆、全局数据区里面
     2. 太多的可以访问对象的方式。变量里放的是对象,指针访问对象,引用访问对象
引用在定义时必须初始化来建立一种绑定关系
向上造型
 把子类对象当作父类对象叫 upcast 反之叫 downcast, 但downcast 有风险
ploymorphism 多态性
 在基类中声明某一个函数是 virtual 的那么他的子子孙孙同名的这个函数都是 virtual的
 virtual 的作用是告诉编译器函数的调用如果是通过指针或者引用的话,你就不能确定他一定是什么类型,
 需要到运行的时候才能确定, 这个指针所指的是什么类型,你在调用那个类型的成员函数
多态性建立在两个基础上
     1: upcast 向上造型
     2: dynamic binding 动态绑定
         Binding : which function to be called
         static binding: call the function as the code       // 编译时就知道调用那个函数
         dynamic binding: call the function of the object    // 运行时才知道该调用那个函数
learning record code:
#include <iostream>
using namespace std;
// int* f(int *x)
// {
//     (*x) ++;
//     return x;
// }
// int & g(int &x)
// {
//     x++;
//     return x;
// }
// int x;
// int & h()
// {
//     int q;
//     return x;
// }
// class A{
//     private:
//         int i;
//     public:
//         A() : i(10){}
//         void print() { cout << i << endl; }
// };
// class B : public A{
// };
class Shape;
class XYpos{
    private:
        int x, y;
    public:
        XYpos() : x(0), y(0){};
        void set(int _x, int _y);
        int getx(){ return x; }
        int gety(){ return y; }
        friend void move(XYpos &pos);
};
void XYpos::set(int _x, int _y)
{
    x = _x, y = _y;
}
class Shape{
    public:
        Shape(){};
        virtual ~Shape(){}
        virtual void render();
        void move(XYpos &pos);
        virtual void resize();
    protected:
        XYpos center;
};
void Shape::render()
{
    cout << "Shape render() called" << endl;
}
void Shape::move(XYpos &pos)
{
    center.set(pos.getx(), pos.gety());
}
void Shape::resize()
{
    cout << "Shape resize called" << endl;
}
class Ellipse : public Shape{
    public:
        Ellipse(float maj, float minr) : major_axis(maj), minor_axis(minr) {}   // 构造函数
        virtual void render();
    protected:
        float major_axis, minor_axis;
};
void Ellipse::render()
{
    cout << "Ellipse render() called" << endl;
}
class Circle : public Ellipse{
    public:
        Circle(float radius) : Ellipse(radius, radius) {}
        virtual void render();
};
void Circle::render()
{
    cout << "Circle render() called" << endl;
}
// 实现多态性的基础,向上造型, Ellipse, Circle 等都被当中哟 Shape来看待,这就是为什么参数是 Shape *p
void render(Shape *p)   // 这个render函数是通用函数,对任何shape的子类都是通用的
{
    p->render();        // p 是一个多态对象
}
void func()
{
    Ellipse ell(10, 20);
    ell.render();
    Circle circ(40);
    circ.render();
    render(&ell);
    render(&circ);
}
int main()
{
    func();
    // int x = 10;
    // int &y = x;
    // y = 18;
    // cout << y << endl;
    // int a = 0;
    // f(&a);
    // g(a);
    // h() = 16;
    // cout << x << endl;
    // A a;
    // B b;
    // a.print();
    // b.print();
    // cout << sizeof(a) << endl;
    // cout << sizeof(b) << endl;
    // int *p = (int *) &a;
    // cout << p << " " << *p << endl;
    // *p = 20;
    // a.print(); 
    // p = (int *) &b;
    // cout << p << " " << *p << endl;
    
    return 0;
}  

京公网安备 11010502036488号