2013-04-15 82 views
1

我在寫一個簡單的程序來模擬一個手錶。班級日期,班級時間,班級觀察。手錶來自日期和時間。嘗試從Time成員函數中調用日期成員函數。它說我無法做到沒有物體。我正在學習,我相信大部分這是廢話哈哈。任何幫助將被appriciated。基本上在Time :: Tick結束時,我試圖在inDay中調用Class Date成員函數。從另一個類成員函數調用一個成員函數。我不知道如何從另一個班級做一個朋友功能。我試圖讓我的功能是靜態的,但是它導致我走了一條錯誤的漫長道路。有什麼建議?從不同的類成員函數調用成員函數

#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(&currentTime); 
    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 
+1

請查看此[常問問題](http://stackoverflow.com/faq)以瞭解如何使用Stackoverflow站點以及如何提出問題。你可能還想熟悉一個[簡短的自包含正確/可編譯示例 - SSCCE]的概念(http://sscce.org/)。 –

回答

3

Time類不從Date下降。它不能調用可能碰巧是子類父類的類。想象一下,如果你單獨實例化了一個TimeDate會在什麼樣的基礎上運作?

增加Time直到它翻轉然後增量Date需要在Watch中可以看到兩者的方法。

+0

謝謝。我會嘗試。 –

+0

作爲變體:方法tick可以具有Date類型的參數作爲指針或引用。但控制兩者都是更好的解決方案。 – scones

+0

這工作!感謝您的幫助。 –