最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入探討C++父類子類中虛函數(shù)的應(yīng)用

 更新時(shí)間:2013年05月28日 12:07:32   作者:  
本篇文章是對(duì)C++父類子類中虛函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
構(gòu)造函數(shù)不能是虛函數(shù),因?yàn)樵谡{(diào)用構(gòu)造函數(shù)創(chuàng)建對(duì)象時(shí),構(gòu)造函數(shù)必須是確定的,所以構(gòu)造函數(shù)不能是虛函數(shù)。
析構(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.測(cè)試類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)文章

最新評(píng)論

天门市| 简阳市| 宾阳县| 新竹县| 娱乐| 定结县| 启东市| 保靖县| 柳河县| 章丘市| 蚌埠市| 象山县| 建湖县| 长治市| 湟源县| 普格县| 山东省| 岢岚县| 峨山| 中西区| 屏东市| 赤水市| 明溪县| 馆陶县| 师宗县| 东安县| 宁城县| 饶平县| 灌南县| 三穗县| 栾川县| 新乐市| 宜州市| 肥西县| 云安县| 静宁县| 夹江县| 灵丘县| 永安市| 宜昌市| 永仁县|