Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / C++中的函数多态性应用&虚函数的灵活应用

多态性与虚函数一、多态性派生类对象可以替代基类对象为基类的引用初始化或赋值。函数的多态性其实就是对函数不同形式的声明的一种灵活应用。比如说,我们同名不同参数的函数就是对函数的一种多态性表现;同名同参就是函数的覆盖;如果我们用不同类型的参数和个数来声明不同或相同的函数,那么程序会根据我们调用实参的个数和类型进行匹配调用之前声明的函数模型,进行运算求值。二、虚函数在类的继承层次结构中,在不同的层次中可以出现同名同参(类型、个数)都相同的函数。在子类中调用父类的成员方法,可以使用子类对象调用时使用父类的作用域实现。虚函数的作用是允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或引用来访问基类和派生类中的同名函数。举一个实例来说明使用虚函数与不使用虚函数的区别,基类和派生类中都有同名函数。不使用虚函数:
  1. //   
  2. //  Student.h   
  3. //  Programs   
  4. //   
  5. //  Created by bo yang on 4/17/12.   
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #ifndef Programs_Student_h   
  10. #define Programs_Student_h   
  11. using namespace std;  
  12.   
  13. class Student  
  14. {  
  15. public:  
  16.     Student(int ,string,float);  
  17.     void display();  
  18. protected:  
  19.     int num;  
  20.     string name;  
  21.     float score;  
  22. };  
  23.   
  24.   
  25. #endif  
 
  1. //   
  2. //  Student.cpp   
  3. //  Programs   
  4. //   
  5. //  Created by bo yang on 4/17/12.   
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #include <iostream>   
  10. #include <string>   
  11. #include "Student.h"   
  12. using namespace std;  
  13.   
  14. //定义构造函数   
  15. Student::Student(int n,string nam,float s)  
  16. {  
  17.     num=n;  
  18.     name=nam;  
  19.     score=s;  
  20. }  
  21.   
  22. void Student::display()  
  23. {  
  24.     cout<<"num:"<<num<<"  name:"<<name<<"  score:"<<score<<" "<<endl;  
  25. }  
  1. //   
  2. //  Graduate.h   
  3. //  Programs   
  4. //   
  5. //  Created by bo yang on 4/17/12.   
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #ifndef Programs_Graduate_h   
  10. #define Programs_Graduate_h   
  11. #include "Student.h"   
  12. #include <string.h>   
  13. using namespace std;  
  14.   
  15. class Graduate:public Student  
  16. {  
  17. public:  
  18.     Graduate(int ,string ,float,float);  
  19.     void display();  
  20. private:  
  21.     float pay;  
  22. };  
  23.   
  24.   
  25. #endif  
 
  1. //   
  2. //  Graduate.cpp   
  3. //  Programs   
  4. //   
  5. //  Created by bo yang on 4/17/12.   
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #include <iostream>   
  10. #include "Graduate.h"   
  11. #include "Student.h"   
  12. using namespace std;  
  13.   
  14. void Graduate::display()  
  15. {  
  16.     cout<<"num:"<<num<<" name:"<<name<<" score:"<<score<<" pay="<<pay<<endl;  
  17.       
  18. }  
  19.   
  20. Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p)  
  21. {  
  22.       
  23. }  
  1. //   
  2. //  main.cpp   
  3. //  Student&Graduate   
  4. //   
  5. //  Created by bo yang on 4/17/12.   
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.   
  7. //   
  8.   
  9. #include <iostream>   
  10. #include "Student.h"   
  11. #include "Graduate.h"   
  12. using namespace std;  
  13.   
  14. int main(int argc, const char * argv[])  
  15. {  
  16.   
  17.     Student s1(1000,"David",100);  
  18.     Graduate g1(2000,"Jarry",50,20);  
  19.       
  20.     Student *p=&s1;  
  21.     p->display();  
  22.       
  23.     p=&g1;  
  24.     p->display();  
  25.     return 0;  
  26.    }