Как перегрузить для полиморфного массива <
Недавно проходил перегрузку операторов. Сейчас задание написать полиморфный массив, но не знаю как перегрузить << для него. В классе Shop есть функция output, в которой записан цикл вывода. Думал достаточно буде перегрузить << для каждого класса, но к сожалению не так( Мне нужно либо для Shop перегрузить <<, чтобы вывести всю информацию массива, либо же создать метод output(как у меня), только рабочий.. Заранее спасибо)
P.s. Данные с файла file.txt
2
@ Pen China 20 1 1 2015
/#(без слеша) HeadPhones Italy 47 1 5 2016 technology
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream filein("file.txt");
ofstream fileout("errors.txt");
class Product {
protected:
string name, producer;
float price;
int day, month, year;
public:
Product();
Product(string n, string p, float pr, int d, int m, int y);
Product(const Product& l);
friend istream& operator>>(istream& in, Product& l);
friend ostream& operator<<(ostream& out, Product& l);
};
Product::Product() :name(""), producer(""), price(0), day(0), month(0), year(0) {};
Product::Product(string n, string p, float pr, int d, int m, int y) {
name = n;
producer = p;
price = pr;
day = d;
month = m;
year = y;
}
Product::Product(const Product& l) {
name = l.name;
producer = l.producer;
price = l.price;
day = l.day;
month = l.month;
year = l.year;
}
istream& operator>>(istream& in, Product& l) {
in >> l.name >> l.producer >> l.price >> l.day >> l.month >> l.year;
if (l.price < 0) {
fileout << "The price of " << l.name << " was less than zero. ";
cout << "ERROR! Enter a new price of " << l.name << " : ";
while (l.price < 0) {
cin >> l.price;
}
fileout << "It was changed on - " << l.price << endl;
}
if (l.day < 0 || l.day > 31) {
fileout << "The day of " << l.name << " was less than zero or bigger than thirty one. ";
cout << "ERROR! Enter a new day of " << l.name << " : ";
while (l.day < 0 || l.day > 31) {
cin >> l.day;
}
fileout << "It was changed on - " << l.day << endl;
}
if (l.month < 0 || l.month > 12) {
fileout << "The month of " << l.name << " was less than zero or bigger than twelve. ";
cout << "ERROR! Enter a new month of " << l.name << " : ";
while (l.month < 0 || l.month > 12) {
cin >> l.month;
}
fileout << "It was changed on - " << l.month << endl;
}
if (l.year < 0 || l.year > 2020) {
fileout << "The year of " << l.name << " was less than zero or bigger than two thousand and twenty. ";
cout << "ERROR! Enter a new year of " << l.name << " : ";
while (l.year < 0 || l.year > 2020) {
cin >> l.year;
}
fileout << "It was changed on - " << l.year << endl;
}
return in;
}
ostream& operator<<(ostream& out, Product& l) {
out << "Name - " << l.name << endl
<< "Producer - " << l.producer << endl
<< "Price - " << l.price << "$" << endl
<< "Date - " << l.day << "." << l.month << "." << l.year << endl << endl;
return out;
}
class FarmProduct : public Product {
protected:
string category;
public:
FarmProduct();
FarmProduct(string n, string p, float pr, int d, int m, int y, string c);
friend istream& operator>>(istream& in, FarmProduct& l);
friend ostream& operator<<(ostream& out, FarmProduct& l);
};
FarmProduct::FarmProduct() :category(""), Product() {}
FarmProduct::FarmProduct(string n, string p, float pr, int d, int m, int y, string c) : Product(n, p, pr, d, m, y) {
category = c;
}
istream& operator>>(istream& in, FarmProduct& l){
in >> l.name >> l.producer >> l.price >> l.day >> l.month >> l.year >> l.category;
if (l.price < 0) {
fileout << "The price of " << l.name << " was less than zero. ";
cout << "ERROR! Enter a new price of " << l.name << " : ";
while (l.price < 0) {
cin >> l.price;
}
fileout << "It was changed on - " << l.price << endl;
}
if (l.day < 0 || l.day > 31) {
fileout << "The day of " << l.name << " was less than zero or bigger than thirty one. ";
cout << "ERROR! Enter a new day of " << l.name << " : ";
while (l.day < 0 || l.day > 31) {
cin >> l.day;
}
fileout << "It was changed on - " << l.day << endl;
}
if (l.month < 0 || l.month > 12) {
fileout << "The month of " << l.name << " was less than zero or bigger than twelve. ";
cout << "ERROR! Enter a new month of " << l.name << " : ";
while (l.month < 0 || l.month > 12) {
cin >> l.month;
}
fileout << "It was changed on - " << l.month << endl;
}
if (l.year < 0 || l.year > 2020) {
fileout << "The year of " << l.name << " was less than zero or bigger than two thousand and twenty. ";
cout << "ERROR! Enter a new year of " << l.name << " : ";
while (l.year < 0 || l.year > 2020) {
cin >> l.year;
}
fileout << "It was changed on - " << l.year << endl;
}
return in;
}
ostream& operator<<(ostream& out, FarmProduct& l) {
out << "Name - " << l.name << endl
<< "Producer - " << l.producer << endl
<< "Price - " << l.price << "$" << endl
<< "Date - " << l.day << "." << l.month << "." << l.year << endl
<< "Category - " << l.category << endl << endl;
return out;
}
class Shop : public FarmProduct {
private:
string Name;
int size;
Product** mas;
public:
Shop();
Shop(string n, int s);
void input();
void output();
};
Shop::Shop() : Name(""), size(0), FarmProduct() {}
Shop::Shop(string n, int s) : FarmProduct() {
Name = n;
size = s;
mas = new Product * [size];
}
void Shop::input() {
string symbol;
for (int i = 0; i < size; i++) {
filein >> symbol;
if (symbol == "@") {
Product* pic = new Product;
filein >> *pic;
mas[i] = pic;
}
else if (symbol == "#") {
FarmProduct* farm = new FarmProduct;
filein >> *farm;
mas[i] = farm;
}
}
}
void Shop::output() {
for (int i = 0; i < size; i++) {
cout << mas[i]; // ошибка
} }
int Menu();
void Choice(string _name, int x, int n);
int main(){
string _name;
int n;
filein >> n;
cout << "Enter the name of your shop - ";
getline(cin, _name);
Choice(_name, Menu(), n);
}
int Menu() {
cout << "1) Input info.\n"
<< "2) Find produce.\n"
<< "3) Find the cheapest product with some name.\n"
<< "4) Number of products with the same name.\n"
<< "5) Add or remove product.\n"
<< "6) Output info.\n\n"
<< "Your choice - ";
int x;
cin >> x;
return x;
}
void Choice(string _name, int x, int n) {
Shop obj(_name, n);
switch (x)
{
case 1: {
obj.input();
Choice(_name, Menu(), n);
break;
}
case 6: {
obj.output();
Choice(_name, Menu(), n);
}
default:
break;
}
}