关于继承和多态,有个例子无法实现

飞翔的薄荷 飞翔的薄荷 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

以下代码需要实现printer printer_imp1  printer_imp2 . 但是一直无法实现,求解决.

#include <vector>
#include <sstream>

int main()
{
    int test[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::vector<int> v(test, test + 10);
    std::vector<std::shared_ptr<printer>> ps;

    {
        auto p = std::make_shared<printer_imp1>();
        ps.push_back(p);  //按逗号分割打印
    }

    {
       auto p = std::make_shared<printer_imp12>();
        ps.push_back(p); //按回车分割打印
    }

    for (auto& p : ps)
    {
        p->print(std::cout,test);
        std::stringstream ss;
        p->print(ss, v);      
    }

    getchar();
}

 

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
飞翔的薄荷
C++ 语言 65,214 成员 250,514 内容