VUSuperior Chat Room

Sunday, 17 May 2015

CS304 - Object Oriented Programming Assignment No. 1 Solution and Discussion Spring 2015 Due Date: May 20, 2015


#include <iostream>
using namespace std;
class Time {
public:
     Time();                  // default constructor
     void setTime(int,int,int);
     void show24hour();
     void show12hour();
private:
     int hour;      
     int minute;          
     int second;          
};  
Time::Time() { hour = minute = second = 0; }


void Time::setTime(int h,int m,int s)
{
     hour = (h >= 0 && h < 24) ? h : 0;
     minute = (m >= 0 && m < 60) ? m : 0;
     second = (s >= 0 && s < 60) ? s : 0;
}
 // show  time in 12 hour format
void Time::show12hour()
{
     cout << ((hour == 0 || hour == 12) ?12 : hour % 12) ;
  cout << ":" << (minute < 10 ? "0" : "") << minute ;
  cout << ":" << (second < 10 ? "0" : "") << second ;
  cout << (hour < 12 ? " AM" : " PM");
}
 // show  time in 24 hour format
void Time::show24hour()
{
     cout << (hour < 10 ? "0" : "") << hour << ":" ;
   cout << (minute < 10 ? "0" : "") << minute  ;

}


//main simple class Time
int main()
{
 
     Time t;   // instantiate object t of class Time
cout << "initial time " << endl;      
     cout << "Time display in 12 hours format: ";
     t.show12hour();
     cout << "\nTime display in 24 hours format:";
     t.show24hour();

    t.setTime(13,55,23);
    cout << "\nAfter set: ";
     cout << "\nTime display in 12 hours format:  ";
     t.show12hour();
     cout << "\nTime display in 24 hours format: ";
      t.show24hour();
        t.setTime(25,67,98);
 
 
cout << "\nAfter Setting invalid  Values:\n" ;
     
    t.show12hour();
    cout << "\n" ;  
    t.show24hour();

    cout << endl;
return 0 ;
}

0 comments:

Post a Comment