首页 > 编程 > C++ > 正文

C++之普通成员函数、虚函数以及纯虚函数的区别与用法要点

2020-05-23 14:17:41
字体:
来源:转载
供稿:网友

本篇文章主要介绍了C++中的普通成员函数、虚函数以及纯虚函数,非常的详细,有需要的朋友可以参考下

普通成员函数是静态编译的,没有运行时多态,只会根据指针或引用的“字面值”类对象,调用自己的普通函数;虚函数为了重载和多态的需要,在基类中定义的,即便定义为空;纯虚函数是在基类中声明的虚函数,它可以再基类中有定义,且派生类必须定义自己的实现方法。

假设我们有三个类Person、Teacher、Student它们之间的关系如下:

C++之普通成员函数、虚函数以及纯虚函数的区别与用法要点

类的关系图

普通成员函数

【Demo1】

根据这个类图,我们有下面的代码实现

 

 
  1. #ifndef __OBJEDT_H__ 
  2. #define __OBJEDT_H__ 
  3.  
  4. #include <string> 
  5. #include <iostream> 
  6.  
  7. class Person 
  8. public
  9. Person(const string& name, int age) : m_name(name), m_age(age) 
  10.  
  11. void ShowInfo() 
  12. cout << "姓名:" << m_name << endl; 
  13. cout << "年龄:" << m_age << endl; 
  14.  
  15. protected
  16. string m_name; //姓名 
  17. int m_age; //年龄 
  18. }; 
  19.  
  20.  
  21. class Teacher : public Person 
  22. public
  23. Teacher(const string& name, int age, const string& title) 
  24. : Person(name, age), m_title(title) 
  25.  
  26. void ShowInfo() 
  27. cout << "姓名:" << m_name << endl; 
  28. cout << "年龄:" << m_age << endl; 
  29. cout << "职称:" << m_title << endl; 
  30.  
  31. private
  32. string m_title; //职称 
  33. }; 
  34.  
  35. class Student : public Person 
  36. public
  37. Student(const string& name, int age, int studyId) 
  38. : Person(name, age), m_studyId(studyId) 
  39.  
  40. void ShowInfo() 
  41. cout << "姓名:" << m_name << endl; 
  42. cout << "年龄:" << m_age << endl; 
  43. cout << "学号:" << m_studyId << endl; 
  44.  
  45. private
  46. int m_studyId; //学号 
  47. }; 
  48.  
  49. #endif //__OBJEDT_H__ 

测试代码:

 

 
  1. void test() 
  2. Person* pPerson = new Person("张三", 22); 
  3. Teacher* pTeacher = new Teacher("李四", 35, "副教授"); 
  4. Student* pStudent = new Student("王五", 18, 20151653); 
  5. pPerson->ShowInfo(); 
  6. cout << endl; 
  7. pTeacher->ShowInfo(); 
  8. cout << endl; 
  9. pStudent->ShowInfo(); 
  10. cout << endl; 
  11. delete pPerson; 
  12. delete pTeacher; 
  13. delete pStudent; 

结果:

姓名:张三

年龄:22

姓名:李四

年龄:35

职称:副教授

姓名:王五

年龄:18

学号:20151653

说明:

这里的ShowInfo就是一个普通的函数。pPerson、pTeacher和pStudent三个对象调用ShowInfo分别展示自己的信息。

我们知道:父类的指针是可以指向子类的对象的。我们把上面的测试代码稍微改一下:

【Demo2】

 

 
  1. void test() 
  2. Person* pPerson = new Person("张三", 22); 
  3. Person* pTeacher = new Teacher("李四", 35, "副教授"); 
  4. Person* pStudent = new Student("王五", 18, 20151653); 
  5. pPerson->ShowInfo(); 
  6. cout << endl; 
  7. pTeacher->ShowInfo(); 
  8. cout << endl; 
  9. pStudent->ShowInfo(); 
  10. cout << endl; 
  11. delete pPerson; 
  12. delete pTeacher; 
  13. delete pStudent; 

结果:

姓名:张三

年龄:22

姓名:李四

年龄:35

姓名:王五

年龄:18

这时,pTeacher和pStudent只输出了姓名和年龄,并没有输出子类所具有的特性(职称和学号)。这应该不是你期望的结果,你可能期望pTeacher和pStudent输出老师和学生的完整信息,这时就需要用虚函数。

虚函数

我们把Person中的ShowInfo成员改成虚函数(在前面加上virtual),代码如下:

【Demo3】

 

 
  1. class Person 
  2. public
  3. Person(const string& name, int age) : m_name(name), m_age(age) 
  4.  
  5. virtual void ShowInfo() 
  6. cout << "姓名:" << m_name << endl; 
  7. cout << "年龄:" << m_age << endl; 
  8.  
  9. protected
  10. string m_name; //姓名 
  11. int m_age; //年龄 
  12. }; 

在执行上面【Demo2】中的测试代码,得到我们想到的结果:

姓名:张三

年龄:22

姓名:李四

年龄:35

职称:副教授

姓名:王五

年龄:18

学号:20151653

虚函数用法要点:

虚函数的声明方式:virtual RETURN_TYPE functionName(ARGS 参数列表);

虚函数作用:现实C++中的多态,进行动态绑定(父类指针可指向子类的对象),直到运行时才知道要调用哪个版本(哪个类定义)的函数;

