// clock.h
#ifndef CLOCK_H_
#define CLOCK_H_
#include <iostream>
using namespace std;
class Clock
{
private: // private data members
int hours;
int minutes;
int seconds;
public:
// constructors
Clock(int seconds);
Clock(int minutes, int seconds);
Clock(int hours, int minutes, int seconds);
Clock(); //default constructor
void incrementTime(int secondsAmount); // increment
the time by given number of seconds
void reset(); // reset the time back to 00:00:00
friend ostream& operator<<(ostream &out,
const Clock &clock); // output the time in AM/PM and military
format
};
#endif
//end of clock.h
// clock.cpp
#include "clock.h"
#include <iomanip>
Clock ::Clock()
{
hours=0;
minutes=0;
seconds=0;
}
Clock ::Clock(int seconds)
{
this->seconds = seconds;
minutes=0;
hours=0;
if(this->seconds > 59)
{
minutes =
this->seconds/60;
this->seconds =
this->seconds%60;
}
if(minutes > 59)
{
hours = minutes/60;
minutes = minutes%60;
}
if(hours > 23)
{
hours = hours%24;
}
}
Clock ::Clock(int minutes, int seconds)
{
this->seconds = seconds;
this->minutes=minutes;
hours=0;
if(this->seconds > 59)
{
this->minutes +=
this->seconds/60;
this->seconds =
this->seconds%60;
}
if(this->minutes > 59)
{
hours = this->minutes/60;
this->minutes =
this->minutes%60;
}
if(hours > 23)
{
hours = hours%24;
}
}
Clock::Clock(int hours, int minutes, int seconds)
{
this->seconds = seconds;
this->minutes = minutes;
this->hours = hours;
if(this->seconds > 59)
{
this->minutes +=
this->seconds/60;
this->seconds =
this->seconds%60;
}
if(this->minutes > 59)
{
this->hours +=
this->minutes/60;
this->minutes =
this->minutes%60;
}
if(this->hours > 23)
{
this->hours =
this->hours%24;
}
}
void Clock::incrementTime(int secondsAmount)
{
this->seconds += secondsAmount;
if(this->seconds > 59)
{
this->minutes +=
this->seconds/60;
this->seconds =
this->seconds%60;
}
if(this->minutes > 59)
{
this->hours +=
this->minutes/60;
this->minutes =
this->minutes%60;
}
if(this->hours > 23)
{
this->hours =
this->hours%24;
}
}
void Clock::reset()
{
hours=0;
minutes=0;
seconds =0;
}
ostream& operator<<(ostream &out, const Clock
&clock)
{
out<<" Time in AM/PM format : ";
if(clock.hours >=0 && clock.hours
<=11)
{
out<<setfill('0') <<
setw(2)<<clock.hours<<":"<<setfill('0') <<
setw(2)<<clock.minutes<<":"<<setfill('0')
<< setw(2)<<clock.seconds<<"
AM"<<endl;
}else if(clock.hours == 12)
{
out<<setfill('0') <<
setw(2)<<clock.hours<<":"<<setfill('0') <<
setw(2)<<clock.minutes<<":"<<setfill('0')
<< setw(2)<<clock.seconds<<"
PM"<<endl;
}
else
{
int hour = clock.hours-12;
out<<setfill('0') <<
setw(2)<<hour<<":"<<setfill('0') <<
setw(2)<<clock.minutes<<":"<<setfill('0')
<< setw(2)<<clock.seconds<<"
PM"<<endl;
}
out<<" Time in military format : "<<setfill('0') << setw(2)<<clock.hours<<":"<<setfill('0') << setw(2)<<clock.minutes<<":"<<setfill('0') << setw(2)<<clock.seconds<<endl;
return out;
}
//end of clock.cpp
// main.cpp : C++ program to create and test class ClockWork
#include <iostream>
#include "clock.h"
using namespace std;
int main() {
Clock clock(17,35,59);
cout<<clock<<endl;
clock.incrementTime(121);
cout<<clock<<endl;
clock.reset();
cout<<clock<<endl;
return 0;
}
//end of main.cpp
Output:
3. write a c++ program: Design and implement a class called Clock that describes the time...
Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...
IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute. b) Incrementing...
IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute. b) Incrementing...
Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...
JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....
Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...
In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....
The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private. Write a class called BankAccount that has: a string data member called customerName, a string data member called customerID, and a double data member called customerBalance a constructor that takes two strings and a double (name, ID, balance) and uses them...
Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...
In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...