问题
I have an assignment and trying to understand something. I have an instruction to create two interfaces: IComparable
and IPrintable
. Also, I need to create a template called Interval
.
I am given the main
function and I need to implement these classes accordingly so it will work as intended.
This is the function I am currently implementing (the comments displays what the input should look like):
void testDate() {
Date independence(14, 5, 1948);
cout << independence << endl;
Date otherDate = independence;
cout << "Independence:" << independence << ", Other: " << otherDate << endl; // Independence:14/05/1948, Other: 14/05/1948
otherDate.setMonth(2);
cout << "Other date: " << otherDate << endl; // Other date: 14/02/1948
otherDate.setDay(29);
cout << "Other date: " << otherDate << endl; // Other date: 29/02/1948
otherDate.setYear(1947);
cout << "Other date: " << otherDate << endl; // Other date: Not a leap year
otherDate = Date(24, 1, 1959);
cout << "Other date: " << otherDate << endl; // Other date: 24/01/1959
cout << "Comparing using polymorphism" << endl; // Comparing using polymorphism
IComparable<Date> *indP = dynamic_cast <IComparable<Date> *> (&independence);
/* --------------------------- ^^^ Stuck in the line above ^^^ --------------------------- */
cout << "Is independence <= otherDate ? " << (*indP <= otherDate) << endl; // Is independence <= otherDate ? true
IComparable<Date> *otherP = dynamic_cast <IComparable<Date> *> (&otherDate);
cout << "Is other date <= independence ? " << (*otherP <= independence) << endl; // Is other date <= independence ? false
}
If you will look at code, you can see where I am stuck and that's my problem:
As far as I know, this type of writing is using templates. But in the instructions, IComparable
is said to be an interface and not a template.
How can I implement this using an interface? Can I implement it using an interface?
This is my Date.cpp:
#include <iostream>
#include "Date.h"
#include "IComparable.h"
using namespace std;
void Date::setDay(int d) { day = d; }
int Date::getDay() const { return day; }
void Date::setMonth(int m) { month = m; }
int Date::getMonth() const { return month; }
void Date::setYear(int y) { year = y; }
int Date::getYear() const { return year; }
Date::Date(int d, int m, int y) {
setDay(d);
setMonth(m);
setYear(y);
}
void Date::operator= (const Date& other) {
day = other.getDay();
month = other.getMonth();
year = other.getYear();
}
void Date::toOs(ostream& output) const {
// TODO : Check if leap year!
output << getDay() << "/" << getMonth() << "/" << getYear();
}
bool Date::isLeapYear(int yearToCheck) const {
if (yearToCheck % 4 == 0)
{
if (yearToCheck % 100 == 0)
{
if (yearToCheck % 400 == 0)
return true;
else
return false;
}
else
return false;
}
else
return false;
return false;
}
回答1:
What doesn't work ?
Let's evacuate your line issue: You do not need dynamic_cast to implement polymorphism. dynamic_cast
should only be used in very specific cases. And the cast to succeed, it requires some inheritance relations between the source and target types (here Date
and IComparable<Date>
). Unfortunately, without the class definitions and the error message, it's not possible to advise further.
What is an interface ?
You are right: IComparable<Date>
is a template, and not an interface.
Interfaces are not a C++ language feature. But there is a common understanding about what it is: it's a class with no functionality that is meant to promise a bahavior that will be implemented by other classes. The C++ standard describes it this way (in a foot note, so it's indicative only):
An abstract class can also be used to define an interface for which derived classes provide a variety of implementations
The functions have to be virtual to get the polymorphic behavior. Abstract classes are furthermore classes that have pure virtual functions (i.e. functions that are not defined) and that can therefore never be implemented directly.
Next steps ?
The general idea would be:
class IComparable {
public:
virtual bool is_equal(const IComparable &a, const IComparable &b) = 0;
virtual bool is_lesser(const IComparable &a, const IComparable &b) = 0;
... // whetever else you want
virtual ~IComparable(){}; // good practice: one virtual function -> virtual destructor
};
You could then let Date
implement it:
class Date : public IComparable {
public:
bool is_equal(const IComparable &a, const IComparable &b) override;
... // you SHOULD override all the pure virtual of the interface
};
来源:https://stackoverflow.com/questions/59717270/how-to-implement-an-interface-in-c