zoukankan      html  css  js  c++  java
  • c++中一个多态的实例

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    class A{
    public:
        A(){
            a = 0;
        }
        int a;
    };
    
    class B :public A{
    public:
        B(){
            a = 1;
        }
        int a;
    };
    
    int main(){
        B b ;
        B *pb = &b;
        A* pa = &b;
    
        printf("a in B is: %d
    ", pb->a);
        printf("a in A is: %d
    ", pa->a);
    
        printf("value of pb is: %p
    ", pb);
        printf("value of pa is: %p
    ", pa);
    
        printf("address of pb->a is: %p
    ", &(pb->a));
        printf("address of pa->a is: %p
    ", &(pa->a));
    
        return 0;
    }

    结果如下:

  • 相关阅读:
    day20
    day19
    day18
    day17
    day16
    day14 HTML
    day7课程
    day6
    python-day4
    python-day3
  • 原文地址:https://www.cnblogs.com/wang-130213/p/9074129.html
Copyright © 2011-2022 走看看