gpt4 book ai didi

C++ 类,与不同用户一起处理一个文件

转载 作者:行者123 更新时间:2023-11-28 06:18:48 24 4
gpt4 key购买 nike

我有一个基类和一个派生类。目前,我正在分别读取每个文件,即 carfile 和 sportsCarFile。然后将它们加载为成员以用于其他目的。我试图了解如何读取一个文件,然后正确读取文件中的每一行并调用正确的加载函数。下表显示了数据的外观:

Car/SportsCar  CarName Age Colour Price 
Car Abbort 8 Yellow 899.99
SportsCar Aufdi 7 Brown 989.99
Car ATX 5 White 9823.23
Car POL 3 Yellow 8232.33

这是当前代码:

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Car{
public:
Car();
virtual void Load(ifstream& carFile);
virtual int LoadString(string filename);
void display();

protected:
string CarName;
int Age;
string Colour;
double Price;
int countCars;
Car *ptrToCarList;
};

class SportsCar : public Car
{
public:
SportsCar();
virtual void LoadSports(ifstream& carFile);
virtual int LoadString(string filename);
void displayLoad();
void display();
protected:
int engineSize;
SportsCar *ptrToSportsCarList;
int countCars;

};

Car::Car()
{
CarName = "Unknown";
countCars = 0;
}

void Car::Load(ifstream& carFile)
{
carFile >> CarName >> Age >> Colour >> Price;
}

int Car::LoadString(string filename)
{
ifstream inFile(filename);

if (!inFile)
{
cout << "Sorry, file not found" << endl;
return -1;
}

ptrToCarList = new Car[countCars];

for (int i = 0; i < countCars; i++)
{
ptrToCarList[i].Load(inFile);
}
inFile.close();
return 0;
}

void Car::display()
{
cout << CarName << " " << Age << " " << Colour << " " << Price << " " ;
}

void SportsCar::displayLoad()
{
Car::display();
cout<<engineSize<<endl;
}

void SportsCar::display()
{
for (int i = 0; i < countCars; i++)
{
ptrToSportsCarList[i].displayLoad();
}
}


void SportsCar::LoadSports(ifstream& carFile){
Car::Load( carFile);
carFile >> engineSize;
}

SportsCar::SportsCar()
{
CarName = "Unknown";
countCars = 0;

}

int SportsCar::LoadString(string filename)
{
ifstream inFile(filename);

if (!inFile)
{
cout << "Sorry, file not found" << endl;
return -1;
}
countCars = 2;

ptrToSportsCarList = new SportsCar[countCars];

for (int i = 0; i < countCars; i++)
{
ptrToSportsCarList[i].LoadSports(inFile);
}
inFile.close();
return 0;
}

int main()
{
SportsCar example2;
example2.LoadString("sportsCarFile.txt");
example2.display();
return 0;
}

最佳答案

我会创建一个函数来读取汽车然后返回适当的类型(使用 shared_ptr 所以我们不需要管理内存):

shared_ptr<Car> loadCar(ifstream& in)
{
string type;
in >> type; // is this syntax right? I don't use C++ very often
shared_ptr<Car> car;
if (type == "SportsCar") {
car = new SportsCar();
} else if (type == "Car") {
car = new Car();
} else {
throw [some sort of exception?];
}
car.load(in);
}

我觉得这叫工厂。好处是你的Car - 解析代码会弄清楚它是什么类型的汽车,所以这是你在使用 API 时不必考虑的事情:

ifstream f(filename);
vector<shared_ptr<Car>> cars;
while (!f.eof()) {
cars.push_back(loadCar(f));
}

请注意 Car::load()SportsCar::load()应该都返回一辆汽车,而不是像您当前的 Car 那样的列表类(class)。我选择使用 vector 因为他们很容易合作。您的类(class)也过于复杂。

所以,我会把你的类(class)改成这样:

class Car{
public:
Car();
virtual ~Car();
virtual void load(ifstream& carFile);
virtual void display();

protected:
string name;
int age;
string colour;
double price;
};

class SportsCar : public Car
{
public:
SportsCar();
virtual void load(ifstream& carFile);
virtual void display();
protected:
int engineSize;
};

或者更好,overload operator<<() , 所以你可以做 cout << car .

关于C++ 类,与不同用户一起处理一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29709482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com