how to add two class object as argument in c++

How to add two class object as argument 

Create a class time and constructor having hour, minute and second as an argument which is used to take two time data from user. The add function that takes two class object as argument adds them respectively then display aggregate result.

soln:  to add two class object as argument:
#include<iostream>
#include<conio.h>
using namespace std;
class time{
int h,m,s;
public:
time(){
}
time(int hr,int mi,int se)
{
h=hr;
m=mi;
s=se;
}
void add(time a, time b)
{
h=a.h+b.h;
m=a.m+b.m;
s=a.s+ b.s;
s=h*3600 + m*60+s;
h=s/3600;
m=(s%3600)/60;
s=((s%3600)%60);
}
void display(){
cout<<"hours="<<h<<"minutes="<<m<<"seconds="<<s;
}
};
int main()
{
time a(5,50,10);
time b(5,20,30);
time c;
c.add(a,b);
c.display();

}

OUTPUT:
To add two class object as argument 
to add two class object as argument


No comments

Powered by Blogger.