Object-Oriented Programming Laboratuvar 10 12.11.2017 1. Aşağıda declaration ı verilen Time class ının definition ını yazın. Bir Time objesindeki zaman değerini bir saniye arttıran tick metodunu ekleyin. tick metodu, Time objesindeki zaman değerinin doğru olmasını sağlamalıdır. Bu sebeple dakika arttırma, saat arttırma işlemlerinin de doğru yapılması gerekmektedir. İstenilenleri yerine getirdikten sonra Time class ını ve tick metodunu test edin. 18:00:00 dan başlayarak bir Time objesini 24 saat boyunca tick ile arttırın. Her tick ten sonra objeyi konsola yazdırın. #ifndef TIME_H #define TIME_H class Time public: Time(int h=0,int m=0,int s=0); void sethour(int h); void setminute(int m); void setsecond(int s); int gethour() const; int getminute() const; int getsecond() const; void printmilitary() const; void printstandard() const; void tick(); private: int hour; int minute; int second; ; #endif // TIME_H #include "Time.h" #include <iostream> using std::cout; using std::endl; Time::Time(int h,int m,int s) sethour(h); setminute(m); setsecond(s); void Time::setHour(int h) if(0<=h && h<=23) hour = h;
hour = 0; void Time::setMinute(int m) if(0<=m && m<=59) minute = m; minute = 0; void Time::setSecond(int s) if(0<=s && s<=59) second = s; second = 0; int Time::getHour() const return hour; int Time::getMinute() const return minute; int Time::getSecond() const return second; void Time::printMilitary() const if(hour<10) cout << hour << ":"; if(minute<10) cout << minute << ":"; if(second<10) cout << second; void Time::printStandard() const
if(hour%12<10) cout << hour%12 << ":"; if(minute<10) cout << minute << ":"; if(second<10) cout << second; if(0<=hour && hour<12) cout << " AM"; cout << " PM"; void Time::tick() second++; if(second==60) second = 0; minute++; if(minute==60) minute = 0; hour++; if(hour==24) hour = 0; #include "Time.h" #include <iostream> using std::cout; using std::endl; int main(void) Time T(18,0,0); int t; for(t=0; t<24*60*60; t++)
T.printMilitary(); cout << "\t"; T.printStandard(); cout << endl; T.tick(); return 0; 2. Aşağıda tarif edilen Date class ını, gerek gördüğünüz özelliklerle birlikte oluşturun. Oluşturduğunuz class ın özelliklerini test etmek için kendi main fonksiyonunuzu yazın. - Bir Date objesindeki tarih değerini aşağıda verilen formatlarda konsola yazan metodlar olsun. DDD YYYY 12.11.2017 tarihi için: 342 2017 MM/DD/YY 12.11.2017 tarihi için: 11/12/17 Month DD, YYYY 12.11.2017 tarihi için: Kasım 12, 2017 - Yukarıda verilen formatlardaki yer alan değerleri alarak (yer almayanları almayarak) Date objesine ilk değer ataması yapan costructor metodları olsun. DDD YYYY Gün sırasını ve yıl alan bir constructor. Ay değeri gün sayısından hesaplansın. MM/DD/YY Gün, ay ve yıl değerlerinin tamamını alan bir constructor. Month DD, YYY Gün, ay ve yıl değerlerinin tamamını alan bir constructor. #ifndef DATE_H #define DATE_H class Date private: int day; int month; int year; public: Date(int ddd, int yyyy); Date(int mm, int dd, int yy); void ddd_yyyy(void) const; void mm_dd_yy(void) const; void month_dd_yyyy(void) const; void setday(int d); void setmonth(int m); void setyear(int y); bool leapyear(void) const; int daysofmonth(void) const; const char* monthname(void) const; ; #endif // DATE_H #include "Date.h"
#include <iostream> using std::cout; using std::endl; Date::Date(int ddd, int yyyy) setyear(yyyy); month = 1; day = 1; while(ddd>daysofmonth()) ddd-=daysofmonth(); month++; day = ddd; Date::Date(int mm, int dd, int yyyy) setyear(yyyy); setmonth(mm); setday(dd); void Date::ddd_yyyy(void) const int ddd=0, lpr; Date D(1, 1, year); for(lpr=1; lpr<month; lpr++) D.setmonth(lpr); ddd += D.daysofmonth(); ddd += day; if(ddd<10) cout << "00"; if(ddd<100) cout << ddd << " "; if(year<10) cout << "000"; if(year<100) cout << "00"; if(year<1000) cout << year; void Date::mm_dd_yy(void) const
if(month<10) cout << month << "/"; if(day<10) cout << day << "/"; if(year%100<10) cout << year; void Date::month_dd_yyyy(void) const cout << monthname() << " "; if(day<10) cout << " "; cout << day << ", "; cout << year; void Date::setday(int d) if(1<=d && d<=daysofmonth()) day = d; day = 1; void Date::setmonth(int m) if(1<=m && m<=12) month = m; month = 1; void Date::setyear(int y) if(0<=y) year = y; year = 1970; bool Date::leapyear(void) const if((year%4==0 && year%100!=0) (year%400==0)) return true; return false;
int Date::daysofmonth(void) const switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: if(leapyear()) return 29; return 28; case 4: case 6: case 9: case 11: return 30; default: return 0; const char* Date::monthname(void) const static char monthnames[][16] = "Ocak", "Subat", "Mart", "Nisan", "Mayis", "Haziran", "Temmuz", "Agustos", "Eylul", "Ekim", "Kasim", "Aralik"; return monthnames[month-1]; #include "Date.h" #include <iostream> using std::cout; using std::endl; int main(void)
Date d_1(316, 2017); Date d_2(11, 12, 2017); cout << "d_1:" << endl; d_1.ddd_yyyy(); cout << endl; d_1.mm_dd_yy(); cout << endl; d_1.month_dd_yyyy(); cout << endl; cout << endl; cout << "d_2:" << endl; d_2.ddd_yyyy(); cout << endl; d_2.mm_dd_yy(); cout << endl; d_2.month_dd_yyyy(); cout << endl; cout << endl; return 0;