Having trouble with class constructors printing - c++

I'm trying to learn how classes and their constructors work by creating a Time class that includes an hour, minute, and second. I wanted to print one time by using the default constructor and one through user input. While my program compiles, it does not ask for user input, most likely because of the way I call the class function getHour (if I were to only print the input hour). I am also unsure of how to print the time (0,0,0) through the default constructor.
Any help would be appreciated!
Main:
#include <iostream>
#include "Time.h"
int main(){
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time time1(hour, minute, second);
std::cout << time1.getHour() << std::endl;
return 0;
}
Class implementation:
#include <iostream>
#include "Time.h"
//default constructor
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
//construct from hour, minute, second
Time::Time(int theHour, int theMinute, int theSecond) {
hour = theHour;
minute = theMinute;
second = theSecond;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}

Works fine for me, this was my output:
Enter the hour, minute, and second:
2
45
32
2
Press any key to continue . . .
Make sure you are rebuilding the code and running the new executable.
In the constructor you can just do:
Time::Time() {
hour = 0;
minute = 0;
second = 0;
std::cout << hour << " " << minute << " " << second << std::endl;
}
this will be called anytime you call Time with the default constructor:
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time t; //<--- this prints 0,0,0
Time time1(hour, minute, second);
std::cout << time1.getHour() << std::endl;
system("pause");
return 0;
will result in:
Enter the hour, minute, and second:
11
19
59
0 0 0
11
Press any key to continue . . .

I am also unsure of how to print the time (0,0,0) through the default constructor.
Unless I missing something subtle,
// Construct a Time object using the default constructor.
Time time2;
// Print it's hour
std::cout << time2.getHour() << std::endl;

The code you shared doesn't try to print an object constructed with the default constructor since getHour() is called by time1. The is doing what you tell it to do. If you want the full time from time1you'll have to call all of the getters eg: std::cout << time1.getHour() << " : " << time1.getMinute() << " : " << time1.getSecond() << std::endl

"I am also unsure of how to print the time (0,0,0) through the default constructor."
consider a displayTime() method that can be called on to display the time of any instance
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time time1(hour, minute, second); // overloaded constructor
Time time2; // default constructor
// std::cout << time1.getHour() << std::endl;
// std::cout << time2.getHour() << std::endl;
time1.displayTime();
time2.displayTime();
getch();
return 0;
}
add displayTime() method
void Time::displayTime() const{
std::cout << this->hour << ","
<< this->minute << ","
<< this->second << std::endl;
}

Related

C++ Queues:How to loop to display the correct start time and wait time of a Car Wash

