構造函數與對象指針

#include "stdafx.h"
#include<iostream>
using namespace std;
class A
{
public:
 A()
 {
  cout<<"This is A/n";
 }
protected:
private:
};
class B: public A
{
public:
 B()
 {
  cout<<"This is B/n";
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 A*pa = new B;  這裏只申明A類型的指針不會調用A的構造函數,但是 new B的時候就會先調用A的構造函數,再調用B的構造函數。
 //A a;
 //B b;
 system("pause");
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章