You are on page 1of 11

Question # 1 Create a class time.

Your class should include three integer data members to hold values for hours, minutes and seconds and one additional data member to hold value for am / pm. Your class should provide a constructor that receives values for hours, minutes, seconds and AM/PM. The inputs should be validated. Also provide a default constructor. Add usual set and get functions for data members. Add a function tick() to the class time. Write a program that tests the tick member function in a loop that prints the time during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute b) Incrementing into the next hour c) Incrementing into AM to PM and PM to AM for a 12 hour format. #include <iostream.h> #include <conio.h> #include <string> using namespace std; class Time{ public:

Time(int h=0, int m=0, int s=0, string mer="am"); void setTime(int, int, int, string); void setHour(int); void setMinute(int); void setSecond(int); void setMeridiem(string); int getHour(void); int getMinute(void); int getSecond(void); string getMeridiem(void); void print(void); void tick(void); private: int hour; int minute; int second; string meridiem; }; Time::Time(int hr, int min, int sec, string mer) { setTime(hr, min , sec, mer); void Time::setTime(int h, int m, int s, string mer) { setHour(h); setMinute(m); setSecond(s);

setMeridiem(mer); } void Time::setHour(int h){hour=(h>=1 && h<12)?h:12;} void Time::setMinute(int m){ minute = (m>=0 && m < 60)?m:0; } void Time::setSecond(int s){ second = (s>= 0 && s < 60)? s: 0; } void Time::setMeridiem(string md){ if(md == "am" || md == "pm"){ meridiem = md; }else{ meridiem = "am"; } } int Time::getHour(void){return hour;} int Time::getMinute(void){return minute;} int Time::getSecond(void){return second;} string Time::getMeridiem(void){return meridiem;} void Time::print(void){ cout<<hour<<":"<<minute<<":"<<second<<" "<<meridiem<<endl;

} void Time::tick(void){ setSecond(getSecond() + 1); if(getSecond() == 0){ setMinute(getMinute()+1); } if(getMinute() == 0 && getSecond() == 0){ setHour(getHour() + 1); } if(getHour() == 12){ if(getMeridiem() == "am"){ setMeridiem("pm"); } else{ setMeridiem("am"); } } } const int MAX_TICKS = 10; int main(){ Time t(5,59,58,"am"); for(int ticks = 1; ticks < MAX_TICKS; ++ticks){ t.print(); cout<<endl; t.tick();

} getch(); return 0; } Question 2 Write member functions and utility functions for the class Date as per given in following class interface. #include <conio.h> #include <string> using namespace std; class Date { // interface of the class public: void display(); // to display the date on the screen void setDay(int i); // setting the day void setMonth(int i); // setting the month void setYear(int i); // setting the year int getDay(); // getting the value of day int getMonth(); // getting the value of month int getYear(); // getting the value of year void nextDay(); // Constructors of the class Date(); Date (int, int); Date(int, int, int);

// Destructor of the class ~Date (); // hidden part of the class private: int day, month, year; int getDoMonth(int, int); //to get no. of days in the month }; Date::Date(){ } Date::~Date(){ } Date::Date(int d, int m){ setDay(d); setMonth(m); } Date::Date(int d, int m, int y){ setDay(d); setMonth(m); setYear(y); } void Date::setDay(int d) {day=d;} void Date::setMonth(int m) {month = m;}

void Date::setYear(int y) {year = y;} int Date::getDay(void) {return day;} int Date::getMonth(void) {return month;} int Date::getYear(void) {return year;} void Date::display(void){ cout<<day<<"-"<<month<<"-"<<year<<endl; } void Date::nextDay(void){ int nextday = day + 1; int totaldays = getDoMonth(month , year); if(nextday > totaldays){ nextday = 1; } cout<<nextday; } int Date::getDoMonth(int month, int year){ int Days; if(month == 4 || month == 6 || month == 9 || month == 11) Days = 30; else if (month == 02)

{ bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if (isLeapYear == 0) Days = 29; else Days = 28; } else Days = 31; return Days; } Int main(){ Date d(5,10,2011); d.nextDay(); getch(); return 0; } Question 3 Create a class base to store an integer value along with its base (2, 5, 8 and 10). Provide a constructor that sets integer and its base, with default values of 10 for base and 0 for integer value. Provide member function display to print integer in the form base-2(11001100). Another member function to change the integer to any one of the other base (2,5,8 and 10). Use long integer to store the value. #include <iostream.h> #include <conio.h>

#include <string> #include <stdio.h> #include <stdlib.h> using namespace std;

class Base{ public: Base(int val=0, int base=0); void setBaseValue(int, int); void setValue(int); void setBase(int); void setResultValue(long int); long convertBase(int baseTo); int getValue(void); int getBase(void); long int getResultValue(void); void print(void); private: int value; int base; long int resvalue; }; Base::Base(int v, int b){

setBaseValue(v, b); } void Base::setBaseValue(int v, int b){ setValue(v); setBase(b); } void Base::setValue(int v){ value = v; } void Base::setBase(int b){ base = b; } int Base::getValue(void){return value;} int Base::getBase(void){return base;} void Base::print(){ int i = getValue(); char buffer [33]; itoa(i,buffer,2); cout<<"Binary: "<<buffer<<endl; } long int Base::convertBase(int to){

int i = getValue(); char buffer [33]; long int res; itoa (i,buffer,to); res = atol (buffer); return res; } Int main(){ Base b1(20, 10); b1.print(); cout<<"please enter output base : "; int reqbase; cin>>reqbase; long int res = b1.convertBase(reqbase); cout<<res; getch(); return 0; }

You might also like