Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trials h
30 PointsI have a C++ question Can you help
C++ Please Help :
include <fstream>
include <iomanip>
include <iostream>
include <string>
include <cstdlib>
include <iterator>
include <vector>
include <sstream>
using namespace std;
class Car
{
friend istream &operator>>(istream&, Car&);
friend ostream &operator<<(ostream&, Car&);
public:
Car (string carBrand, string carModel, int carYear);
string getBrand();
string getModel();
int getYear();
protected:
string brand;
string model;
int year;
};
istream &operator>>(istream& is, Car& c_in)
{
string line;
getline(is, line);
istringstream iss(line);
iss >> c_in.brand;
iss >> c_in.model;
iss >> c_in.year;
getline(iss, c_in.brand);
getline(iss, c_in.model);
iss >> c_in.year;
return is;
} ostream &operator<<(ostream& os, Car& c_out) { os << c_out.brand <<" "<<c_out.model <<" "<<c_out.year; return os; } Car::Car(string carBrand, string carModel, int carYear) :brand(carBrand), model(carModel), year(carYear) {} string Car::getBrand() { return brand; } string Car::getModel() { return model; } int Car::getYear() { return year; } ///////////////////////////////////////////////////////////// class Parts { friend istream &operator>>(istream&, Parts&); friend ostream &operator<<(ostream&, Parts&); public: Parts(); Parts(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold); string getDescription(); float getPrice(); string getManufacturer(); int getQtySold(); protected: string description; float price; string manufacturer; int qtySold; };
istream& operator>>(istream& is, Parts& p_in) { string line; getline(is, line);
istringstream iss(line);
iss >> p_in.description;
iss >> p_in.price;
iss >> p_in.manufacturer;
iss >> p_in.qtySold;
getline(iss, p_in.description);
iss >> p_in.price;
getline(iss, p_in.manufacturer);
iss >> p_in.qtySold;
return is;
} ostream &operator<<(ostream& os, Parts& p_out) { os << p_out.description <<p_out.price <<p_out.manufacturer<<p_out.qtySold; return os; } Parts::Parts() { description = ""; price = 0; manufacturer = ""; qtySold = 0;
} Parts::Parts(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold) :description(partsDescription), price(partsPrice), manufacturer(partsManufacturer), qtySold(partsQtySold) {} string Parts::getDescription() { return description; } float Parts::getPrice() { return price; } string Parts::getManufacturer() { return manufacturer; } int Parts::getQtySold() { return qtySold; } ///////////////////////////////////////////////////////////// class Brakes :public Parts { friend istream &operator>>(istream&, Brakes&); friend ostream &operator<<(ostream&, Brakes&); public: Brakes(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold,Car c, string brakesMaterial); string getMaterial(); protected: Car car; string material; }; istream &operator>>(istream& is, Brakes& b_in) { string line; getline(is, line);
istringstream iss(line);
iss >> b_in.material;
iss >> b_in.description;
iss >> b_in.price;
iss >> b_in.manufacturer;
iss >> b_in.qtySold;
getline(iss, b_in.description);
iss >> b_in.price;
getline(iss, b_in.manufacturer);
iss >> b_in.qtySold;
getline(iss, b_in.material);
return is;
} ostream &operator<<(ostream& os, Brakes& b_out) { os << b_out.description <<b_out.price <<b_out.manufacturer <<b_out.qtySold << b_out.car.getBrand() <<b_out.car.getModel() << b_out.car.getYear()<<b_out.material; return os; } Brakes::Brakes(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold, Car c, string brakesMaterial) : Parts (partsDescription, partsPrice, partsManufacturer,partsQtySold),car(c), material(brakesMaterial) {} string Brakes::getMaterial() { return material; } ///////////////////////////////////////////////////////////// class Lights : public Parts { friend istream &operator>>(istream&, Lights&); friend ostream &operator<<(ostream&, Lights&); public:
Lights(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold,Car c, int wat);
int getWatts();
protected:
Car car;
int watts;
}; istream &operator>>(istream& is , Lights& l_in) { string line; getline(is, line);
istringstream iss(line);
iss >> l_in.watts;
iss >> l_in.description;
iss >> l_in.price;
iss >> l_in.manufacturer;
iss >> l_in.qtySold;
getline(iss, l_in.description);
iss >> l_in.price;
getline(iss, l_in.manufacturer);
iss >> l_in.qtySold;
iss >> l_in.watts;
return is;
} ostream &operator<<(ostream& os, Lights& l_out) { os << l_out.description <<l_out.price <<l_out.manufacturer <<l_out.qtySold << l_out.car.getBrand() <<l_out.car.getModel() << l_out.car.getYear() << l_out.watts; return os; } Lights::Lights(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold,Car c, int wat) : Parts (partsDescription, partsPrice, partsManufacturer,partsQtySold), car(c), watts(wat) {} int Lights::getWatts() { return watts; } /////////////////////////////////////////////////////////////
class Oil : public Parts
{
friend istream &operator>>(istream&, Oil&);
friend ostream &operator<<(ostream&, Oil&);
public:
Oil();
Oil(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold, float weight,string type , float quarts);
float getWeight();
string getType();
float getQuarts();
protected:
float weight;
string type;
float quarts;
};
istream &operator>>(istream& is, Oil& o_in)
{
string line;
getline(is, line);
istringstream iss(line);
iss >> o_in.weight;
iss >> o_in.type;
iss >> o_in.quarts;
iss >> o_in.description;
iss >> o_in.price;
iss >> o_in.manufacturer;
iss >> o_in.qtySold;
getline(iss, o_in.description);
iss >> o_in.price;
getline(iss, o_in.manufacturer);
iss >> o_in.qtySold;
iss >> o_in.weight;
getline(iss, o_in.type);
iss >> o_in.quarts;
return is;
} ostream &operator<<(ostream& os, Oil& o_out) { os << o_out.description<<o_out.price <<o_out.manufacturer <<o_out.qtySold << o_out.weight << o_out.type << o_out.quarts; return os; } Oil::Oil() { weight = 0.0; type = " "; quarts = 0.0; } Oil::Oil(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold, float wei,string typ , float quart) : Parts (partsDescription, partsPrice, partsManufacturer,partsQtySold), weight(wei), type(typ), quarts(quart) {} float Oil::getWeight() { return weight; } string Oil::getType() { return type; } float Oil::getQuarts() { return quarts; } ///////////////////////////////////////////////////////////// class Tires : public Parts { friend istream &operator>>(istream&, Tires&); friend ostream &operator<<(ostream&, Tires&); public: Tires(); Tires(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold,float tireSize ,int tireWarranty); float getSize(); int getWarranty(); protected: float size; int warranty ; }; istream &operator>>(istream& is, Tires& t_in) { string line; getline(is, line);
istringstream iss(line);
iss >> t_in.size;
iss >> t_in.warranty;
iss >> t_in.description;
iss >> t_in.price;
iss >> t_in.manufacturer;
iss >> t_in.qtySold;
getline(iss, t_in.description);
iss >> t_in.price;
getline(iss, t_in.manufacturer);
iss >> t_in.qtySold;
iss >> t_in.size;
iss >> t_in.warranty;
return is;
} ostream &operator<<(ostream& os, Tires& t_out) { os << t_out.description <<t_out.price <<t_out.manufacturer <<t_out.qtySold << t_out.size<< t_out.warranty; return os; } Tires::Tires() { size = 0; warranty = 0 ; } Tires::Tires(string partsDescription, float partsPrice, string partsManufacturer,int partsQtySold,float tireSize ,int tireWarranty) : Parts (partsDescription, partsPrice, partsManufacturer,partsQtySold), size(tireSize), warranty(tireWarranty) {} float Tires::getSize() { return size; } int Tires::getWarranty() { return warranty; }
///////////////////////////////////////////////////////////// int main() { ifstream FileIn("/Users/fatemehrostami/Desktop/Data.txt", ios::in); if (!FileIn) { cerr << "File could not be open"<<endl; exit (EXIT_FAILURE);
}