Question

Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current time at your location.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here are the 5 files you will be needing. Let me know for any changes or modifications. We have override the get_hours and get_minutes member functions

///////////////////////////////////////////////////////////////////////

/////////////////////////////// Clock.h///////////////////////////////

///////////////////////////////////////////////////////////////////////

#ifndef CLOCK_H

#define CLOCK_H

#include<string>

using std::string;

class Clock

{

                public:

                                virtual int get_hours();

                                virtual int get_minutes();

                                virtual string get_time();

};

#endif

////////////////////////////////////////////////////////////////////////

/////////////////////////////// Clock.cpp///////////////////////////////

////////////////////////////////////////////////////////////////////////

#include "Clock.h"

#include<string>

#include<ctime>

#include<sstream>

using namespace std;

int Clock::get_hours() {

                time_t current_time = time(0);

                tm* local_time=localtime(&current_time);

                return local_time->tm_hour;

}

int Clock::get_minutes() {

                time_t current_time = time(0);

                tm* local_time=localtime(&current_time);

                return local_time->tm_min;

}

string Clock::get_time() {

                int hours = get_hours();

                int minutes = get_minutes();

                if(hours>12){

                                stringstream ss;

                                ss<<(hours-12)<<":"<<minutes;

                                string time;

                                ss>>time;

                                return time+" PM";

                }else{

                                stringstream ss;

                                ss<<(hours)<<":"<<minutes;

                                string time;

                                ss>>time;

                                return time+" AM";

                }

}

///////////////////////////////////////////////////////////////////////////

/////////////////////////////// WorldClock.h///////////////////////////////

///////////////////////////////////////////////////////////////////////////

#ifndef WORLDCLOCK_H

#define WORLDCLOCK_H

#include "Clock.h"

class WorldClock:public Clock

{

                public:

                                WorldClock(int);

                                int get_hours();

                                int get_minutes();

                private:

                                int ahead;

};

#endif

/////////////////////////////////////////////////////////////////////////////

/////////////////////////////// WorldClock.cpp///////////////////////////////

/////////////////////////////////////////////////////////////////////////////

#include "WorldClock.h"

#include<string>

#include<ctime>

#include<sstream>

using namespace std;

WorldClock::WorldClock(int ahead):Clock(), ahead{ahead}{

}

                int WorldClock::get_hours(){

                                time_t current_time = time(0);

                                tm* local_time=localtime(&current_time);

                                int hours= local_time->tm_hour;

                                int minutes = local_time->tm_min;

                                int total_minutes = hours*60+minutes + ahead*30;

                                total_minutes%=24*60;

                                return total_minutes/60;

                }

                int WorldClock::get_minutes(){

                                time_t current_time = time(0);

                                tm* local_time=localtime(&current_time);

                                int hours= local_time->tm_hour;

                                int minutes = local_time->tm_min;

                                int total_minutes = hours*60+minutes + ahead*30;

                                total_minutes%=24*60;

                                return total_minutes%60;

                }

///////////////////////////////////////////////////////////////////////

/////////////////////////////// main.cpp///////////////////////////////

///////////////////////////////////////////////////////////////////////

#include <iostream>

#include "Clock.h"

#include "WorldClock.h"

using namespace std;

int main(){

                Clock clock;

                cout<<"New York Time: "<<clock.get_time()<<endl;

                WorldClock worldClock(3);

                cout<<"London Time: "<<worldClock.get_time()<<endl;

}

///////////////////////////////////////////////////////////////////////

Add a comment
Know the answer?
Add Answer to:
Use c++ to code: Problem 1 Implement a class Clock whose get hours and get_minutes member functions return the current...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    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....

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that...

    CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that keeps an appointment book. Make a class Appointment that stores a description of the appointment, the appointment day, the starting time, and the ending time. Your program should keep the appointments in a sorted vector. Users can add appointments and print out all appointments for a given day. When a new appointment is added, use binary search to find where it should be inserted...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT