虚函数与多态章节总结

一、基础知识

1、多态性的基本概念

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

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

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

2动态联编

(1)虚函数和基类指针

写有关键字virtual的成员函数称为虚函数,实现运行多态的关键是要说明虚函数,另外必须用基类指针调用派生类的不同实现版本。

例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() ;
}

只能访问基类继承来的数据成员,如果想访问派生类新增成员,可以把基类中的who改为虚函数,即在前面加virtual

注意:

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

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

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

析构函数可以是虚函数,但构造函数不行

(2)虚函数的重载特性

在派生类中重载基类的虚函数要求函数名、返回类型、参数个数、 参数类型和顺序完全相同, 如果仅仅返回类型不同, C++ 认为是错误重载, 如果函数原型不同,仅函数名相同,丢失虚特性 

(3)、虚析构函数

构造函数不能是虚函数,析构函数可以,作用是指引delete运算符正确析构动态对象。

#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 ;
} 

这是普通析构函数,删除Ap时只能调用基类析构函数,应该在基类析构函数前加virtual析构

(4)注意:

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

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

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

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

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

构造函数、内联成员函数、静态成员函数不能是虚函数。

例3:

#include <iostream>
using namespace std;
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;
   }
};
int main()
{
     C objc;
     cout<<objc.funB(3)<<endl;
     B objb;
     cout<<objb.funB(3)<<endl;
}

运行结果为13.5和9,在用virtual限定funA后,就成为了虚函数,实现了覆盖,调用的是派生类的结果

例4:

#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;
}

show为虚函数,在派生类中实现了覆盖,主函数通过基类指针访问对象成员,调用的是对应派生类的show函数

3、纯虚函数和抽象类

不对虚函数做定义,而把它声明为纯虚函数,它的实现留给该基类的派生类去做,这是纯虚函数的作用

纯虚函数是一个在基类中说明的虚函数,在基类中没有定义, 要求任何派生类都定义自己的版本,纯虚函数为各派生类提供一个公共界面
纯虚函数说明形式:
virtual  类型  函数名(参数表)= 0 ;

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

例5:

#include<iostream>
using namespace std ;
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";
    }
};


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() ;
 }
在基类中定义了纯虚函数,后面三个派生类里每一个都做出了自己的定义

二、心得:

通过学习,我学会了直接利用基类指针访问同名成员的方式,但是必须要配合虚函数才能实现覆盖,达到多态性的目的。这一方面的应用的话,主要可以用在继承中同名成员的访问,比如图书管理系统的登录就可以用到这种方式,定义继承之后,直接在主函数中调用基类指针去访问管理和用户的同名登录函数,这一部分我还不是特别理解和掌握,这个登录我正在努力尝试去做,相信能够利用好这方面的知识。

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/80791307