Anonymous

Write A C++ Program Which Contains A Class Named Time Having Three Data Members.?

3

3 Answers

Anonymous Profile
Anonymous answered
#include
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 "<":"<":"<}

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);
}



By Azm
ayesha shahid Profile
ayesha shahid answered
This task explores defining a relatively simple C++ class.
The class you will implement is called Time. It represents all possible times in a 24-hour
period, including hours, minutes and seconds. An immediate representation issue is how to
handle morning (am) and afternoon (pm) times. We could have a separate bool indicating
whether the time is am or pm. It is easier, however, to represent the hours in military time. This
means that the hours of the day are numbered from 0 to 23, with 13 being 1 pm, 14 being 2 pm,
etc.
Checkpoints
1. In the first checkpoint you will get started by implementing the initial class design,
several member functions, and a “driver” main program. Most of the following
instructions are built around use of Visual Studio.
A) If you are working in Visual Studio, create a new project. Eventually you will
create three files within the project. These files will be called Time.h, Time.cpp
and main.cpp.
B) Begin work on Time.h. Create this file by selecting the “Add New Item...” option
in the Project Menu (or use Ctrl+Shift+A). Select which type of new file you want
to add (in this case a Header file) and give it the name Time. You do not need to
include the extension (in this case .h) in the name because it will be added for
you. Adding the extension though will have the same effect. (Aside: When there is
more than one project in the Solution you need to make sure that the correct
project is highlighted in the Solution Explorer before carrying out the above.)
Now click OK. Within the file, declare a class called Time. Read the syntax
carefully (such as the semi-colon at the end of the class declaration). Add private
member variables for the hour, minute and second. In the public area of the
class, declare two constructors: One, the default constructor, should initialize each
of the member variables to 0; the other, having three arguments, accepts initial
values for the hour, minute and second as function call arguments. Declare
member functions to access the values of the hour, the minute and the second
(three different member functions). It will be crucial for Checkpoint 3 to make
these const. (Recall: A const member function cannot change the member
variables.)
c) Switch to working on main.cpp. Create a new file within your project, as above.
Be sure to add code to #include Time.h in addition to including iostream.
(Warning: The syntax of the include statement is different. Have the main
program create two Time objects, one using each constructor. Show use of the
functions that access the values of hour, minute and second by printing the two
times.
D) Switch to working on Time.cpp. Create a new file within your project as above.
Don’t forget to add the line to #include Time.h. Implement the constructors and
member functions.
E) Now, compile your program and remove errors. Here’s where the difference
between compiling and linking matters.
For Visual Studio users, you can compile each of the two .cpp files individually
(the .h file isn’t compiled separately) by typing Ctrl-F7 when the pane containing
that file is active. This will allow you to see and remove compiler errors for each
file individually. (Note that errors caused by the code in the .h file, Time.h will
appear when compiling either .cpp file.) You can also compile and link multiple
files at once using Build/Build project name. You will have to do this even after
compiling each .cpp file individually because the Ctrl-F7 command does not link
to create an executable program.
Anonymous Profile
Anonymous answered
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.

Answer Question

Anonymous