For this car wash simulation, your program reads in the car arrival time through an input file. The total wash time for a car is 3 minutes. Another car can not go into the wash while a car is being washed which will increase the waiting time. If a car departs at minute 3 the next car needs to go in at minute 4 if it has already arrived.
I have already tried reading in the file all at once and then creating another loop but that has not worked. I have tried many things, I think I am only having a problem with how to loop the program.
#include <iostream>
#include <cassert>
#include <fstream>
#include <queue>
#include <cstdlib>
using namespace std;
class averager {
private:
int cnt;
int sum;
public:
averager(){
cnt=0;
sum=0;
}
void plus_next_number(int value)
{
cnt++;
sum+=value;
}
double average_time()
{
assert(cnt>0);
return (sum/cnt);
}
int how_many_cars()
{
return cnt;
}
};
class Washmachine {
private:
int time_for_wash;
int time_left;
public:
Washmachine(int n) {
time_for_wash = n;
time_left = 0;
}
bool is_busy() {
return (time_left > 0);
}
void startWashing() {
if(!is_busy()) {
time_left = time_for_wash;
}
}
void one_second(){
if(is_busy()) {
--time_left;
}
}
};
int main() {
queue<int> waitQueue;
int carArrival;
averager cal;
ifstream infile;
ofstream arrivalrec;
arrivalrec.open("arrival_time.txt");
arrivalrec << "Car Number " << "Arrival Time " << "Car Wash Start Time " << "Departure Time "
<< "Wait Time "
<< "Total Time " << endl
<< endl;
int maxWaitTime; // maxWaitTime initially 0:00
int totalWaitTime; // total time customers wait
int endTime = 540; // times for the simulation
int totalServiceTime;
int startTime;
int carNum = 0; // number of cars washed in study
int washTime = 3; // fixed time for a wash in minutes
int DeptTime;
int TotalTime;
int timeleft=0;
int waitTime;
int temp;
int sw;
Washmachine carwashing(washTime);
infile.open("input.txt");
for (int startTime=0;startTime<=endTime;startTime++){
infile>>temp;
waitQueue.push(temp);
if((!carwashing.is_busy())&&(!waitQueue.empty())) {
carArrival=waitQueue.front();
waitQueue.pop();
waitTime=temp-carArrival;
cal.plus_next_number(temp-carArrival);
carwashing.startWashing();
}
carwashing.one_second();
if (maxWaitTime<waitTime)
maxWaitTime=waitTime;
// add waiting time for customer to totalWaitTime.
totalWaitTime+=waitTime;
totalServiceTime+=washTime;
startTime=temp+waitTime;
TotalTime=washTime+waitTime;
DeptTime=startTime +washTime;
// increment the number of customers served
carNum++;
// set washAvailable to false since equipment back in service
// output the summary data for the simulation include number of cars
// washed, average customer waiting time and pct of time wash operates
arrivalrec << carNum << " " << temp << " " <<startTime
<< " " << DeptTime << " " <<
waitTime << " " << TotalTime << endl
<< endl << endl;
}
arrivalrec << "Maximum customer waiting time for a car wash is "
<< "14 minutes" << endl;
arrivalrec << "Percentage of time car wash operates is 57 "
//<< ((totalServiceTime / endTime) * 100.0)
<< '%' << endl;
arrivalrec << "Number of customers remaining at " << endTime
<< " is 8"<<endl; //<< waitQueue.size() << endl;
arrivalrec<<"\nCars washed were: "<<carNum<<endl;
arrivalrec<<"\nThe average waiting time is: "<<cal.average_time()<<endl;
int car_denied=0;
while(!waitQueue.empty())
{
waitQueue.pop();
car_denied++;
}
arrivalrec<<"\nThe number of denied cars is: 2 "<<endl;
arrivalrec<<endl;
return 0;
}
Car Arrival 0 car start 0 car depart 3 wait time 0 total time 3
3 4 7 1 4
10 10 13 0 3
11 14 17 3 6
Please try the following loop for the main function of your Car washing simulation.
Instead of looping over startTime, the loop uses the simulation runTime. All events like putting a car to the queue, starting and documenting the car washing process as well as counting waitTime are done by conditions:
infile.open("input.txt");
infile >> temp;
carNum = 1;
for (runTime=1;runTime<=endTime;runTime++){
if (runTime == temp) {
waitQueue.push(temp);
infile >> temp;
}
if((!carwashing.is_busy())&&(!waitQueue.empty())) {
carArrival=waitQueue.front();
waitQueue.pop();
startTime = runTime;
waitTime=startTime-carArrival;
totalWaitTime = waitTime;
TotalTime = washTime + waitTime;
cal.plus_next_number(startTime-carArrival);
carwashing.startWashing();
}
else
{
waitTime++;
}
if (carwashing.is_busy())
carwashing.one_second();
if ((!carwashing.is_busy())&&(startTime >= DeptTime)) {
DeptTime = startTime + washTime;
totalServiceTime += washTime;
arrivalrec << carNum << " " << carArrival << " " << startTime
<< " " << DeptTime << " " <<
totalWaitTime << " " << TotalTime << endl
<< endl << endl;
carNum++;
}
}
Please note that the file reading of the first car is done outside of the loop.
I also added the runTime variable and some initialization to your declaration:
queue<int> waitQueue;
int carArrival = 0;
averager cal;
ifstream infile;
ofstream arrivalrec;
arrivalrec.open("arrival_time.txt");
arrivalrec << "Car Number " << "Arrival Time " << "Car Wash Start Time " << "Departure Time "
<< "Wait Time "
<< "Total Time " << endl
<< endl;
int maxWaitTime = 0; // maxWaitTime initially 0:00
int totalWaitTime = 0; // total time customers wait
int endTime = 75; // times for the simulation
int totalServiceTime = 0;
int startTime = 0;
int carNum = 0; // number of cars washed in study
int washTime = 3; // fixed time for a wash in minutes
int DeptTime = 0;
int TotalTime = 0;
int timeleft=0;
int waitTime=0;
int temp;
int sw;
int runTime;
Washmachine carwashing(washTime);
I've taken the desired output from your other post:
Hope it helps you?

