C++虚函数与纯虚函数用法与区别

1.概念

 定义一个函数为虚函数,不代表函数为不被实现的函数。定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。
 定义一个函数为纯虚函数,才代表函数没有被实现。定义他是为了实现一个接口,起到一个规范的作用,规范继承这个。类的程序员必须实现这个函数。

-------------------------

C++的虚函数主要作用是“运行时多态”,父类中提供虚函数的实现,为子类提供默认的函数实现

子类可以重写父类的虚函数实现子类的特殊化。

class A
{
public:
    virtual void out2(string s)
    {
        cout<<"A(out2):"<<s<<endl;
    }
};

-------------------------

C++中包含纯虚函数的类,被称为是“抽象类”。抽象类不能使用new出对象,只有实现了这个纯虚函数的子类才能new出对象。

C++中的纯虚函数更像是“只提供申明,没有实现”,是对子类的约束,是“接口继承”。

C++中的纯虚函数也是一种“运行时多态”。

class A
{
public:
    virtual void out1(string s)=0;
    virtual void out2(string s)
    {
        cout<<"A(out2):"<<s<<endl;
    }
};

例子:

#include <iostream>
using namespace std;

class A
{
public:
    virtual void out1()=0;  ///由子类实现
    virtual ~A(){};
    virtual void out2() ///默认实现
    {
        cout<<"A(out2)"<<endl;
    }
    void out3() ///强制实现
    {
        cout<<"A(out3)"<<endl;
    }
};

class B:public A
{
public:
    virtual ~B(){};
    void out1()
    {
        cout<<"B(out1)"<<endl;
    }
    void out2()
    {
        cout<<"B(out2)"<<endl;
    }
    void out3()
    {
        cout<<"B(out3)"<<endl;
    }
};

int main()
{
    A *ab=new B;
    ab->out1();
    ab->out2();
    ab->out3();
    cout<<"************************"<<endl;
    B *bb=new B;
    bb->out1();
    bb->out2();
    bb->out3();

    delete ab;
    delete bb;
    return 0;
}

2.实例化一个类 

有纯虚函数的类是不可能生成类对象的,如果没有纯虚函数则可以。比如: 

class CA 
{ 
public: 
    virtual void fun() = 0;  // 说明fun函数为纯虚函数 
    virtual void fun1(); 
}; 

class CB 
{ 
public: 
   virtual void fun(); 
   virtual void fun1(); 
}; 

// CA,CB类的实现 
... 

void main() 
{ 
    CA a;   // 不允许,因为类CA中有纯虚函数 
    CB b;   // 可以,因为类CB中没有纯虚函数 
    ... 
} 

3.虚函数在多态中间的使用

多态一般就是通过指向基类的指针来实现的。

4.用父类指针在运行时刻来调用子类

void animal::fun1(animal *maybedog_maybehorse) 
{ 
     maybedog_maybehorse->born();
} 

参数maybedog_maybehorse在编译时刻并不知道传进来的是dog类还是horse类,所以就把它设定为animal类,具体到运行时决定了才决定用那个函数。也就是说用父类指针通过虚函数来决定运行时刻到底是谁而指向谁的函数。

5.用虚函数

#include <iostream.h> 

class animal 
{ 
public: 
     animal(); 
     ~animal(); 
     void fun1(animal *maybedog_maybehorse); 
     virtual void born(); 
}; 

void animal::fun1(animal *maybedog_maybehorse) 
{ 
     maybedog_maybehorse->born(); 
}

animal::animal() { } 
animal::~animal() { } 
void animal::born() 
{ 
     cout<< "animal"; 
} 
///horse
class horse:public animal 
{ 
public: 
     horse(); 
     ~horse(); 
     virtual void born(); 
}; 

horse::horse() { } 
horse::~horse() { } 
void horse::born()
{ 
     cout<<"horse"; 
} 
///main
void main() 
{ 
     animal a; 
     horse b; 
     a.fun1(&b); 
} 

//output: horse

6.不用虚函数

#include <iostream.h> 
class animal 
{ 
public: 
     animal(); 
     ~animal(); 
     void fun1(animal *maybedog_maybehorse); 
     void born(); 
}; 

void animal::fun1(animal *maybedog_maybehorse) 
{ 
     maybedog_maybehorse->born(); 
} 

animal::animal() { }
animal::~animal() { } 
void animal::born() 
{ 
     cout<< "animal"; 
} 
horse
class horse:public animal 
{ 
public: 
     horse(); 
     ~horse(); 
     void born(); 
}; 

horse::horse() { } 
horse::~horse() { } 
void horse::born()
{ 
     cout<<"horse"; 
} 
main
void main() 
{ 
     animal a; 
     horse b; 
     a.fun1(&b); 
} 
//output: animal

http://www.cppblog.com/ElliottZC/archive/2007/07/20/28417.html

https://www.cnblogs.com/xudong-bupt/p/3570304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值