Answer to Question #230072 in C++ for Arup

Question #230072

 WAP to add two time[hr:min:sec] using class and obj...concept(passing object as an argument).


1
Expert's answer
2021-08-28T08:58:42-0400
#include <iostream>
using namespace std;


class Time
{
	int hr;
	int min;
	int sec;


public:
	Time()
	{
		this->hr = 0;
		this->min = 0;
		this->sec = 0;
	}
	Time(int hr, int min, int sec)
	{
		this->hr = hr;
		this->min = min;
		this->sec = sec;
	}


	Time add(Time otherTime)
	{
		Time result;


		result.sec = this->sec + otherTime.sec;
		result.min = this->min + otherTime.min;
		result.hr = this->hr + otherTime.hr;
		if(result.sec >= 60){
			result.min ++; 
			result.sec = result.sec % 60; 
		} 
		if(result.min >= 60){
			result.hr ++; 
			result.min  = result.min  % 60; 
		}


		return result;
	}


	void display(){
		cout<< this->hr << ":" << this->min << ":" << this->sec;
	}
};


int main()
{


	int hr,min,sec;


	cout << "Enter the first time: " << endl;
	cin>> hr >> min >> sec;
	Time t1(hr, min, sec);// t2, 
	cout << "Enter the second time: " << endl;
	cin>> hr >> min >> sec;
	Time t2(hr, min, sec);
	Time result=t1.add(t2);


	cout << "Sum: "<< endl;
	result.display();


	cout << endl<< endl;


	system("pause");
	return 0;
}







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS