Школьное задание: я получил исходный код, который я не могу изменить. Я должен реализовать следующие вещи: интерфейсы, сделанные из чистых виртуальных машин «IPrintable» и «IComparable», соответствующий интерфейс - IPrintable, который содержит перегрузки для операторов ввода-вывода. Класс Date, который реализует эти два. и другой шаблонный класс, который не имеет отношения к этому вопросу
Моя текущая проблема - я не уверен, как сделать перегрузку, пока она чиста, так как операторы io также являются друзьями, а не прямыми. Это не должен быть шаблон, однако я не могу придумать другой способ получить легкий доступ к производным функциям и частным лицам. Это заголовок ввода-вывода:
#include <iostream>
using namespace std;
#ifndef IPrintable_h
#define IPrintable_h
template<typename T>
class IPrintable {
virtual friend ostream& operator<<(ostream&, const T&) = 0;
virtual friend istream& operator>>(istream&, T&) = 0;
};
#endif // ! IPrintable_h
соответствующие части заголовка производного класса:
#include "IComparable.h"
#include "IPrintable.h"
#include <iostream>
using namespace std;
.
.
class Date : public IPrintable<Date>, IComparable<Date> {
.
.
.
friend ostream& operator<< (ostream&, const Date&);
friend istream& operator>> (istream&, Date&);
До сих пор VS не говорил, что есть ошибки, однако я не уверен, поэтому я спрашиваю здесь. если их нет, то проблема, вероятно, заключается в том, как я пытаюсь реализовать их в Date. cpp:
ostream& std::operator<<(ostream& stream, const Date& requested){
Неизменяемая часть исходного кода:
void testDate() {
Date independence(14, 5, 1948);
Date otherDate = independence;
cout << "Independence:" << independence << ", Other: " << otherDate << endl;
otherDate.setMonth(2);
cout << "Other date: " << otherDate << endl;
otherDate.setDay(29);
cout << "Other date: " << otherDate << endl;
otherDate.setYear(1947);
cout << "Other date: " << otherDate << endl;
otherDate = Date(24, 1, 1959);
cout << "Other date: " << otherDate << endl;
cout << "Comparing using polymorphism" << endl;
IComparable<Date> *indP = dynamic_cast <IComparable<Date> *> (&independence);
cout << "Is independence <= otherDate ? " << (*indP <= otherDate) << endl;
IComparable<Date> *otherP = dynamic_cast <IComparable<Date> *> (&otherDate);
cout << "Is other date <= independence ? " << (*otherP <= independence) << endl;
}
void testDateInput() {
Date otherDate(1, 1, 1);
do {
cin >> otherDate;
cout << otherDate << endl;
} while (otherDate != Date(1, 1, 1));
}
Отредактировано для чтобы сделать назначение более понятным
Добавление части запрошенного исходного кода и полных заголовков