C++纯虚函数
来源:互联网 发布:linux修复系统 编辑:程序博客网 时间:2024/05/16 19:17
1.纯虚函数的定义:
在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数,它的实现留给该基类的派生类去做。这就是纯虚函数的作用。
纯虚函数可以让类先具有一个操作名称,而没有操作内容,让派生类在继承时再去具体地给出定义。凡是含有纯虚函数的类叫做抽象类。这种类不能声明对象,只是作为基类为派生类服务。除非在派生类中完全实现基类中所有的的纯虚函数,否则,派生类也变成了抽象类,不能实例化对象。
2.引入纯虚函数的原因
1、为了方便使用多态特性,我们常常需要在基类中定义虚函数。
2、在很多情况下,基类本身生成对象是不合情理的。例如,动物作为一个基类可以派生出老虎、孔雀等子类,但动物本身生成对象明显不合常理。
为了解决上述问题,引入了纯虚函数的概念,将函数定义为纯虚函数(方法:virtual ReturnType Function()= 0;)。若要使派生类为非抽象类,则编译器要求在派生类中,必须对纯虚函数予以重写以实现多态性。同时含有纯虚函数的类称为抽象类,它不能生成对象。这样就很好地解决了上述两个问题。
3.相似的概念
多态性:
指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动作。C++支持两种多态性:编译时多态性,运行时多态性。
a.编译时多态性:通过重载函数实现
b运行时多态性:通过虚函数实现。
//////////////////////////////////////////////////// 纯虚函数/////////////////////////////////////////////////#include <iostream>#include <string>using namespace std ;// 基类class Item_Base{public:// 默认构造函数Item_Base( const string& book = "" , double sales_price = 0.0 ) : isbn( book ) , price( sales_price ) { }// 拷贝构造函数Item_Base( const Item_Base& item ) : isbn( item.isbn ) , price( item.price ) {}// 重载赋值操作符Item_Base& operator= ( const Item_Base& item ){isbn = item.isbn ;price = item.price ;return *this ;}string book() const { return isbn ; }virtual double net_price( int n ) const {return n * price ;}virtual ~Item_Base() { } private:string isbn ;public:double price ;} ;// 派生类class Bulk_Item : public Item_Base{public:// 构造函数Bulk_Item ( const string& book , double sales_price , int qty = 0 , double discount_rate = 0.0 ) : Item_Base( book , sales_price ) , min_qty( qty ) , discount( discount_rate ) { }// 默认构造函数Bulk_Item() { } // 拷贝构造函数Bulk_Item( const Bulk_Item& item ) : Item_Base( item ) , min_qty( item.min_qty ) , discount( item.discount ){}// 重载赋值操作符Bulk_Item& operator= ( const Bulk_Item& rhs ){if( this != &rhs ){Item_Base::operator =( rhs ) ;min_qty = rhs.min_qty ;discount = rhs.discount ;}return *this ;}double net_price( int n ) const ;private:int min_qty ;double discount ;} ;double Bulk_Item::net_price( int n ) const{if( n >= min_qty ){return n * price * ( 1 - discount ) ;}else{return n * price ;}}void print_total( ostream& os , const Item_Base& item , int n ){os << "ISBN: " << item.book() << "\nnumber_sold: " << n << "\ntotal_price: " << item.net_price( n ) << endl ;} int main(){Item_Base base( "123456" , 1.2 ) ;Bulk_Item derived( "123456" , 1.2 , 5 , 0.5 ) ;print_total( cout , base , 10 ) ;cout << endl ; print_total( cout , derived , 10 ) ;cout << endl ;Item_Base temp1 = base ; // 调用复制构造函数Bulk_Item temp2 = derived ; // 调用复制构造函数print_total( cout , base , 10 ) ;cout << endl ; print_total( cout , derived , 10 ) ;cout << endl ;Item_Base temp_base ; temp_base = base ; // 复制,调用复制操作符Bulk_Item temp_bulk ;temp_bulk = derived ; // 复制,调用复制操作符print_total( cout , temp_base , 10 ) ;cout << endl ; print_total( cout , temp_bulk , 10 ) ;return 0 ;}
- 【C/C++】纯虚函数
- [C++]虚函数与纯虚函数
- C++: 浅谈虚函数、纯虚函数
- C++::关于虚函数,纯虚函数
- [C++]虚函数和纯虚函数
- c++-虚函数与纯虚函数
- C方式实现C++纯虚函数
- 【c++】纯虚函数和抽象类
- C++:纯虚函数与抽象类
- 【C++】纯虚函数的简单应用。
- [C++]纯虚函数和抽象类
- c/c++----------虚函数、虚继承、纯虚函数
- 【C++】普通函数、虚函数和纯虚函数
- C/C++ 函数指针、虚函数、纯虚函数
- C++——虚函数与纯虚函数
- 虚函数和纯虚函数用法(C++)
- C++-虚函数,多态性,纯虚函数,抽象类
- 虚函数、纯虚函数通俗解释(C++,Java)
- 模拟RI锁定导致阻塞的场景
- 在Linux下adb连接不上android手机的终极解决方案
- Java中常出现的乱码问题总结
- global.cpp
- WinCE驱动开发感想
- C++纯虚函数
- redo日志损坏恢复总结:
- 第七章总结
- VC网络及通讯编程
- POJ 3253 Fence Repair 优先队列
- 《Expert Android》关键点摘录之一:Exploring Custom Views
- Andriod基本技巧——数据转化
- 走进小作坊(一)--------谈谈学习方法
- 解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 问题