深入探討C++父類子類中虛函數(shù)的應(yīng)用
更新時(shí)間:2013年05月28日 12:07:32 作者:
本篇文章是對C++父類子類中虛函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
構(gòu)造函數(shù)不能是虛函數(shù),因?yàn)樵谡{(diào)用構(gòu)造函數(shù)創(chuàng)建對象時(shí),構(gòu)造函數(shù)必須是確定的,所以構(gòu)造函數(shù)不能是虛函數(shù)。
析構(gòu)函數(shù)可以是虛函數(shù)。
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.輸出結(jié)果:
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 *****************
析構(gòu)函數(shù)可以是虛函數(shù)。
1.父類Father.h:
復(fù)制代碼 代碼如下:
#pragma once
class Father
{
public:
Father(void);
virtual ~Father(void);
virtual int getCount();
public:
int count;
};
Father.cpp
復(fù)制代碼 代碼如下:
#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:
復(fù)制代碼 代碼如下:
#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
復(fù)制代碼 代碼如下:
#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
復(fù)制代碼 代碼如下:
#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.輸出結(jié)果:
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 *****************
相關(guān)文章
C++ 中malloc()和free()函數(shù)的理解
這篇文章主要介紹了C++ 中malloc()和free()函數(shù)的理解的相關(guān)資料,這里提供用法示例幫助大家理解這部分知識,需要的朋友可以參考下2017-08-08一起來學(xué)習(xí)C++的構(gòu)造和析構(gòu)
這篇文章主要為大家詳細(xì)介紹了C++構(gòu)造和析構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03C++編程中隊(duì)內(nèi)聯(lián)函數(shù)的理解和使用
這篇文章主要介紹了C++編程中隊(duì)內(nèi)聯(lián)函數(shù)的理解和使用,簡單舉例講解了inline關(guān)鍵字引出的內(nèi)聯(lián)函數(shù)的相關(guān)知識,需要的朋友可以參考下2016-01-01