How do I convert from seconds to minutes using this library?

I have a program that uses this popular library, however I am struggling to use it to convert from seconds to minutes
The following code...
#include <iostream>
#include "units.h"
int main(int argc, const char * argv[])
{
{
long double one = 1.0;
units::time::second_t seconds;
units::time::minute_t minutes(one);
seconds = minutes;
std::cout << "1 minute is " << seconds << std::endl;
}
{
long double one = 1.0;
units::time::second_t seconds(one);
units::time::minute_t minutes;
minutes = seconds;
std::cout << "1 second is " << minutes << std::endl;
}
return 0;
}
produces...
1 minute is 60 s
1 second is 1 s
however, I would have expected it to produce...
1 minute is 60 s
1 second is .016666667 m
The library offers a units::convert method, check the doc here.
Here's a working snippet:
long double one = 1.0;
units::time::second_t seconds(one);
units::time::minute_t minutes;
minutes = seconds;
std::cout << "1 second is " << minutes << std::endl;
std::cout << "1 second is "
<< units::convert<units::time::seconds, units::time::minutes>(seconds)
<< std::endl;
For more, I suggest searching in the doc.
I don't know the library you are using, but C++11 added the std::chrono::duration class that seems to be able to do what you want:
#include <chrono>
#include <iostream>
int main()
{
{
std::chrono::minutes minutes(1);
std::chrono::seconds seconds;
seconds = minutes;
std::cout << "1 minute is " << seconds.count() << std::endl;
}
{
std::chrono::seconds seconds(1);
using fMinutes = std::chrono::duration<float, std::chrono::minutes::period>;
fMinutes minutes = seconds;
std::cout << "1 second is " << minutes.count() << std::endl;
}
return 0;
}
Note that the default std::chrono::minutes uses an integer counter, and thus reports that 1 second is 0 minutes. That is why I define my own float-minutes.
In any case, the above program produces the following output:
1 minute is 60
1 second is 0.0166667

How would you go about making this if else if program much more simplistic and less code redundant (C++)?

Write a program that asks for the names of three runners and the time it took each
of them to finish a race. The program should display who came in first, second, and
third place.
Input Validation: Only accept positive numbers for the times.
My Code
#include <iostream>
#include <string>
using namespace std;
int main()
{
string runner1, runner2, runner3;
int time1, time2, time3;
cout << "Please enter the names of three runners" << endl;
cin >> runner1 >> runner2 >> runner3;
cout << "How many minutes did it take " << runner1 << " to finish the race?" << endl;
cin >> time1;
cout << "How many minutes did it take " << runner2 << " to finish the race?" << endl;
cin >> time2;
cout << "How many minutes did it take " << runner3 << " to finish the race?" << endl;
cin >> time3;
if (time1 < time2 && time1 < time3)
{
cout << runner1 << " is 1st place!" << endl;
if (time2 < time3)
{
cout << runner2 << " is 2nd place!" << endl;
cout << runner3 << " is 3rd place!" << endl;
}
else if (time3 < time2)
{
cout << runner3 << " is 2nd place!" << endl;
cout << runner2 << " is 3rd place!" << endl;
}
}
else if (time2 < time1 && time2 < time3)
{
cout << runner2 << " is 1st place!" << endl;
if (time1 < time3)
{
cout << runner1 << " is 2nd place!" << endl;
cout << runner3 << " is 3rd place!" << endl;
}
else if (time3 < time1)
{
cout << runner3 << " is 2nd place!" << endl;
cout << runner2 << " is 3rd place!" << endl;
}
}
else if (time3 < time2 && time3 < time1)
{
cout << runner3 << " is 1st Place!" << endl;
if (time2 < time1)
{
cout << runner2 << " is 2nd place!" << endl;
cout << runner1 << " is 3rd place!" << endl;
}
else if (time1 < time2)
{
cout << runner1 << " is 2nd place!" << endl;
cout << runner2 << " is 3rd place!" << endl;
}
}
else
{
cout << "Error! Please restart the program and input a positive value" << endl;
}
return 0;
}
Keep the runners ordered in an array by their time, iterate over the runners array and output (name + i + " place")
I think a linked list would be really good, but it might lose to a raw array if the runners don't change place very often or if there aren't many runners.
"Separate the drawing code from the game logic"
Your drawing code is cout calls. Your game logic is determining what place each runner is in. Then you draw based on that state that you calculated.
Your way is the most direct and fastest way to solve the problem of printing out runner places though.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
struct Runner {
int time;
int id;
int operator<(Runner runner) {
return time < runner.time;
}
};
char* pickSuffix(int place) {
switch (place) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}
int main() {
//make my runners, read in my times, runner 1 first
Runner runners[3];
for (int i = 0; i < 3; i++) {
cout << "Enter runner " << i+1 << "'s time: ";
cin >> runners[i].time;
cout << endl;
runners[i].id = i+1; //setup their id, used like a name
}
//figure out what place each runner came in
//must overload operator< for object to use sort
sort(runners, &runners[2]);
//the position of the runner in the array is the place they came in
//since it is sorted by their times
for (int i = 0; i < 3; i++) {
cout << "Runner #" << runners[i].id << " came in " << i+1 << pickSuffix(i+1) << " place!" << endl;
cout << runners[i].time << endl;
}
system("pause");
return 0;
}
Here is a sample of the code written the way I described. I think for most purposes you wouldn't want to sort the runners in the array though, you would add a member variable place and just store their calculated position.
It looks like you're beginning your journey in c++ programming. Welcome.
I'd seek to break the problem down into its component parts, so that we can end up expressing the intent of the solution in an easy-to-understand form:
int solve(std::istream& is, std::ostream& os) {
Runners runners;
collect_runners(os, is, runners);
sort_by_time(runners);
print_first_3(os, runners);
return 0;
}
Note that I have not tied the solution to any particular input or output stream. That's so I can test it easily later.
Now that we have expressed the intent of the solution cleanly, we fill in the blanks (I have used various 'advanced' techniques that I hope you will find instructive and interesting).
I have used c++11.
Comments inline.
#include <string>
#include <vector>
#include <algorithm>
#include <type_traits>
#include <iostream>
#include <sstream>
/** The concept of coupling a runner's name with the time it took to run the race
*
*/
struct Runner {
std::string name;
int time;
};
/** A collection of runners and their corresponding time.
*
*/
using Runners = std::vector<Runner>;
/** Sort a container with a predicate. Return a reference to the container
*
* #tparam Container
* #tparam Pred
* #param c
* #param pred
* #return
*/
template<class Container, class Pred>
auto sort_container(Container &c, Pred &&pred) -> Container & {
std::sort(std::begin(c), std::end(c), std::forward<Pred>(pred));
return c;
}
/** Sort a Runners array by ascending time
*
* #param vec
* #return
* */
Runners &sort_by_time(Runners &vec) {
auto by_increasing_time = [](Runner const& l, Runner const& r) {
return l.time < r.time;
};
sort_container(vec, by_increasing_time);
return vec;
}
/** Print the first 3 runnes in an array of runners to an ostream
*
* #param os
* #param vec
*/
void print_first_3(std::ostream& os, Runners const &vec) {
static const char *nth[] = {
"first",
"second",
"third"
};
auto limit = std::extent<decltype(nth)>::value;
limit = std::min(limit, vec.size());
for (std::size_t i = 0; i < limit; ++i) {
auto const &runner = vec[i];
os << runner.name << " is in " << nth[i] << " place with a time of " << runner.time << "\n";
}
}
/** Ask a question on the console if the answer is to come from stdin
*
* #param is
* #param q
*/
template<class Target>
void question(std::istream& is, Target& target, std::string const& q)
{
if (std::addressof(is) == static_cast<std::istream*>(std::addressof(std::cin)))
{
std::cout << q << std::endl;
}
is >> target;
}
/** Build a runner using the supplied input and output streams
*
* #param os
* #param is
* #return
*/
Runner collect_runner(std::ostream& os, std::istream& is)
{
Runner runner {};
question(is, runner.name, "runner's name?");
question(is, runner.time, "runner's time?");
return runner;
}
/** Populate a Runners array using input and output streams
*
* #param os
* #param is
* #param runners
* #return
*/
Runners& collect_runners(std::ostream& os, std::istream& is, Runners& runners)
{
int nrunners = 0;
question(is, nrunners, "how many runners?");
while (nrunners-- > 0) {
runners.push_back(collect_runner(os, is));
}
return runners;
}
/** Solve the problem at hand
*
* #param is
* #param os
* #return
*/
int solve(std::istream& is, std::ostream& os) {
Runners runners;
collect_runners(os, is, runners);
sort_by_time(runners);
print_first_3(os, runners);
return 0;
}
/** Solve the problem using either std input/output or test input
*
* #param argc
* #param argv
* #note If argc == 2 and argv[1] == "test" then run solve using test input.
* We do this so that we can test our code without playing around with external files or
* the console each time we want to test it
* #return
*/
int main(int argc, char **argv) {
if (argc == 2 && argv[1] == std::string("test")) {
static const char test_data[] = R"__(
5
bob 40
bill 20
sue 30
peter 25
zool 29
)__";
std::istringstream test_stream{test_data};
return solve(test_stream, std::cout);
} else {
return solve(std::cin, std::cout);
}
}
test on the console with:
$ ./a.out test
expected output:
bill is in first place with a time of 20
peter is in second place with a time of 25
zool is in third place with a time of 29
I may have laughed a little at the complexity of Richard's answer.
He makes excellent points about organizing code, of course.
I end up posting my own review and a "simpler" sample, because I think the most important side of a program is functionality.
In this case,
you need error handling (the user may enter illegal values)
you need to check input (the user may enter more than 3 names, may enter identical names etc)
you need to properly report ranks. If two runners had the same time, you will rank them in "arbitrary" order. You will want to correctly rate them as shared places.
My code
is less tied to 3 runners (only when reading the three names, because that's what the sample required)
exemplifies the use of partial_sort to get just the first 3 positions sorted That is no longer possible since the display function handles collections of unknown size and sharing places implies there maybe more than 3 ranked runners.
Live On Coliru
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
void static inline ignore_rest_of_line() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
struct Runner {
std::string name;
int time = 0;
bool operator<(Runner const& o) const { return time < o.time; }
};
std::vector<Runner> read_three() {
std::string name1, name2, name3;
auto unique = [&] { return std::set<std::string>{ name1, name2, name3 }.size() == 3; };
std::cout << "Please enter the names of three runners\n";
while (std::cin) {
if (std::cin >> name1 >> name2 >> name3 && unique())
break;
std::cout << "Enter 3 unique names\n";
ignore_rest_of_line();
}
ignore_rest_of_line();
return { { name1, 0 }, { name2, 0 }, { name3, 0 } };
}
void read_time(Runner& runner) {
while (std::cin) {
std::cout << "How many minutes did it take " << runner.name << " to finish the race?\n";
if ((std::cin >> runner.time) && runner.time > 0)
break;
std::cout << "Enter a valid time\n";
std::cin.clear();
ignore_rest_of_line();
}
ignore_rest_of_line();
}
template <typename List>
void display_winners(List runners) {
std::sort(std::begin(runners), std::end(runners));
std::string ranks[] = { "1st", "2nd", "3rd" };
auto prev = runners.begin();
auto rank = std::begin(ranks);
for (auto& runner : runners) {
if (runner.time != prev->time)
++rank;
if (rank == std::end(ranks))
break;
std::cout << runner.name << " is " << *rank << " place\n";
}
}
int main() {
std::cin.exceptions(std::ios::eofbit);
auto runners = read_three();
for (auto& runner : runners) {
read_time(runner);
}
display_winners(runners);
}
The sample run shows
Please enter the names of three runners
a a b
Enter 3 unique names
a b c
How many minutes did it take a to finish the race?
9
How many minutes did it take b to finish the race?
invalid time 7
Enter a valid time
How many minutes did it take b to finish the race?
8
How many minutes did it take c to finish the race?
8
c is 1st place
b is 1st place
a is 2nd place

Convert time into minutes and seconds

I made a code to convert time into minutes and seconds using constructor. In output I'm getting time in seconds but its not displaying time in minutes and can't figured out my mistake so anyone can help me out...
#include<iostream>
using namespace std;
class Time
{
public:
Time(int);
Time(float);
};
Time::Time(int t)
{
cout << "Time in seconds: " << t*60*60 << "\n";
}
Time::Time(float t)
{
cout << "Time in Minutes: " << int(t*60) << "\n";
}
int main()
{
int hr;
cout << "Enter the time in Hours: ";
cin >> hr;
Time t1(hr);
Time t2(float(hr));
}
You aren't typecasting correctly. You need to typecast by putting the desired type in parentheses, not by trying to use it as a function. For example, you'd need to do this:
Time t2((float) hr);
cout << "Time in Minutes: " << (int) t*60 << "\n";
While Time t2(float(hr)); is legal, it's a function prototype because this is a vexing parse. It is equivalent to Time t2(float hr);, which means that t2 is a function that takes a float called "hr" and returns a Time. This has no effect since such a function is never implemented or called.
If this seems absurd, here's a way to think about it that can help. Consider this code:
Time t2 (float (*hr));
Clearly this is a perfectly normal way to declare t2 as a function that takes a pointer to a float called "hr". Well, shouldn't it also be legal without the *? And the C++ grammar rule is that if something can be a prototype, it is.
Should works :
Time t2((float)hr);
Time::Time(int hour){
cout << "Time in seconds: " << hour*60*60 << "\n";
cout << "Time in minutes: " << hour*60 << "\n";
}
try adding
Time::Time(float t)
{
cout << "Time in Minutes: " << float(t*60) << "\n";
}
tell me if it works..

Problems with Add method in TimeUnit class

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class TimeUnit
{
public:
TimeUnit(int m, int s)
{
this -> minutes = m;
this -> seconds = s;
}
string ToString()
{
ostringstream o;
o << minutes << " minutes and " << seconds << " seconds." << endl;
return o.str();
}
void Simplify()
{
if (seconds >= 60)
{
minutes += seconds / 60;
seconds %= 60;
}
}
TimeUnit Add(TimeUnit t2)
{
TimeUnit t3;
t3.seconds = seconds + t2.seconds;
if(t3.seconds >= 60)
{
t2.minutes += 1;
t3.seconds -= 60;
}
t3.minutes = minutes + t2.minutes;
return t3;
}
private:
int minutes;
int seconds;
};
int main(){
cout << "Hello World!" << endl;
TimeUnit t1(2,30);
cout << "Time1:" << t1.ToString() << endl;
TimeUnit t2(3,119);
cout << "Time2:" << t2.ToString();
t2.Simplify();
cout << " simplified: " << t2.ToString() << endl;
cout << "Added: " << t1.Add(t2).ToString() << endl;
//cout << " t1 + t2: " << (t1 + t2).ToString() << endl;
/*cout << "Postfix increment: " << (t2++).ToString() << endl;
cout << "After Postfix increment: " << t2.ToString() << endl;
++t2;
cout << "Prefix increment: " << t2.ToString() << endl;*/
}
I'm having problems with my Add method. Xcode is giving me this error: "No matching constructor for initialization of TimeUnit"
Could someone please tell me what I am doing wrong? I've literally tried everything that I know how to do, but I can't even get it to compile with this method.
Here are the instructions from my professor:
The TimeUnit class should be able to hold a time consisting of Minutes
and Seconds. It should have the following methods:
A constructor that takes a Minute and Second as parameters ToString()
- Should return the string equivilant of the time. "M minutes S seconds." Test1 Simplify() - This method should take the time and
simplify it. If the seconds is 60 seconds or over, it should reduce
the seconds down to below 60 and increase the minutes. For example, 2
Min 121 seconds should become 4 minutes 1 second. Test2 Add(t2) -
Should return a new time that is the simplified addition of the two
times Test3 operator + should do the same thing as Add Test4 pre and
postfix ++: should increase the time by 1 second and simplify Test5
In your TimeUnit::Add function, you tried to initialize t3 with default constructor. However, your TimeUnit doesn't have one:
TimeUnit Add(TimeUnit t2)
{
TimeUnit t3; ///<<<---- here
///.....
}
Try update TimeUnit::Add to this way:
TimeUnit Add(const TimeUnit& t2)
{
return TimeUnit(this->minutes+t2.minutes, this->seconds+t2.seconds);
}
The specific problem is because there is no TimeUnit::TimeUnit() defined, only TimeUnit(const int &m, const int &s).