深入探討C++父類子類中虛函數的應用

來源:互聯網
上載者:User

建構函式不能是虛函數,因為在調用建構函式建立對象時,建構函式必須是確定的,所以建構函式不能是虛函數。
解構函式可以是虛函數。

1.父類Father.h:
複製代碼 代碼如下:#pragma once
class Father
{
public:
Father(void);
virtual ~Father(void);
virtual int getCount();
public:
int count;
};

Father.cpp複製代碼 代碼如下:#include "StdAfx.h"
#include "Father.h"
#include <iostream>
using namespace std;
Father::Father(void)
{
count = 1;
cout<<"Father is called. count = "<<count<<endl;
}
Father::~Father(void)
{
cout<<"~Father is called."<<endl;
}
int Father::getCount()
{
cout<<"Father::getCount() count = "<<count<<endl;
return count;
}

2.子類Child.h:
複製代碼 代碼如下:#pragma once
#include "father.h"
class Child :
public Father
{
public:
Child(void);
virtual ~Child(void);
virtual int getCount();
int getAge();
public:
int age;
};

Child.cpp複製代碼 代碼如下:#include "StdAfx.h"
#include "Child.h"
#include <iostream>
using namespace std;
Child::Child(void)
{
count = 2;
age = 20;
cout<<"Child is called. count = "<<count<<", age = "<<age<<endl;
}
Child::~Child(void)
{
cout<<"~Child is called."<<endl;
}
int Child::getCount()
{
cout<<"Child::getCount() count = "<<count<<endl;
return count;
}
int Child::getAge()
{
cout<<"Child::getAge() age = "<<age<<endl;
return age;
}

3.測試類別Test.cpp
複製代碼 代碼如下:#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include "Child.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Father *father1 = new Father();
cout<<"father1 count = "<<father1->getCount()<<endl;
delete father1;
cout<<"************** father1 end *****************"<<endl<<endl;
Father *father2 = new Child();
cout<<"father2 count = "<<father2->getCount()<<endl; // father2 don't have getAge() method
delete father2;
cout<<"************** father2 end *****************"<<endl<<endl;
Child *child = new Child();
cout<<"child count = "<<child->getCount()<<endl;
cout<<"child age = "<<child->getAge()<<endl;
delete child;
cout<<"************** child end *****************"<<endl<<endl;
getchar();
return 0;
}

4.輸出結果:
Father is called. count = 1
Father::getCount() count = 1
father1 count = 1
~Father is called.
************** father1 end *****************

Father is called. count = 1
Child is called. count = 2, age = 20
Child::getCount() count = 2
father2 count = 2
~Child is called.
~Father is called.
************** father2 end *****************

Father is called. count = 1
Child is called. count = 2, age = 20
Child::getCount() count = 2
child count = 2
Child::getAge() age = 20
child age = 20
~Child is called.
~Father is called.
************** child end *****************

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.