关于虚函数与多态的学习总结

xiaoxiao2021-02-28  43

一,基础知识

    1,基本概念

           多态性(Polymorphism)是指一个名字,多种语义;或界面相同,多种实现。

 重载函数是多态性的一种简单形式。

         虚函数允许函数调用与函数体的联系在运行时才进行,称为动态联编。

    2,虚函数和动态联编

          冠以关键字 virtual 的成员函数称为虚函数, 实现运行时多态的关键首先是要说明虚函数,另外,必须用

          基类指针调用派生类的不同实现版本

    3,虚函数和基类指针

          通过基类指针,只能访问从基类继承的成员

     ps:

           1> 一个虚函数,在派生类层界面相同的重载函数都保持虚特性

           2>虚函数必须是类的成员函数

           3>不能将友元说明为虚函数,但虚函数可以是另一个类的友元

           4>析构函数可以是虚函数,但构造函数不能是虚函数

    4,虚函数的重载特性

           在派生类中重载基类的虚函数要求函数名、返回类型、参数个数、

  参数类型和顺序完全相同, 如果仅仅返回类型不同,C++认为是错误重载,如果函数原型不同,仅函数名相同,丢失虚特性 

    5, 虚析构函数

          构造函数不能是虚函数。建立一个派生类对象时,必须从类层次的根开始,沿着继承路径逐个调用基类的构造函数, 析构函数可以是虚的。虚析构函数用于指引delete 运算符正确析构动态对象 

    PS:

        1>派生类应该从它的基类公有派生。?

        2>必须首先在基类中定义虚函数。

        3>派生类对基类中声明虚函数重新定义时,关键字virtual可以不写。

        4>一般通过基类指针访问虚函数时才能体现多态性。

        5>一个虚函数无论被继承多少次,保持其虚函数特性。

        6>虚函数必须是其所在类的成员函数,而不能是友元函数,也不能是静态函数。

        7>构造函数、内联成员函数、静态成员函数不能是虚函数。(虚函数不能以内联的方式进行处理)

        8>析构函数可以是虚函数,通常声明为虚函数。

    6,成员函数调用虚函数(采用动态联编)

    7,纯虚函数和抽象类

        纯虚函数是一种特殊的虚函数,在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数实现留给该基类的派生类去做,这就是纯虚函数的作用。

          纯虚函数是一个在基类中说明的虚函数,在基类中没有定义, 要求任何派生类都定义自己的版本

          纯虚函数为各派生类提供一个公共界面,纯虚函数说明形式:

                  virtual 类型  函数名(参数表)= 0 ;

         一个具有纯虚函数的基类称为抽象类。

二,简单应用

    1,虚函数和基类指针

#include<iostream> using namespace std ; class Base { public : Base(char xx) { x = xx; } void who() { cout << "Base class: " << x << "\n" ; } protected: char x; } ; class First_d : public Base { public : First_d(char xx, char yy):Base(xx) { y = yy; } void who() { cout << "First derived class: "<< x << ", " << y << "\n" ; } protected: char y; } ; class Second_d : public First_d { public : Second_d( char xx, char yy, char zz ) : First_d( xx, yy ) { z = zz; } void who() { cout << "Second derived class: "<< x << ", " << y << ", " << z << "\n" ; } protected: char z; } ; int main() { Base B_obj( 'A' ) ; First_d F_obj( 'T', 'O' ) ; Second_d S_obj( 'E', 'N', 'D' ) ; Base * p ; p = & B_obj ; p -> who() ; p = &F_obj ; p -> who() ; p = &S_obj ; p -> who() ; F_obj.who() ; ( ( Second_d * ) p ) -> who() ; } //通过基类指针 //只能访问从基类继承的成员 2,虚函数的重载特性 class base { public : virtual void vf1 ( ) ; virtual void vf2 ( ) ; virtual void vf3 ( ) ; void f ( ) ; } ; class derived : public base { public : void vf1 ( ) ; // 虚函数 void vf2 ( int ) ; // 重载,参数不同,虚特性丢失 char vf3 ( ) ; // error,仅返回类型不同 void f ( ) ; // 非虚函数重载 } ; void g ( ) { derived d ; base * bp = & d ; // 基类指针指向派生类对象 bp -> vf1 ( ) ; // 调用 deriver :: vf1 ( ) bp -> vf2 ( ) ; // 调用 base :: vf2 ( ) bp -> f ( ) ; // 调用 base :: f ( ) }

3,虚析构函数

例 普通析构函数在删除动态派生类对象的调用情况 #include<iostream> using namespace std ; class A { public: ~A(){ cout << "A::~A() is called.\n" ; } } ; class B : public A { public: ~B(){ cout << "B::~B() is called.\n" ; } } ; int main() { A *Ap = new B ; B *Bp2 = new B ; cout << "delete first object:\n" ; delete Ap; cout << "delete second object:\n" ; delete Bp2 ; }

