Constructors

This is a discussion on Constructors within the C++ Programming forums, part of the General Programming Boards category; I am writing a time class. I have set the default constructor using member initialization list to set the values ...

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

    Constructors

    I am writing a time class. I have set the default constructor using member initialization list to set the values

    // Time .h

    Time(int hour = 0, int minute = 0, int second = 0);

    // Time.cpp

    Time::Time(int hour, int minute, int second)
    : hour(hour), minute(minute), second(second)
    {
    cout << "Constructor Time (default ) "
    << (hour < 12 ? "0" : "") << hour << ":"
    << (minute < 10 ? "0" : "") << minute << ":"
    << (second < 10 ? "0" : "") << second << endl;
    }
    The default consttructor works. Now, I have to make a constructor with three arguments, representing the time components, hour, minute, second.
    I did it this way....

    //Time.h

    Time(int hour, int minute, int second);

    //Time.cpp

    Time::Time(int hour, int minute, int second)
    {
    cout << "Testing the 3 argument constructor" << endl;
    }

    When i compile i get this error
    error C2535: '__thiscall Time::Time(int,int,int)' : member function already defined or declared

    Anybody have any ideas???

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    What do you need that second constructor for ?

    You already HAVE a contructor that takes up to three integers...
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    The assignment calls for ....

    1. Default constructor.Set time components to 00:00:00. Use member intialization list to set values.

    2. Constructor with 3 args, representing time components. If no seconds are supplied, use default as 0. It calls a member function called valid to ensure the time components are valid. If invalid values then use defaults(0,0,0)

    3. Copy constructor.

    4. Destructor.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    So where's the default constructor (a constructor that takes no arguments)?

  5. #5
    Unregistered
    Guest
    //Time.h
    class Time
    {
    public:
    Time();//declaration default constructor
    Time(int _hour = 0, int _min = 0, int _sec = 0);//declaration 3 parameter constructor with defaults to zero for all three parameters
    //copy constructor
    //destructor
    bool valid(int, int, int);
    int hour;
    int min;
    int sec;
    };

    //Time.cpp
    //default constructor setting variables using initialization list
    Time::Time():
    hour(0),
    min(0),
    sec(0)
    {}

    //three parameter constructor
    Time::Time(int _hour, int _min, int _sec = 0)
    {
    //if invalid input
    if(!valid(_hour, _min, _sec))
    {
    hour = min = sec = 0;
    }
    else if(valid(_hour, _min, _sec)
    {
    hour = _hour;
    min = _min;
    sec = _sec);
    }
    }

    //etc.

    //driver program
    #include <iostream.h>
    #include "Time.h"

    int main()
    {
    Time time1;//uses default constructor
    Time time2(12, 43);//uses default in 3 parameter counstructor
    Time time3(1, 2, 3);//three parameter constructor
    Time time4(2, 5, 89);//three parameter costructor with invalid input

    cout << time1.hour << ' ' << time1.min << ' ' << time1.sec <<end;

    cout << time2.hour << ' ' << time2.min << ' ' << time2.sec << endl;

    cout << time3.hour << ' ' << time3.min << ' ' << time3.sec << endl;

    cout << time4.hour << ' ' << time4.min << ' ' << time4.sec << endl;

    return 0;
    }

    output from above, if class completed correctly
    0 0 0
    12 43 0
    1 2 3
    0 0 0

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    I've been trying the suggestions from people on this board and i'm still having troubles...

    //Time.H

    #ifndef __TIME_H__
    #define __TIME_H__

    class Time
    {
    private:
    int minute;
    int hour;
    int second;
    char ch;

    public:
    Time(int hour = 0, int minute= 0, int second = 0);
    Time(int hour, int minute, int second = 0);
    Time(Time & copy);
    ~Time();
    bool setTime();
    bool istime(int hour, int minute, int second);
    void print() const;
    bool valid(int, int, int);

    };

    #endif

    //Time.cpp

    #include <iostream>
    #include <iomanip>
    #include "Time.h"

    using namespace std;

    Time::Time(int hour, int minute, int second)
    : hour(hour), minute(minute), second(second)
    {
    cout << "Constructor Time (default ) ";
    print();
    }
    Time::Time(int hour, int minute, int second = 0)
    {
    if (!valid(hour, minute, second))
    {
    hour = 0;
    minute = 0;
    second = 0;
    }
    else if (valid(hour, minute, second))
    {
    hour = hour;
    minute = minute;
    second = second;
    }
    }
    Time::Time(Time & copy)
    {
    Time(copy.hour, copy.minute, copy.second);
    cout << "Copy Constructor Time";
    print();
    }
    Time::~Time()
    {
    cout << "Destructor Time: ";
    print();
    }
    bool Time::setTime()
    {
    cout << "Enter a time of the appointment (HH:MM:SS) ===> ";
    cin >> hour >> ch >> minute >> ch >> second;

    if ( !cin.good() )
    {
    cout << "Error: Invalid data!\n";
    cin.clear();
    cin.ignore(1024, '\n');
    return false;
    }
    else
    return true;
    }
    void Time:rint() const
    {
    cout << (hour < 12 ? "0" : "") << hour << ":"
    << (minute < 10 ? "0" : "") << minute << ":"
    << (second < 10 ? "0" : "") << second << endl;

    }
    bool Time::istime(int hour, int minute, int second)
    {
    if (hour > 23 || hour < 0)
    return false;
    if (minute > 59 || minute < 0)
    return false;
    if (second > 59 || second < 0)
    return false;
    return true;
    }
    bool Time::valid(int hour, int minute, int second)
    {
    istime(hour, minute, second);
    if ( istime(hour, minute, second) == 0 )
    {
    cout << "Error: thats invalid time!\n";
    cin.clear();
    cin.ignore(1024, '\n');

    return false;
    }
    else
    return true;
    }
    int main()
    {
    Time ttime;

    ttime.setTime();
    ttime.print();

    return 0;
    }

    This is the error message:




    : error C2572: 'Time::Time' : redefinition of default parameter : parameter 3
    : see declaration of 'Time::Time'
    error C2535: '__thiscall Time::Time(int,int,int)' : member function already defined or declared
    : see declaration of 'Time::Time'
    : error C2548: 'Time::Time' : missing default parameter for parameter 3
    : error C2572: 'Time::Time' : redefinition of default parameter : parameter 3
    : see declaration of 'Time::Time'
    : error C2084: function '__thiscall Time::Time(int,int,int)' already has a body
    : error C2264: 'Time::Time' : error in function definition or declaration; function not called
    : error C2264: 'Time::Time' : error in function definition or declaration; function not called

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I've been trying the suggestions from people on this board and i'm still having troubles...

    No, you haven't. A default constructor has no (zero - one less than one) parameters. In your code you do not have a constructor with zero parameters. It looks as though Unregistered has done it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 08:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 07:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 08:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21