I need Code ((Classes and Objects ,Operator overloading))

Write a C++ program which contains a class named Time having three data members.
• Hours
• Minutes
• Seconds

The class must have
• A default and parameterized constructor
• show() method to display the time in proper format like HH:MM:SS on the screen
• get() method to get time from user
• Overloaded Plus (+) and Minus (-) operators
• A destructor

You will overload + and - operator for this class.

In main program make 3 objects of the Time class time1, time2 and time3 and call the get() functions for time1 and time2 then perform time3 = tim1+time2 and then you will display time3 using its show() function.

Note1: While Adding the time keep in mind do not just add the hours into hours and minutes into minutes and seconds in seconds , e.g
10:25:10
+01:45:25
---------------------
11: 70: 35

Will not be correct, instead your code should add times like, Note that as number of minutes have increased 60, hour have been increased.

10:25:10
+ 01:45:25
-------------------
12: 10: 35


Please Help me and write code for me .. its very difficult for me becoz am learner and i want this code before 20 jan
We will not do homework requests for you. Write some code yourself first.
well, i don't know about this question yet ... because i missed my lectures just coz of accident .... and i am asked to submit this code ... that's why am asking over here....... otherwise i will loss ma grade.. but it for sure i will learn and overcome on ma missing lectures. i know its not difficult for you guy's if you will help me than i will be very thank full and next time i 'll just mistakes and problems not for complete code
It's a simple enough assignment that you should be able to do some searches and still complete it in time. However, you'll have to actually spend some time reading and learning...
I'm just trying to help you. I found this solution on the link below

http://qaboard.cramster.com/computer-science-topic-5-445472-cpi0.aspx

The code below is compiled.

/* Sample run with your data
Enter the time
Hour: 10
Minutes: 23
Seconds: 10
Enter the time
Hour: 1
Minutes: 45
Seconds: 25
Time 1 : 10:23:10
Time 2 : 01:45:25

Added :
10:23:10
+01:45:25
----------
12:08:35

Subtracted:
10:23:10
-01:45:25
----------
08:37:45
Press any key to continue . . .
*/





#include<iostream>
#include<iomanip.h>
using namespace std;
void convert(int,int&,int&,int&);
class Time
{
public:
int hour,minute,second;
Time()
{hour=0;
minute=0;
second=0;
}
~Time()
{hour=0;
minute=0;
second=0;
}
Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
void get()
{cout<<"Enter the time\n";
cout<<"Hour: ";
cin>>hour;
cout<<"Minutes: ";
cin>>minute;
cout<<"Seconds: ";
cin>>second;
}
void show()
{cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
}
Time operator + ( Time t)
{int h,m,s;
int totalsec;
totalsec=(hour*3600+minute*60+second)+(t.hour*3600+t.minute*60+t.second);
convert(totalsec,h,m,s);
return Time(h,m,s);
}
Time operator - ( Time t)
{int h,m,s,totalsec;
totalsec=(hour*3600+minute*60+second)-(t.hour*3600+t.minute*60+t.second);
if(totalsec<0)
totalsec=-totalsec;
convert(totalsec,h,m,s);
return Time(h,m,s);
}
void convert(int totalsec,int& h,int& m,int& s)
{ s=totalsec%60;
totalsec=totalsec/60;
m=totalsec%60;
h=(totalsec/60)%12;
if(h==0)
h=12;
return;
}
};

int main()
{ Time time1,time2,added,subtracted;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
time1.get();
time2.get();
added=time1+time2;
subtracted=time1-time2;
cout<<"Time 1 : ";
time1.show();
cout<<"Time 2 : ";
time2.show();
cout<<"\nAdded :\n ";
time1.show();
cout<<"+";
time2.show();
cout<<"----------\n ";
added.show();
cout<<"\nSubtracted:\n ";
time1.show();
cout<<"-";
time2.show();
cout<<"----------\n ";
subtracted.show();
system("pause");
}
@syedmuhammadali:

Please do not post complete solutions like this for people who have clearly not done any work on their own. Although there are some people who will try to learn from the code, most will simply copy+paste.
Zhuge or seymore15074
- now that the damage has been done and ishii has moved on to the next crisis, I have some questions. I have modified the code according to my experience in programming classes.
1. Is it considered better form to keep the function definitions and declarations together in the class definition??? I prefer to eventually separate the class definition into a header file and the function details into an implementation file. Since I haven't been programming with classes for long, I'm not sure what is "normal".
2. The code obtained from cramster did not include a destructor member. Is that because the member variables are not declared as pointers as is shown in CRectangle at http://www.cplusplus.com/doc/tutorial/classes.html. Is a destructor really necessary for something like this. I could understand wanting a destructor for a class of bullets or aliens.

Thanks for any help you can offer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
using namespace std;

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

public:
    Time(); // constructor
    Time(int,int,int); // constructor
    void get();
    void show();
    Time operator + (Time);
    Time operator - (Time);
};

int main()
{
    Time t1;
    Time t2;
    cout<<"Enter first time:";
    t1.get();
    cout<<"Enter second time:";
    t2.get();
    Time t_sum = t1 + t2;
    Time t_diff = t1 - t2;
    cout<<"Sum ";
    t_sum.show();
    cout<<"Diff ";
    t_diff.show();
    system("pause");
}

Time::Time()
{
    hr=0;
    min=0;
    sec=0;
}

Time::Time(int h,int m,int s)
{
    hr=h;
    min=m;
    sec=s;
}

void Time::get()
{
    cout<<"\nEnter hrs:";
    cin>>hr;
    cout<<"Enter min:";
    cin>>min;
    cout<<"Enter sec:";
    cin>>sec;
}

void Time::show()
{
    cout<<"Time is "<<hr<<":"<<min<<":"<<sec<<endl;
}

Time Time::operator + ( Time t)
{
    int h,m,s;
    int sum;
    sum = (hr + t.hr)*3600 + (min + t.min)*60 + sec + t.sec;
    s = sum % 60;
    sum = sum / 60;
    m = sum % 60;
    h = sum / 60;
    return Time(h,m,s);
}

Time Time::operator - ( Time t)
{
    int h,m,s;
    int sum1,sum2,sum;
    sum1 = (hr)*3600 + (min )*60 + sec ;
    sum2 = (t.hr)*3600 + (t.min)*60 + t.sec;
    if (sum1>sum2)
        sum = sum1-sum2;
    else
        sum = sum2-sum1;
    s = sum %60;
    sum = sum/60;
    m = sum % 60;
    h = sum/60;
    return Time(h,m,s);
}

Generally you put the implementation in the header file only if either a) it is a template function or function in a template class, or b) you would like the compiler to inline the function if possible.

Time does not need a destructor since there is nothing that needs to be done.
(Generally in good OO design a destructor is needed only to free up any resources owned by the class that would otherwise not automatically be freed, such as pointers to dynamically allocated memory).
A bundle of thanks to you specially syedmuhammadali and egregory314
i have done ma assignment on time with the help of ur posts
am really thank full to you.....
Onece again thanx from ma core of heart
thanx alot
Topic archived. No new replies allowed.