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.
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?
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...
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute...
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute (between 0 - 59) second (between 0 - 59) MyTime class consists of the following members: Default constructor: set the time to 00:00:00 Constructor taking 3 integers i.e., hour, minute and second respectively. This constructor sets this object's Time based on the given parameters. Constructor taking 2 integers i.e., hour and minute respectively. This constructor sets this object's hour and minute based on the...
c++ Design the Weather class that contains the following members: Data members to store: -a day...
c++ Design the Weather class that contains the following members: Data members to store: -a day (an integer) -a month (an integer) -a year (an integer) -a temperature (a float) -a static data member that stores the total of all temperatures (a float) Member functions: -a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly entered temperature to the total). -a static...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: -...
C++ Assignment. Design the Weather class that contains the following members: Data members to store: - a day (an integer) - a month (an integer) - a year (an integer) - a temperature (a float) - a static data member that stores the total of all temperatures (a float) Member functions: - a constructor function that obtains a day, month, year and temperature from the user. This function should also accumulate/calculate the total of all temperatures (i.e., add the newly...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT