C Program basic classes we will learn how to use classes and
C++ Program basic classes!
we will learn how to use classes and inheritance in a practical application. You will implement three classes (Clock, Digital, and ConvertTime). Digital class will inherit from Clock class. ConvertTime will inherit from Digital class.
The first class in the program is the Clock class, it will contain: - Three member variables to describe the time: hours, minutes and seconds. -
A default constructor. -
Accessor and mutator functions. -
Member function show() that will output the time (hr:min:sec) in two digit format for each variable (e.g. 01:09:45).
Next, you will have the Digital class that is derived from Clock class.
This class will contain: -
SetAMPM() : It will add (AM/PM) next to the time depending on whether the time is before midday or after midday. -
Increment(): This function will increment the seconds variable by 1. Depending on the new seconds, you need to see if you need to update minutes or not, and the same thing for hours.(e.g. 11:59:59 after incrementing the sec will be 12:00:00) -
Redefine show() function to output the time adding AM or PM at the end.(11:23:45AM) -
Member variable of type char [size] for the AM/PM. -
Add appropriate constructors.
Next, you will have ConvertTime class that is derived from Digital class. This class will contain: -
Redefine show() function: will convert the time to GMT time zone, then will output the correct time. -
timer(): will read the current time from the computer (min:sec) and will set the timer for exactly 2 min and 0 sec. After the timer is up, a message will appear on the screen to notify the user that the time is up and will ask if he/she wants to set timer for 2 mins again.
Finally main() function, it will not do much: - Declare an object for the Digital class. -
The program should allow the user to input the time for hr, min and sec. -
By using the Digital class object, the program should output the time (that the user just entered) on the screen in the correct format. ( Time is 11:23:59 AM ) - Output the time again but after incrementing the seconds. ( Time is 11:24:00 AM ) - Declare an object for ConvertTime class.
Use the show() function to output the time after converting it to GMT. -
Call the timer() function.
Note: For timer() function: There are built in classes, functions that you can use to read the current local time from your computer in hour, mins, and seconds. The syntax for these commands are as follows
#include time_t now; struct tm nowlocal; now = time(NULL); nowlocal = *localtime(&now); // now you can use nowlocal.tm_min, nowlocal.tm_sec,nowlocal.tm_hour.
Solution
#include <iostream>
using namespace std;
class clock
{
public int hours,minutes seconds;
clock();
private :
int min2;
public :
int Getmin2();
};
int Toto::Getmin2(){ //accessor function
return min2;
}
class Toto{
private :
int _time2;
public :
void Settime2(int);
};
void Toto::Settime2(int time2){ // mnutator function
_time2 = time2;
}
public void show() // output the time
{
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << \'-\'
<< (now->tm_mon + 1) << \'-\'
<< now->tm_mday
<< endl;
class digital :public clock //derived class
{
public string ampm()
{
time_t tim;
time(&tim);
cout << ctime(&tim)
}
public void increment() //increment seconds and minutes variable by 1
{
private:
int hours; // 0 to 23
int minutes; // 0 to 59
public:
// required constructors
Time(){
hours = 0;
minutes = 0;
}
Time(int h, int m){
hours = h;
minutes = m;
}
// method to display time
void displayTime()
{
cout << \"H: \" << hours << \" M:\" << minutes <<endl;
}
// overloaded prefix ++ operator
Time operator++ ()
{
++minutes; // increment this object
if(minutes >= 60)
{
++hours;
minutes -= 60;
}
return Time(hours, minutes);
}
// overloaded postfix ++ operator
Time operator++( int )
{
// save the orignal value
Time T(hours, minutes);
// increment this object
++minutes;
if(minutes >= 60)
{
++hours;
minutes -= 60;
}
// return old original value
return T;
}
};
}
class converttime:public digital
{
public void redefineshow()
{
time_t t;
struct tm tm;
struct tm * tmp;
...
t = timegm(&tm);
...
tmp = gmtime(t);
}
public timer() // setting up the timer
{
Sleep(sec*1000);
}
int main()
{
cout << \"Wait 5 seconds \" << endl;
timer(2);
cout << \"Timer is up ! DO you wish to put the timer again ? \" << endl;
return 0;
}
int main(void)
{
digital d;
converttime ct;
ct.timer(1);
d.show();
cout<<\" time is \";
Time T1(11, 59), T2(10,40);
++T1; // increment T1
T1.displayTime(); // display T1
++T1; // increment T1 again
T1.displayTime(); // display T1
T2++; // increment T2
T2.displayTime(); // display T2
T2++; // increment T2 again
T2.displayTime(); // display T2
return 0;
}



