Write a OOP to create a class named "Time" with data members 'hour' and 'minute' a constructor to initialize............. ---PU-2011

Sep 15, 2018

Q. Write a OOP to create a class named "Time" with data members 'hour' and 'minute' a constructor to initialize the objects, and two  member functions to add two times and display the resultant time. you must demonstrate returning object from the function that will add two times.

Program:

// Program to add two times

#include
using namespace std;
class Time{
	private:
		int hour;
		int minute;
	public:
		//constructor for initializing objects
		//this constructor uses default arguments
		Time(int h = 0, int m = 0){
			hour = h;
			minute = m;
		}
		Time add(Time t){
			Time temp;
			temp.minute = minute + t.minute;
			temp.hour = hour + t.hour;
			if(temp.minute >= 60){
				temp.hour++;
				temp.minute -= 60;
			}
			return temp;
		}
		// function to display time
		void display(){
			cout<<hour<<" hr "<<minute<<" min"<<endl;
		}
};

int main(){
	Time t1(4,30), t2(5,30),t3;
	t3 = t1.add(t2);
	t1.display();
	t2.display();
	t3.display();
	return 0;
}

Sample Run:

4 hr 30 min
5 hr 30 min
10 hr 0 min

Related


Tools/Apps





© Nepal Exchange Rates