繁体   English   中英

如何在 C++ 中实现接口

[英]How to implement an interface in C++

我有一个任务,并试图理解一些东西。 我有一个创建两个接口的指令: IComparableIPrintable 另外,我需要创建一个名为Interval的模板。

我得到了main函数,我需要相应地实现这些类,以便它按预期工作。

这是我目前正在实现的功能(注释显示输入应该是什么样子):

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

}

如果您查看代码,您会发现我卡在哪里,这就是我的问题:据我所知,这种类型的写作是使用模板。 但是在说明中, IComparable被称为接口而不是模板。

我如何使用接口实现这个? 我可以使用接口实现它吗?

这是我的 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;
}

什么不起作用?

让我们疏散您的线路问题:您不需要dynamic_cast来实现多态性。 dynamic_cast应该只在非常特殊的情况下使用。 而转换要成功,它需要源类型和目标类型之间的一些继承关系(这里是DateIComparable<Date> )。 不幸的是,如果没有类定义和错误消息,就不可能提供进一步的建议。

什么是接口?

你是对的: IComparable<Date>是一个模板,而不是一个接口。

接口不是 C++ 语言功能。 但是对于它是什么有一个共同的理解:它是一个没有功能的类,旨在承诺将由其他类实现的行为。 C++ 标准是这样描述的(在脚注中,所以它只是指示性的):

抽象类还可用于定义接口,派生类为其提供各种实现

函数必须是虚拟的才能获得多态行为。 此外,抽象类是具有纯虚函数(即未定义的函数)的类,因此永远不能直接实现。

下一步 ?

一般的想法是:

 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
 };  

然后你可以让Date实现它:

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
}; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM