Question1:
Modify your assignment-3, add separator mutator for each field and validate its value in mutators. The setTime validates time by using individual mutators of fields. Add no-argument constructor and three argument constructor. There argument constructor validates fields by using individual mutator of each field. Write new application to use your class and all of its member functions.
Hint:
You will add three functions which are:
void set_hr(int h);//to set hr then validate it.
void set_min(int m);//to set min then validate it.
void set_sec(int s);//to set sec then validate it.
And in the functions SetTime and the overloaded constructor you will call the previous three functions.
Question2:
Modify the definition of class Money show in display 11.8 so that all of the following are added:
A). the operator <, <=, > and >= have each been overloaded to apply to the type money and use these overloaded operator for the comparison of Money objects in your application.

And here is the code for assignment 3.

#include <iostream>
#include <string>
using namespace std;

class ClockType
{
public:
	ClockType();
	//Default constructor
	ClockType(int the_hr, int the_min, int the_sec);
	//3-parameter contructor
	void setTime(int the_hr, int the_min, int the_sec);
	//Takes in the time(hour, minute, second-of type int)entered by 	the user and validates it.
	void printTime();
	//Displays the time on the screen.
	void incrementSeconds();
	//This function is used for incrementing the variable for second 	by 1.
	void incrementMinutes();
	//This function is used for incrementing the variable for minute 	by 1.
	void incrementHours();
	//This function is used for incrementing the variable for hour by 	1.
private:
	int hr;
	int min;
	int sec;
};

int main()
{
	ClockType current_time, increment_time(12,12,12);
	int hours, minutes, seconds, no_hr, no_min, no_sec;

	cout << "This program is used to calculate the incremented 				time\n" << "after the user has current time and the numbers 		to increment the time with.\n\n\n";
	cout << "Please enter the current time:\n\n";
	cout << "hours : ";
	cin >> hours;
	cout << "minutes : ";
	cin >> minutes;
	cout << "seconds : ";
	cin >> seconds;

	current_time.setTime(hours, minutes, seconds);
	increment_time.setTime(hours, minutes, seconds);
	cout << "\n\nCurrent time";
	current_time.printTime();

	cout << "\n\nPlease enter the number of hours, minutes and 				seconds\n" << "for incrementing the current time:\n\n";
	cout << "no. of hours: ";
	cin >> no_hr;
	cout << "no. of minutes: ";
	cin >> no_min;
	cout << "no. of seconds: ";
	cin >> no_sec;

	while(no_sec>0)
	{
		increment_time.incrementSeconds();
		no_sec--;
	}
	while(no_min>0)
	{
		increment_time.incrementMinutes();
		no_min--;
	}
	while(no_hr>0)
	{
		increment_time.incrementHours();
		no_hr--;
	}
	cout << "\n\nIncremented time";
	increment_time.printTime();
	cout << endl;

	return 0;
}
ClockType::ClockType()
{
}

ClockType::ClockType(int the_hr, int the_min, int the_sec)
{
	setHours(the_hr);
	setMinutes(the_min);
	setSeconds(the_sec);
}
void ClockType::setTime(int the_hr, int the_min, int the_sec)
{
	if(the_hr>=0 && the_hr<=23)
		hr = the_hr;
	else
		hr = 0;
	if(the_min>=0 && the_min<=59)
		min = the_min;
	else
		min = 0;
	if(the_sec>=0 && the_sec<=59)
		sec = the_sec;
	else
		sec = 0;
}

void ClockType::printTime()
{
	cout << " in the format:  h:m:s" << endl;
	cout << "   The time is:  " << hr << ":" << min << ":" << sec << endl;
}



void ClockType::incrementSeconds()
{
	sec = sec+1;
	if(sec>59)
	{
		sec = 0;
		incrementMinutes();
	}
}

void ClockType::incrementMinutes()
{
	min = min+1;
	if(min>59)
	{
		min = 0;
		incrementHours();
	}
}

void ClockType::incrementHours()
{
	hr = hr+1;
	if(hr>23)
		hr = 0;
}

** I was absent in this class and have no clue how to start. Please someone explain it to me.

start with this :

>>You will add three functions which are:
void set_hr(int h);//to set hr then validate it.
void set_min(int m);//to set min then validate it.
void set_sec(int s);//to set sec then validate it.

Add these function inside your classes :

void set_hr(int h);//to set hr then validate it.
void set_min(int m);//to set min then validate it.
void set_sec(int s);//to set sec then validate it.

And make sure the input you get is valid. Example you shouldn't have
negative hours as an input.

I actually did that.. I wrote the code and everything. But I got negative numbers.. I have no idea how and why is that.
Can you check my code below please.

#include <iostream>
#include <string>
using namespace std;

class ClockType
{
public:
	ClockType();
	//Default constructor
	ClockType(int the_hr, int the_min, int the_sec);
	//3-parameter contructor


	void set_hr(int h);
	void set_min(int m);
	void set_sec(int s);
	//The 3 new functions.
	
	void setTime(int the_hr, int the_min, int the_sec);
	//Takes in the time(hour, minute, second-of type int)entered by 	the user and validates it.
	void printTime();
	//Displays the time on the screen.
	void incrementSeconds();
	//This function is used for incrementing the variable for second 	by 1.
	void incrementMinutes();
	//This function is used for incrementing the variable for minute 	by 1.
	void incrementHours();
	//This function is used for incrementing the variable for hour by 	1.
private:
	int hr;
	int min;
	int sec;
};

int main()
{
	ClockType current_time, increment_time(12,12,12);
	int hours, minutes, seconds, no_hr, no_min, no_sec;

	cout << "This program is used to calculate the incremented time\n" 
		 << "after the user has current time and the numbers to increment the time with.\n\n\n";
	cout << "Please enter the current time:\n\n";
	cout << "hours : ";
	cin >> hours;
	cout << "minutes : ";
	cin >> minutes;
	cout << "seconds : ";
	cin >> seconds;

	current_time.setTime(hours, minutes, seconds);
	increment_time.setTime(hours, minutes, seconds);
	cout << "\n\nCurrent time";
	current_time.printTime();

	cout << "\n\nPlease enter the number of hours, minutes and seconds\n" 
		<< "for incrementing the current time:\n\n";
	cout << "no. of hours: ";
	cin >> no_hr;
	cout << "no. of minutes: ";
	cin >> no_min;
	cout << "no. of seconds: ";
	cin >> no_sec;

	while(no_sec>0)
	{
		increment_time.incrementSeconds();
		no_sec--;
	}
	while(no_min>0)
	{
		increment_time.incrementMinutes();
		no_min--;
	}
	while(no_hr>0)
	{
		increment_time.incrementHours();
		no_hr--;
	}
	cout << "\n\nIncremented time";
	increment_time.printTime();
	cout << endl;

	return 0;
}
ClockType::ClockType()
{
}

ClockType::ClockType(int the_hr, int the_min, int the_sec)
{
	void set_hr(int h);
	void set_min(int m);
	void set_sec(int s);
}
void ClockType::setTime(int the_hr, int the_min, int the_sec)
{
	void set_hr(int h);
	void set_min(int m);
	void set_sec(int s);	
}

void set_hr(int h)
{
	int the_hr;
if(the_hr>=0 && the_hr<=23)
		h = the_hr;
	else
		h = 0;
}

void set_min(int m)
{
	int the_min;
	if(the_min>=0 && the_min<=59)
		m = the_min;
	else
		m = 0;
}
void set_sec(int s)
{
	int the_sec;
	if(the_sec>=0 && the_sec<=59)
		s = the_sec;
	else
		s = 0;
}

void ClockType::printTime()
{
	cout << " in the format:  h:m:s" << endl;
	cout << "   The time is:  " << hr << ":" << min << ":" << sec << endl;
}



void ClockType::incrementSeconds()
{
	sec = sec+1;
	if(sec>59)
	{
		sec = 0;
		incrementMinutes();
	}
}

void ClockType::incrementMinutes()
{
	min = min+1;
	if(min>59)
	{
		min = 0;
		incrementHours();
	}
}

void ClockType::incrementHours()
{
	hr = hr+1;
	if(hr>23)
		hr = 0;
}

Edited 6 Years Ago by fadia: n/a

This article has been dead for over six months. Start a new discussion instead.