我们必要对虚函数进行定义;

一旦父类的成员函数声明virtual,其子类的函数不管有没有声明为virtual,都是虚函数;

如果虚函数使用默认实参,父类和子类定义的默认实参最好一致。

【Demo4】:针对第4点说明:

 

 
  1. class Person 
  2. public
  3. Person(const string& name, int age) : m_name(name), m_age(age) 
  4.  
  5. virtual void ShowInfo() 
  6. cout << "姓名:" << m_name << endl; 
  7. cout << "年龄:" << m_age << endl; 
  8.  
  9. string GetName(); //正确,普通函数如果不被使用,可以只有声明没有定义 
  10. virtual int GetAge(); //错误,虚函数必须要有定义,即使是一个空实现,因为编译器无法确定会使用哪个函数 
  11.  
  12.  
  13. protected
  14. string m_name; //姓名 
  15. int m_age; //年龄 
  16. }; 

【Demo5】:针对第5点进行说明:

设计我们的类如下定义。

 

 
  1. class Person 
  2. public
  3. virtual void SetAge(int age = 0) 
  4. m_age = age; 
  5. //... 省略 
  6. }; 
  7.  
  8.  
  9. class Teacher : public Person 
  10. public
  11. virtual void SetAge(int age = 1) 
  12. m_age = age; 
  13.  
  14. //... 省略 
  15. }; 
  16.  
  17. class Student : public Person 
  18. public
  19. virtual void SetAge(int age = 2) 
  20. m_age = age; 
  21.  
  22. //... 省略 
  23. }; 

测试1:

 

 
  1. void test() 
  2. Person* pPerson = new Person("张三", 22); 
  3. Teacher* pTeacher = new Teacher("李四", 35, "副教授"); 
  4. Student* pStudent = new Student("王五", 18, 20151653); 
  5. pPerson->SetAge(); 
  6. pTeacher->SetAge(); 
  7. pStudent->SetAge(); 
  8.  
  9. pPerson->ShowInfo(); 
  10. cout << endl; 
  11. pTeacher->ShowInfo(); 
  12. cout << endl; 
  13. pStudent->ShowInfo(); 
  14. cout << endl; 
  15. delete pPerson; 
  16. delete pTeacher; 
  17. delete pStudent; 

结果:

姓名:张三

年龄:0

姓名:李四

年龄:1

职称:副教授

姓名:王五

年龄:2

学号:20151653

测试2:

 

 
  1. void test() 
  2. Person* pPerson = new Person("张三", 22); 
  3. Person* pTeacher = new Teacher("李四", 35, "副教授"); 
  4. Person* pStudent = new Student("王五", 18, 20151653); 
  5. pPerson->SetAge(); 
  6. pTeacher->SetAge(); 
  7. pStudent->SetAge(); 
  8.  
  9. pPerson->ShowInfo(); 
  10. cout << endl; 
  11. pTeacher->ShowInfo(); 
  12. cout << endl; 
  13. pStudent->ShowInfo(); 
  14. cout << endl; 
  15. delete pPerson; 
  16. delete pTeacher; 
  17. delete pStudent; 

结果:

姓名:张三

年龄:0

姓名:李四

年龄:0

职称:副教授

姓名:王五

年龄:0

学号:20151653

纯虚函数

在上面的例子中,我们假设所有的人都要工作,但不同的人工作的方式不同。于是我们就要强制要求继承自Person的子类都要有工作的方法,这就需要纯虚函数。定义如下:

【Demo6】

 

 
  1. class Person 
  2. public
  3. //... 省略 
  4. virtual void DoWork() = 0; 
  5. //... 省略 
  6. }; 

但此时我们编译

 

 
  1. Person* pPerson = new Person("张三", 22); 

这句话时会报错:error C2259: ‘Person' : cannot instantiate abstract class

这是因为我们并没有为Person实现DoWork方法,而包含纯虚函数的类是一个抽象的类,抽象类不能被实例化。

于是我们在子类中对它实现如下:

【Demo7】

 

 
  1. class Teacher : public Person 
  2. public
  3. //... 省略 
  4. virtual void DoWork() 
  5. cout << "教书..." << endl; 
  6. //... 省略 
  7. }; 
  8.  
  9. class Student : public Person 
  10. public
  11. //... 省略 
  12. virtual void DoWork() 
  13. cout << "学习..." << endl; 
  14. //... 省略 
  15. }; 

没用DoWork方法:

 

 
  1. void test() 
  2. Person* pTeacher = new Teacher("李四", 35, "副教授"); 
  3. Person* pStudent = new Student("王五", 18, 20151653); 
  4.  
  5. pTeacher->DoWork(); 
  6. cout << endl; 
  7. pStudent->DoWork(); 
  8. cout << endl; 
  9.  
  10. delete pTeacher; 
  11. delete pStudent; 

结果:

教书…

学习…

纯虚函数用法要点:

纯虚函数的声明方式:virtual RETURN_TYPE functionName(ARGS 参数列表) = 0;

含有纯虚函数的类是一个抽象的类,抽象类不能被实例化。

包含纯虚函数的抽象类常用来当作对外的接口,说明这个类有什么功能,而没有具体的实现,基体的实现交由子类完成。

通过以上对普通成员函数、虚函数以及纯虚函数的介绍,希望可以对大家有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表