4,成员函数调用虚函数(采用动态联编)

#include <iostream.h> class A { public: virtual double funA(double x) { cout<<"funA of class A called."<<endl; return x*x; } double funB(double x) { return funA(x)/2; } }; class B:public A { public: virtual double funA(double x) { cout<<"funA of class B called."<<endl; return 2*x*x; } }; class C:public B { public: virtual double funA(double x) { cout<<"funA of class C called."<<endl; return 3*x*x; } }; void main() { C objc; cout<<objc.funB(3)<<endl; B objb; cout<<objb.funB(3)<<endl; } #include <iostream> #include<string> using namespace std; class Animal { string name; public: Animal(string a_name):name(a_name){} virtual void show(){} void show_name() { cout<< "The name is "<<name<<".“<<endl; } }; class Cat :public Animal { string kind; public: Cat(string a_name,string a_kind):Animal(a_name),kind(a_kind) {} void show(); }; void Cat::show() { show_name(); cout<<" It's a "<<kind<<endl; } class Dog:public Animal { string kind; public: Dog(string a_name,string a_kind):Animal(a_name),kind(a_kind) {} void show(); }; void Dog::show() { show_name(); cout<<" It's a "<<kind<<endl; } class Tiger:public Cat { public: Tiger(string a_name,string a_kind):Cat(a_name,a_kind) {} }; int main() { Animal *p; Cat cat("Tom","cat"); Dog dog("Jerry","Dog"); Tiger tiger("DuDu","Tiger"); p=&cat; p->show(); p=&dog; p->show(); p=&tiger; p->show(); return 0; }

5,纯虚函数与抽象类

class point { /*……*/ } ; class shape ; // 抽象类 { point center ; …… public : point where ( ) { return center ; } void move ( point p ) {center = p ; draw ( ) ; } virtual void rotate ( int ) = 0 ; // 纯虚函数 virtual void draw ( ) = 0 ; // 纯虚函数 } ; …... shape x ; // error,抽象类不能建立对象 shape *p ; // ok,可以声明抽象类的指针 shape f ( ) ; // error, 抽象类不能作为函数返回类型 void g ( shape ) ; // error, 抽象类不能作为传值参数类型 shape & h ( shape &) ; // ok,可以声明抽象类的引用 class figure { protected : double x,y; public: void set_dim(double i, double j=0) { x = i ; y = j ; } virtual void show_area() = 0 ; }; class triangle : public figure { public : void show_area() { cout<<"Triangle with high "<<x<<" and base "<<y <<" has an area of "<<x*0.5*y<<"\n"; } }; class square : public figure { public: void show_area() { cout<<"Square with dimension "<<x<<"*"<<y <<" has an area of "<<x*y<<"\n"; } }; class circle : public figure { public: void show_area() { cout<<"Circle with radius "<<x; cout<<" has an area of "<<3.14*x*x<<"\n"; } }; #include<iostream> using namespace std ; #include"figure.h" int main() { triangle t ; //派生类对象 square s ; circle c; t.set_dim(10.0,5.0) ; t.show_area(); s.set_dim(10.0,5.0) ; s.show_area() ; c.set_dim(9.0) ; c.show_area() ; } #include<iostream> using namespace std ; class Number { public : Number (int i) { val = i ; } virtual void Show() = 0 ; protected: int val ; }; class Hex_type : public Number { public: Hex_type(int i) : Number(i) { } void Show() { cout << "Hexadecimal:" << hex << val << endl ; } }; class Dec_type : public Number { public: Dec_type(int i) : Number(i) { } void Show() { cout << "Decimal: " << dec << val << endl ; } }; class Oct_type : public Number { public: Oct_type(int i) : Number(i) { } void Show() { cout << "Octal: " << oct << val << endl ; } }; void fun( Number & n ) // 抽象类的引用参数 { n.Show() ; } int main() { Dec_type n1(50); fun(n1); // Dec_type::Show() Hex_type n2(50); fun(n2); // Hex_type::Show() Oct_type n3(50); fun(n3); // Oct_type::Show() }

三,学习感悟

    这一块感觉有点难以理解,虚函数还行,纯虚函数就感觉有些麻烦,到了抽象类,就是真的很抽象了。多看了两遍课件,还是不难理解的。虚函数,避免在父类使用基类的指针时还是指向子类中重载前内容,保证了该数据成员或者成员函数一定被继承。纯虚函数,里面没有任何语句,只是充当接口作用,这是就能看出来一点分功能操作的雏形了,这一部分的实际应用不多,但也能明白纯虚函数的作用是什么,函数接口,将功能分块。小的程序看不出什么来,大的程序设计,应该就很有必要了。

转载请注明原文地址: https://www.6miu.com/read-2612385.html

最新回复(0)