You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++如何从文本文件创建电影、图书、期刊三类对象并展示?

解决方案

你可以通过C++多态特性实现三类对象的统一创建与管理,具体实现如下:

完整可运行代码

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <memory> // 智能指针需要的头文件

using namespace std;

struct Item {
    string type;
    string id;
    string title;
    string year;
    // 基类添加虚析构函数,保证多态释放正确
    virtual ~Item() = default;
    // 虚打印函数,子类可以重写
    virtual void print() const {
        cout << "Type: " << type << endl;
        cout << "ID: " << id << endl;
        cout << "Title: " << title << endl;
        cout << "Year: " << year << endl;
    }
};

struct Movie : Item {
    string director;
    void print() const override {
        Item::print();
        cout << "Director: " << director << endl << endl;
    }
};

struct Book : public Item {
    string author;
    string pages;
    void print() const override {
        Item::print();
        cout << "Author: " << author << endl;
        cout << "Number of Pages: " << pages << endl << endl;
    }
};

struct Journal : public Item {
    string volume;
    string number;
    void print() const override {
        Item::print();
        cout << "Volume: " << volume << endl;
        cout << "Number: " << number << endl << endl;
    }
};

void readAllItems(ifstream& input, vector<unique_ptr<Item>>& items)
{
    string line;
    while(getline(input, line)) {
        istringstream iss(line);
        string type;
        // 先读取第一个字段判断类型
        getline(iss, type, ',');
        if (type == "Movie") {
            auto movie = make_unique<Movie>();
            movie->type = type;
            getline(iss, movie->id, ',');
            getline(iss, movie->title, ',');
            getline(iss, movie->year, ',');
            getline(iss, movie->director, ',');
            movie->print();
            items.push_back(move(movie));
        } else if (type == "Book") {
            auto book = make_unique<Book>();
            book->type = type;
            getline(iss, book->id, ',');
            getline(iss, book->title, ',');
            getline(iss, book->year, ',');
            getline(iss, book->author, ',');
            getline(iss, book->pages, ',');
            book->print();
            items.push_back(move(book));
        } else if (type == "Journal") {
            auto journal = make_unique<Journal>();
            journal->type = type;
            getline(iss, journal->id, ',');
            getline(iss, journal->title, ',');
            getline(iss, journal->year, ',');
            getline(iss, journal->volume, ',');
            getline(iss, journal->number, ',');
            journal->print();
            items.push_back(move(journal));
        }
    }
}

int main() {
    vector<unique_ptr<Item>> allItems;
    // 替换为你自己的文件路径
    ifstream input("/Users/nethmi/Desktop/library.txt");
    if(input.is_open()){
        readAllItems(input, allItems);
        input.close();
    } else {
        cout << "文件打开失败,请检查路径" << endl;
    }
    return 0;
}

逻辑说明

  • 所有对象都通过基类Item的智能指针统一存储在同一个vector中,不需要分别维护三个类型的存储容器
  • 读取每行数据时优先解析第一个字段的类型标识,根据类型创建对应子类实例,解析对应数量的字段
  • 通过虚函数实现打印逻辑的多态调用,不同类型的对象会执行各自的打印逻辑

内容的提问来源于stack exchange,提问作者Nethmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 17:06:07