1. 程式人生 > >C++構造、析構、繼承、多型--一道筆試題都考到了

C++構造、析構、繼承、多型--一道筆試題都考到了

#include <stdio.h>

class A
{
public:
    A()
    {
        printf("A constrution.\n");
     }
    ~A()
    {
        printf("A deconstrution.\n");
    }
    void funA();
   virtual  void funB();
};

 void A::funA()
{
        printf("A::funA ...\n");
 }    
    
void A::funB()
{
        printf("A::funB ...\n");
}

class B : public A
{
public:
    B()
    {
        printf("B constrution.\n");
     }
    ~B()
    {
        printf("B deconstrution.\n");
    }
    void funA();
     virtual  void funB();
};

 void B::funA()
{
        printf("B::funA ...\n");
 }    
    
void B::funB()
{
        printf("B::funB ...\n");
}

void main()
{
    A *a = new B();
    B b;
    a->funA();
    a->funB();
    b.funA();
    b.funB();
    delete a;
}

寫出輸出結果:(呵呵,順便複習一下g++的編譯的幾個步驟)