歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++中的函數多態性應用&虛函數的靈活應用

C++中的函數多態性應用&虛函數的靈活應用

日期:2017/3/1 10:23:32   编辑:Linux編程

多態性與虛函數

一、多態性

派生類對象可以替代基類對象為基類的引用初始化或賦值。

函數的多態性其實就是對函數不同形式的聲明的一種靈活應用。比如說,我們同名不同參數的函數就是對函數的一種多態性表現;同名同參就是函數的覆蓋;如果我們用不同類型的參數和個數來聲明不同或相同的函數,那麼程序會根據我們調用實參的個數和類型進行匹配調用之前聲明的函數模型,進行運算求值。

二、虛函數

在類的繼承層次結構中,在不同的層次中可以出現同名同參(類型、個數)都相同的函數。在子類中調用父類的成員方法,可以使用子類對象調用時使用父類的作用域實現。

虛函數的作用是允許在派生類中重新定義與基類同名的函數,並且可以通過基類指針或引用來訪問基類和派生類中的同名函數。

舉一個實例來說明使用虛函數與不使用虛函數的區別,基類和派生類中都有同名函數。

不使用虛函數:

  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. #ifndef Programs_Student_h
  9. #define Programs_Student_h
  10. using namespace std;
  11. class Student
  12. {
  13. public:
  14. Student(int ,string,float);
  15. void display();
  16. protected:
  17. int num;
  18. string name;
  19. float score;
  20. };
  21. #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. #include <iostream>
  9. #include <string>
  10. #include "Student.h"
  11. using namespace std;
  12. //定義構造函數
  13. Student::Student(int n,string nam,float s)
  14. {
  15. num=n;
  16. name=nam;
  17. score=s;
  18. }
  19. void Student::display()
  20. {
  21. cout<<"num:"<<num<<"\n name:"<<name<<"\n score:"<<score<<"\n"<<endl;
  22. }
  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. #ifndef Programs_Graduate_h
  9. #define Programs_Graduate_h
  10. #include "Student.h"
  11. #include <string.h>
  12. using namespace std;
  13. class Graduate:public Student
  14. {
  15. public:
  16. Graduate(int ,string ,float,float);
  17. void display();
  18. private:
  19. float pay;
  20. };
  21. #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. #include <iostream>
  9. #include "Graduate.h"
  10. #include "Student.h"
  11. using namespace std;
  12. void Graduate::display()
  13. {
  14. cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl;
  15. }
  16. Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p)
  17. {
  18. }
  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. #include <iostream>
  9. #include "Student.h"
  10. #include "Graduate.h"
  11. using namespace std;
  12. int main(int argc, const char * argv[])
  13. {
  14. Student s1(1000,"David",100);
  15. Graduate g1(2000,"Jarry",50,20);
  16. Student *p=&s1;
  17. p->display();
  18. p=&g1;
  19. p->display();
  20. return 0;
  21. }
Copyright © Linux教程網 All Rights Reserved