Thread: constructor??

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    constructor??

    i have two classes

    //Event class
    class Event
    {
    public:
    Event();
    Event(const Time & time, const char* name);
    Event(const Event & event);
    ~Event();
    Event & operator = (Event & right);
    void set();
    void setName(const char* name);
    const char* getName() const;
    void setTime(const Time & time);
    Time getTime() const;
    bool isValid() const;
    long getSeconds() const;
    void print() const;

    static int eventsGenerated();

    private:
    Time time;
    char* name;
    static int count;
    };


    #endif

    //Time class

    class Time
    {
    public:
    // default constructor
    Time();
    Time(int hr, int min, int sec = 0);
    // copy constructor
    Time(const Time & time);
    // assignment operator
    Time & operator=(Time & time);
    // destructor
    ~Time();
    void print() const;
    bool set();
    bool isValid() const;
    bool istime(int hour, int minute, int second) const;
    void setHour(int hour);
    int getHour() const;
    void setMinute(int minute);
    int getMinute() const;
    void setSecond(int second);
    int getSecond() const;

    private:
    inline bool isleap(int year) const; // utility function
    int hour, minute, second;
    };


    #endif

    The event default constructor sets time to default values and name to an empty string. I have to use class composition.

    I dont have a problem with seeting name to an empty string, i'm having problems setting time to default values. I know how to do it using inheritance, but not with class composition.

    This is what i have done, but i keep on getting compile errors

    //testdriver.cpp

    Event::Event() : Time(hour, minute, second)
    {
    for (int i =0; i < 100; i++)
    {
    name[i] = NULL;
    }
    }

    any suggestions?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Or does anybody know where i can find good notes/tutorials on class composition

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Maybe you haven't posted all your code, but your default Event constructor doesn't do a thing.
    Time default values should be provided in the Time constructor. You may have to #include whatever library Time is in, in Event class.
    If you use composition, you must make sure the libraries including those other classes are #included, and that appropiate constructors are called, including default.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    First, 'name' is declared as a char *, is memory ever allocated for it? If it not allocated, when you do this:
    name[i] = NULL;
    You are setting memory to NULL at what ever trash value 'name' had in it when it was declared.

    Since 'name' is a char *, you only need to do this:
    Code:
    name = NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM