Question

In: Computer Science

Q3 Develop a Time class which encapsulates hour ,minute and second data members. Provide overloaded +,...

Q3 Develop a Time class which encapsulates hour ,minute and second data members. Provide overloaded +, -, ++ and - - overloaded operators for Time.

Solutions

Expert Solution

I have given the program below. I hope it will help you, if you have any query then tell me.

Take care

Programs:

#include <iostream>

#include <math.h>

using namespace std;

class Time

{

private:

int second;

int minute;

int hour;

public:

//Paramiterised Constructor

Time(int h, int m, int s=0) {

second = s;

minute = m;

hour = h;

}

//Default Constructor

Time (){

second = 0;

minute = 0;

hour = 0;

}

//Notice nothing inside barcket which indicates pre-increment ++.

Time operator ++ ()

{

Time temp;

//as in the given question for the test data there is no "second" value I am incrementing "minute" as functionality of ++. If wanted same can be done to increment second value also

temp.minute = ++minute;

temp.hour = hour;

if(minute == 60){

minute = 0;

temp.minute = 0;

hour = hour + 1;

temp.hour = temp.hour + 1;

}

return temp;

}

// Notice int inside barcket which indicates post-increment ++ .

Time operator ++ (int)

{

Time temp;

temp.minute = minute++;

temp.hour = hour;

if(minute == 60){

minute = 0;

hour = hour + 1;

}

return temp;

}

// Method to set variables of Time

void setvalue(int h, int m, int s=0) {

second= s;

minute = m;

hour = h;

}

// Method to get Time

Time getvalue(){

Time temp;

temp.hour=hour;

temp.minute=minute;

temp.second=second;

return temp;

}

// Method to print Time

void printvalue()

{ cout << "\n Time = "<< hour << " H " << minute <<" M " <<second << " S " <<"\n"; }

};

//main function

int main()

{

Time T1(11,59), T2(10,40);  

cout<< "value of T1";

T1.printvalue();

cout<< "value of T2";

T2.printvalue();

// Operator function is called, only then value of obj is assigned to obj1

Time T3 = ++T1;

cout<< "value of T1 after operator function";

T1.printvalue();

cout<< "value of T3, ie. assigned value of T1 at pre-increment";

T3.printvalue();

// Assigns value of obj to obj1, only then operator function is called.

Time T4 = T2++;

cout<< "value of T2 after operator function";

T2.printvalue();

cout<< "value of T4, ie. assigned value of T2 at post-increment ";

T4.printvalue();

return 0;

}

Related Solutions

Develop a java program with a class named friend with data members like name, phno and...
Develop a java program with a class named friend with data members like name, phno and hobby. Use Parameterized constructor to create two friend objects and invoke checkhobby method which takes only one parameter, to find whether they have same hobby or different hobbies
In C++, Implement the following class that represents a clock. Clock - hour: int - minute:...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute: int - meridiem: string + Clock() + Clock(hr: int, min: int, mer: string) + setTime(hr: int, min: int, mer: string): void + setHour(hr: int): void + setMinute(min: int): void + setMeridiem(mer: string): void + getHour(): int + getMinute(): int + getMeridiem(): string + void tick() + string asString() + string asStandard() Implementation Details: • The default constructor should set the clock to midnight (12:00...
At what time past 6:00 do the minute and hour hands overlap ?
At what time past 6:00 do the minute and hour hands overlap ?
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
Given a clock on the planet 9 with hour, minute, and second hands, where day last 14 hours (hour hand circles twice for a day)
USE MATLABGiven a clock on the planet 9 with hour, minute, and second hands, where day last 14 hours (hour hand circles twice for a day)a) At what time (in hours), the first time after 0th hour, all 3 hands will overlap,b) At what times (in hours) they will overlap between 0th hour and 7th hour?
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor...
Q3) Design a class for saving data of employee, the class should have multiple constructors (constructor overloading) for initializing objects with different parameters. The employee class should have different functions for saving and updating bio-data, educational background, and current status like experience, Salary, position & travel history. The employee class should have a function which asks user what do he likes to see about employee (.i.e., Bio-data, current status....) and only show the respective data. Create two objects of interns...
java For this assignment, you will create a Time class that holds an hour value and...
java For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM. In previous assignments, we had a requirement that your class be named Main. In this assignment, the...
At what time past 6:00 do the minute and hour hands overlap? Please show work.
At what time past 6:00 do the minute and hour hands overlap? Please show work.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT