C++中,虚函数与容器(vector)的巧妙组合

这篇博客探讨了在C++中如何巧妙地结合虚函数和容器(如vector)来实现多态行为。通过创建一个纯虚基类base,以及两个继承自base的子类rea1和rea2,展示了在vector中存储基类指针,然后通过迭代器调用虚函数实现不同子类各自的方法,如init(), start(), end(),并调用子类特有的logprint功能。" 51366423,1462047,使用mmseg4j实现Solr中文分词,"['solr', '分词器', 'mmseg4j', '索引优化', '中文处理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <vector>
using namespace std;

class base    //纯虚基类
{
public:
 virtual void init() = 0;
 virtual void start() = 0;
 virtual void end() = 0;
};

class other1
{
public:
   void logprint1()
 {
  cout<<"print err1"<<endl;
 };
};

class other2
{
public:
 void logprint2()
 {
  cout<<"print err2"<<endl;
 };
};

class rea1:public other1,public base
{
public:
 void init()
 {
  cout<<"rea1"<<endl;
 };
 void start()
 {
  cout<<"start1"<<endl;
 };
 void end()
 {
  cout<<"end1"<<endl;
  logprint1();
 };
};

class rea

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值