C++父类子类中虚函数的使用

来源:互联网 发布:udid授权软件 编辑:程序博客网 时间:2024/06/05 18:01

构造函数不能是虚函数,因为在调用构造函数创建对象时,构造函数必须是确定的,所以构造函数不能是虚函数。

析构函数可以是虚函数。

 

1.父类Father.h:

[cpp] view plain copy
  1. #pragma once  
  2. class Father  
  3. {  
  4. public:  
  5.     Father(void);  
  6.     virtual ~Father(void);  
  7.   
  8.     virtual int getCount();  
  9.   
  10. public:  
  11.     int count;  
  12. };  

Father.cpp

[cpp] view plain copy
  1. #include "StdAfx.h"  
  2. #include "Father.h"  
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. Father::Father(void)  
  8. {  
  9.     count = 1;  
  10.     cout<<"Father is called. count = "<<count<<endl;  
  11. }  
  12.   
  13.   
  14. Father::~Father(void)  
  15. {  
  16.     cout<<"~Father is called."<<endl;  
  17. }  
  18.   
  19.   
  20. int Father::getCount()  
  21. {  
  22.     cout<<"Father::getCount() count = "<<count<<endl;  
  23.     return count;  
  24. }  

 

2.子类Child.h:

[cpp] view plain copy
  1. #pragma once  
  2. #include "father.h"  
  3. class Child :  
  4.     public Father  
  5. {  
  6. public:  
  7.     Child(void);  
  8.     virtual ~Child(void);  
  9.   
  10.     virtual int getCount();  
  11.     int getAge();  
  12.   
  13. public:  
  14.     int age;  
  15. };  

Child.cpp

[c-sharp] view plain copy
  1. #include "StdAfx.h"  
  2. #include "Child.h"  
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. Child::Child(void)  
  8. {  
  9.     count = 2;  
  10.     age = 20;  
  11.     cout<<"Child is called. count = "<<count<<", age = "<<age<<endl;  
  12. }  
  13.   
  14.   
  15. Child::~Child(void)  
  16. {  
  17.     cout<<"~Child is called."<<endl;  
  18. }  
  19.   
  20. int Child::getCount()  
  21. {  
  22.     cout<<"Child::getCount() count = "<<count<<endl;  
  23.     return count;  
  24. }  
  25.   
  26. int Child::getAge()  
  27. {  
  28.     cout<<"Child::getAge() age = "<<age<<endl;  
  29.     return age;  
  30. }  

3.测试类Test.cpp

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include  <cstdlib>  
  3. #include <iostream>  
  4. #include "Child.h"  
  5.   
  6. using namespace std;  
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     Father *father1 = new Father();  
  11.     cout<<"father1 count = "<<father1->getCount()<<endl;  
  12.     delete father1;  
  13.     cout<<"************** father1 end *****************"<<endl<<endl;  
  14.   
  15.     Father *father2 = new Child();  
  16.     cout<<"father2 count = "<<father2->getCount()<<endl; // father2 don't have getAge() method  
  17.     delete father2;  
  18.     cout<<"************** father2 end *****************"<<endl<<endl;  
  19.   
  20.     Child *child = new Child();  
  21.     cout<<"child count = "<<child->getCount()<<endl;  
  22.     cout<<"child age = "<<child->getAge()<<endl;  
  23.     delete child;  
  24.     cout<<"************** child end *****************"<<endl<<endl;  
  25.   
  26.     getchar();  
  27.   
  28.     return 0;  
  29. }  

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 *****************

原文地址:http://blog.csdn.net/ameyume/article/details/6285902

0 0