-
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???
-
What do you need that second constructor for ?
You already HAVE a contructor that takes up to three integers...
-
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.
-
So where's the default constructor (a constructor that takes no arguments)?
-
//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
-
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::print() 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
-
>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.