Search code examples
c++inheritancedefault-constructor

C++ Inheritance Default Constructor in Derived Class Issue


I'm having some issues with inheritance. I have included the two different classes that I'm working with (WorkTicket & ExtendedWorkTicket). ExtendedWorkTicket should be using the member variables of WorkTicket, which is why I have used them as protected.

My issue comes in when I try to create the default constructor for ExtendedWorkTicket. I get an error telling me that ExtendedWorkTicket doesn't have the field names [...] (all except myIsOpen).

I know I can replace all those member variables with WorkTicket(1, "", 1, 1, 2014, "") in the constructor, but that changes the functionality as it would be calling the Parameterized constructor from WorkTicket and tries to validate them using the setters, which it shouldn't be doing.

I need it to just create a blank WorkTicket (with the extra myIsOpen member), like what the WorkTicket's default constructor does.

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

And the inheriting child 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

Solution

  • 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) { }
    

    Remove the initializations of the base-class members; those are handled by the base-class constructor. You don't need to explicitly call out the base constructor, but you can if you think that's clearer.