Search This Blog

Wednesday 24 February 2016

Create a class called TIME that has separate member data for hour(int) and minutes(int).Include the following member functions: setdata( )to set these values to predefined values in the program getdata( )to get these values from the user putdata( ) to di splay these values. Member function add_time( ) to add two time objects to a third time object (e.g. T3.add_time(T1,T2). Make new function to return a time object, so that the function works as follows: T3=T1.add_time(T2).

Practical 14


#include<iostream>
#include<iomanip>

using namespace std;

class TIME
{
    int hour,minutes;
    void setdata(int a,int b)
    {hour=a;minutes=b;};
    public:
        void getdata();
        void putdata();
};

void TIME::getdata()
{
    int a,b;
    cout << "\t\tEnter Hours:";
    cin >> a;
    cout << "\t\tEnter minutes:";
    cin >> b;
    setdata(a,b);
}

void TIME::putdata()
{
    cout << setw(15) << left << "\t\tHour. - " << hour << endl
         << setw(15) << left << "\t\tMInutes - " << minutes << endl;
}

int main()
{
    TIME T1,T2,T3;
    T1.getdata();
    T2.getdata();
    T1.
    return 0;
}
#include<iostream>
using namespace std;
class time1
{
    int hour,minute;
    public:
    void setdata(int x,int y)
    {
        hour=x;
        minute=y;
    }
    void getdata()
    {
        cout<<"\n enter hour:";
        cin>>hour;
        cout<<"\n enter minute:";
        cin>>minute;
    }
    void putdata()
    {
        cout<<"\n hour:"<<hour<<"\n minute:"<<minute;
    }
    friend void add_time(time1,time1);

};
    void add_time(time1 a1,time1 a2)
{
    int hour,minute;
    hour=a1.hour+a2.hour;
    minute=a1.minute+a2.minute;
    while(minute>=60)
    {
        hour++;
        minute=minute-60;
    }
   cout<<"\n hour:"<<hour<<"\t minute:"<<minute;
}



int main()
{
    time1 p1,q1;
    p1.getdata();
    q1.getdata();
    add_time(p1,q1);
    return 0;   
}
 


output:

Enter hour : 1
Enter min : 40

enter hour : 1
enter min : 40

3 hour 20 min

No comments:

Post a Comment