簡體   English   中英

多項式ADT構造函數和刪除

[英]polynomial ADT constructor and deletion

我在理解如何在以下類中使用構造函數時遇到問題

class Polynomial{
private:
    typedef struct term{
        double coef;
        unsigned deg;
        struct term * next;
    }term_t;
    typedef struct term *Term;
    typedef struct term *Poly;
public:
    Polynomial(); //Constructor
    ~Polynomial(); //Destructor
    Poly newPoly(void);

如何分配構造函數? Poly newPoly(void)應該不帶任何條件地返回多項式。 我很難理解如何在這些函數的多項式中使用此特定結構。

刪除newPoly(void) 那只是在做構造函數應該做的工作。

刪除他們一無所獲的typedef。

多項式的編寫方式取決於類的設計方式,而您尚未告訴我們。 通常,對於此類,您將定義一些成員變量,然后在構造函數中對其進行初始化。 例如你可以寫

class Polynomial{
private:
    struct term{
        double coef;
        unsigned deg;
        term* next;
    };
    term* head; // pointer to first term
    int size; // number of terms
public:
    Polynomial() { head = NULL; size = 0; }
    ~Polynomial();
};

但這只是一個建議 由您來設計此類,並確定該設計需要哪些成員變量。

現在要獲得沒有條件的新多項式,您只需編寫

int main()
{
    Polynomial p; // a new polynomial
    ...
}

不要忘記,您還必須為此類編寫一個復制構造函數和一個賦值運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM