時計をシミュレートする簡単なプログラムを書いています。クラスの日付、クラスの時間、クラスのウォッチ。時計は日付と時刻から派生します。Timeメンバー関数から日付メンバー関数を呼び出そうとしています。物がないとできないと書いてあります。私は学んでいますが、これのほとんどはがらくただと確信しています。どんな助けでも感謝します。基本的に Time::Tick の最後に、Class Date メンバー関数 inDay を呼び出そうとしています。別のクラス メンバー関数から 1 つのメンバー関数を呼び出す。別のクラスからフレンド機能を実行する方法がわかりません。関数を静的にしようとしましたが、エラーの長い道のりを歩んできました。何かアドバイス?
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
class Date
{
public:
Date();
void inDay();;// increments day, tests if month increments, if true, increment
void inMonth(); // increments month, tests if year increments, if true, increment
void inYear(); // increment year
void displayWatch(); // calls daysInMonthSwith, calls inDay, Display date
int daysInMonthSwitch(); // determines the number of days in the month based on which month using a switch
//void testPrint(); //Testing purposes only
void setDay();
void setMonth();
void setYear();
private:
int day;
int month;
int year;
int daysInMonth;
};
class Time{
public:
Time();
void setTime(int, int, int ); // set hours, minutes, seconds
void printStandard(); // print time in standard time format 12h
void setHour();
void setMin();
void setSec();
void tick();
private:
int hour;
int minute;
int second;
};
class Watch: public Time, public Date {
public:
Watch();
int getDate();
void setTime(int, int, int);
int getTime();
private:
};
//class Time::
Date::Date() : // Class Constructor
day(27),
month(2),
year(2013),
daysInMonth(31)
{
} // End Constructor
void Date::setDay()
{
int tempDay;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
while (tempDay < 1 || tempDay > daysInMonth)
{
cout << "You entered a day outside the range." << endl;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
}
day = tempDay;
}
void Date::setMonth()
{
int tempMonth;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
while (tempMonth < 1 || tempMonth > 12)
{
cout << "You entered a month outside the range." << endl;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
}
month = tempMonth;
}
void Date::setYear()
{
int tempYear;
cout << "Please enter a year in the full number(Correct: 2005, Incorrect: 05): ";
cin >> tempYear;
year = tempYear;
}
void Date::displayWatch()
{
daysInMonthSwitch();
cout << "Date: " << month << "/" << day << "/" << year << endl;
//testPrint(); // Prints number of days in current month for testing only
//cout << "Month: " << month << endl; // Prints month for testing purposes
//inDay();
}
////////////// inDay function increments day and if conditions are met, increments month and year
void Date::inDay() //increments month and if day is more greater than number of days in month, call inMonth function
{
day++;
if (day > daysInMonth)
{
inMonth();
day = 1;
}
}// end function
void Date::inMonth() // increment month and if month is more greater than or equal to 12, call inYear function
{
month++;
if (month >= 12)
{
Date::inYear();
}
} // end function
void Date::inYear() // increment year
{
year++;
month = 1;
} // end function
//void Date::testPrint()
//{
// cout << "Days in month: " << daysInMonth << endl;
//} // for testing purposes only
int Date::daysInMonthSwitch() // Function contains switch that determines the number of days in the current month
{
//int month = m;
int result;
switch(month)
{
case 1:
result = 31;
break;
case 2:
result = 28;
break;
case 3:
result = 31;
break;
case 4:
result = 30;
break;
case 5:
result = 31;
break;
case 6:
result = 30;
break;
case 7:
result = 31;
break;
case 8:
result = 31;
break;
case 9:
result = 30;
break;
case 10:
result = 31;
break;
case 11:
result = 30;
break;
case 12:
result = 31;
break;
default :
cout << "Invalid Month" << endl;
}
daysInMonth = result;
return daysInMonth;
} // end daysInMonthSwitch function
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Time::Time()
{
const time_t currentTime = time(0);
const tm *localTime = localtime( ¤tTime );
setTime(localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
}
void Time::setHour()
{
int h;
cout << "Please enter the hour: ";
cin >> h;
hour = ( h >= 0 && h <= 24 ) ? h : 0;
}
void Time::setMin()
{
int m;
cout << "Please enter the minute: ";
cin >> m;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
}
void Time::setSec()
{
int s;
cout << "Please enter the second: ";
cin >> s;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
void Time::setTime( int h, int m, int s )
{ // validating time time, if incorrent, set to 0
hour = ( h >= 0 && h <= 24 ) ? h : 0;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
///void Time::tick();
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" <<
setfill('0') << setw(2) << minute << ":" << setw(2) << second
<< ( hour < 12 ? " AM" : " PM");
}
void Time::tick()
{
while (second >=0 && second <=60)
{
if (second < 59)
{
Sleep(1000);
second++;
}
else
{
Sleep(1000);
minute++;
second = 0;
if (minute >= 60)
{
hour++;
minute = 0;
if (hour >= 24)
hour = 0;
inDay();
}
}
}
};
///////////////////////////////////////////////////////////////////
Watch::Watch()
{}
////////////////////////////////////////////////////////////////// End Member Functions
int main()
{
Watch watch1;
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.setDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.inDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
return 0;
} // end main