0


Hey Friends Today I Will Tell You How To Convert Time From 24 To 12 Hours By Using Operator Overloading. From Military Time To Civilian Time. In C++ (OOP)

First Of All You Should Have To know About Both These Times.

 TABLE 

__________________________________________

12-Hour Time               24-Hour Time __________________________________________

12:00 a.m. (midnight)           00:00
12:01 a.m.                      00:01
1:00 a.m.                       01:00
6:00 a.m.                       06:00
 11:59 a.m                       11:59
12:00 p.m. (noon)              12:00
12:01 p.m.                     12:01
6:00 p.m.                      18:00
11:59 p.m.                     23:59




Note that 12 a.m. (midnight) in civilian time is 00 hours in military time. There is no 0 hour in civilian time.
Use This Code For Converting Time If You Do not Understand Then Feel Free To Comment I will Reply You As Soon As It Is Possible.
Given Below Code Is The Most Simplest Code....
 ////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
#include<string.h>
using namespace std;
class time12
{
private:
int hours;
int minutes;
string am_pm;
public:
time12(int hrs,int min,string ap):hours(hrs),minutes(min),am_pm(ap){}
    void showdata()const
    {
        cout<<"Time in 12 hrs is = ";
        if(hours<10) cout<<'0';
        cout<<hours<<':';
        if(minutes<10) cout<<'0';  
         cout<<minutes<<' ';
        cout<<am_pm;
    }   
};
class time24
{
private:
int hrs,min,sec;
string ampm;
    public:
        time24():hrs(0),min(0),ampm(""){};
        void getdata()
        {
        hrs:
        cout<<"Please Enter 24 hrs Time\nEnter Hours (0 to 23) ";
        cin>>hrs;
       
        if(hrs>23)
        {
        cout<<"Sorry The Hours You Enter Are Invalid\n\n\t===>Input Hours Again<===\n\n\n";
        goto hrs;
        }
        min:
        cout<<"\nEnter Minutes ";
        cin>>min;
        if (min>59)
        {
        cout<<"Sorry The Minutes You Enter Are Invalid\n\n\t===>Input Minutes Again<===\n\n\n";
        goto min;
        }
        sec:
        cout<<"\nEnter Seconds";
        cin>>sec;
        if (sec>59)
        {
        cout<<"Sorry The Seconds You Enter Are Invalid\n\n\t===>Input Second Again<===\n\n\n";
        goto sec;
        }
        cout<<endl;   
           
        }
   
    operator time12()
    {
    if(sec>29)
    {
    //sec=0;
    min++;   
    }
    if(min==60)
    {
    min=0;
    hrs++;   
    }
    if(hrs>=12)
    {
    ampm="PM";
    }
    else
     ampm="AM";   
//    hrs12=hrs;
    if(hrs<13)
    {
    hrs;   
    }
    else
        {

        hrs-=12;
       
        if(hrs==12)
        {
            ampm="AM";
        }
        }
           
    return time12(hrs,min,ampm);
    }
};

int main ()
{
    while(true)
    {
    time24 t24;
    t24.getdata();
    time12 t12 = t24;
    t12.showdata();
    }
return 0;

}
 

Post a Comment

 
Top