簡體   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