实验E13:虚函数与运行时多态

在这里插入图片描述

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

class StrHandler
{

protected:
    vector<char>storage_ch;
public:
    StrHandler() {}
    virtual ~StrHandler() {}
    virtual void function(const string& filename) {};
};

class PrintStr :
    public StrHandler
{
public:
    PrintStr() {}

    void function(const string& filename)
    {

        cout << ifstream(filename).rdbuf() << endl;
    }

};


class GetMostStr :
    public StrHandler
{
public:
    GetMostStr() {}

    virtual ~GetMostStr() {}

    void function(const string& filename)
    {
        ifstream ifs(filename);
        string longest, temp;
        while (ifs >> temp)
        {
            if (temp.length() > longest.length())
            {
                longest = temp;
            }

        }
        cout << "包含字符数最多的字符串是" << longest << endl;
    }


};



class Transform :
    public StrHandler
{
public:
    Transform() {}

    virtual ~Transform() {}
    void function(const string& filename)
    {
        ifstream ifs(filename);
        while (!ifs.eof())
        {
            char ch = ifs.get();
            if (ch >= 'A' && ch <= 'Z')
            {
                storage_ch.push_back(ch - ('A' - 'a'));
            }
            else
            {
                storage_ch.push_back((ch));
            }
        }
        cout << "转换完成" << endl;
        cout << "我们进行测验是否真的全部转换成了小写字符" << endl;
        for (int i = 0; i < storage_ch.size(); i++)
        {
            cout << storage_ch[i];
        }

    }
};

void readStrings(const string& file, StrHandler* p)
{
    p->function(file);
}



int main()
{
    string file = "C:\\Users\\4399\\test2.txt";
    PrintStr pstr;
    cout << "接下来进行转换操作" << endl;
    readStrings(file, &pstr);

    GetMostStr gmstr;
    readStrings(file, &gmstr);

    Transform tran;
    cout << "接下来进行转换大小写操作" << endl;
    readStrings(file, &tran);



    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值