派生类问题中的C ++继承默认构造函数

时间:2014-10-23 18:48:05

标签: c++ inheritance default-constructor

我遇到了一些继承问题。我已经包含了我正在使用的两个不同的类(WorkTicket& ExtendedWorkTicket)。 ExtendedWorkTicket应该使用WorkTicket的成员变量,这就是我将它们用作受保护的原因。

当我尝试为ExtendedWorkTicket创建默认构造函数时,我的问题出现了。我收到错误,告诉我ExtendedWorkTicket没有字段名[...](除了myIsOpen之外的所有字段)。

我知道我可以在构造函数中用WorkTicket(1, "", 1, 1, 2014, "")替换所有这些成员变量,但这会改变功能,因为它会从WorkTicket调用参数化构造函数并尝试使用setter验证它们,不应该这样做。

我需要它来创建一个空白WorkTicket(带有额外的myIsOpen成员),就像WorkTicket的默认构造函数那样。

class WorkTicket
{
   public:

    /***************************************************************************
    *   Default and parameterized constructor(s).  
    *   If parameters are not specified, set the work ticket number to zero, 
    *   the work ticket date to 1/1/2000, and all other attributes to empty 
    *   strings.
    ***************************************************************************/

     WorkTicket() : myTicketNumber(0), myClientId(""), myDate(1, 1, 2014), myDescription ("") { } 
     WorkTicket(int ticketNumber, string clientId, int day, int month, int year, string description);

    /***************************************************************************
    *    Copy constructor 
    *    Initializes a new WorkTicket object based on an existing WorkTicket 
    *    object. 
    ***************************************************************************/
     WorkTicket(const WorkTicket& original);

    /***************************************************************************
    *   SetWorkTicket() 
    *   a mutator method to set all the attributes of the object to the 
    *   parameters as long as the parameters are valid. ALL of the parameters 
    *   must be valid in order for ANY of the attributes to change. Validation 
    *   rules are explained for work ticket number and date. Client number 
    *   and Description must be at least one character long. If no problems are 
    *   detected, return TRUE.  Otherwise return FALSE. 
    ***************************************************************************/

     bool SetWorkTicket(int ticketNumber, string clientId, int day, int month, int year, string description);

    /***************************************************************************
    *   ShowWorkTicket( )
    *   An accessor method to display all the object's attributes neatly in 
    *   the console window. 
    ***************************************************************************/    

     virtual void ShowWorkTicket() const; // accessor       

    /***************************************************************************
    *   Attribute Sets/Gets.  
    *   Include a set (mutator) and get (accessor) method for each attribute. 
    ***************************************************************************/

     // Ticket Number     
     void SetTicketNumber(int ticketNumber); 
     int GetTicketNumber() const {return myTicketNumber;}

     // Client ID
     void SetClientId(string clientId) {myClientId = clientId;}
     string GetClientId() const { return myClientId;}

     // Decsription
     void SetDescription(string description) { myDescription = description; }   
     string GetDescription() const { return myDescription; }

     // Date
     void SetDate(int day, int month, int year);
     const MyDate& GetDate() const { return myDate; }

    /***************************************************************************
    *   Operators (LAB 3).  
    *   Include a set (mutator) and get (accessor) method for each attribute. 
    ***************************************************************************/
    WorkTicket& operator=(const WorkTicket& original); // Assignment
    operator string () const;   // (string)
    bool operator==(const WorkTicket& original); // Equality
    friend ostream& operator<<(ostream& out, const WorkTicket& ticket); // Output
    friend istream& operator>>(istream& in, WorkTicket& ticket); // Input

    protected:

    /***************************************************************************
    *   Private Attributes.  An object of class WorkTicket has the following 
    *       private attributes. 
    ***************************************************************************/

    int myTicketNumber; // Work Ticket Number - A whole, positive number.
    string myClientId;      // Client ID - The alpha-numeric code assigned to the client.
    MyDate myDate;      // Work Ticket Date - the date the workticket was created     
    string myDescription;  // Issue Description - A description of the issue the client is having.
};  // end of WorkTicket class

继承子类:

/***************************************************************************
*    LAB 4 Inheritance Class - ExtendedWorkTicket
*
***************************************************************************/
class ExtendedWorkTicket : public WorkTicket
{

    public:
        ExtendedWorkTicket() : myTicketNumber(0), myClientId(""), myDate(1, 1, 2014), "", myIsOpen(true) { } 
        ExtendedWorkTicket(int ticketNumber, string clientID, int day, int month, int year, string description, bool isOpen) : WorkTicket(ticketNumber, clientID, day, month, year, description) {};

        bool GetIsOpen() const { return myIsOpen; }

        bool SetWorkTicket(int ticketNumber, string clientId, int day, int month, int year, string description, bool isOpen = true);
        void ShowWorkTicket() const override;

        void CloseTicket() { myIsOpen = !myIsOpen; }

        friend ostream& operator<<(ostream& out, const ExtendedWorkTicket& ticket); // Output

    private:
        bool myIsOpen;  

}; // end of ExtendedWorkTicket

1 个答案:

答案 0 :(得分:1)

ExtendedWorkTicket::ExtendedWorkTicket() :
#if defined(OPTIONAL)
    WorkTicket(),
#endif
#if defined(ERRONEOUS)
    myTicketNumber(0), myClientId(""), myDate(1, 1, 2014),
    "",   // incidentally, this literal is just bad syntax, it's not assigned to anything
#endif
    myIsOpen(true) { }

删除基类成员的初始化;这些由基类构造函数处理。您不需要显式调出基础构造函数,但如果您认为更清楚,则可以。