2013-04-15 2 views
1

나는 시계를 시뮬레이트하는 간단한 프로그램을 작성 중이다. 수업 일, 수업 시간, 수업 시간. 시계는 날짜와 시간에서 파생됩니다. 시간 멤버 함수에서 날짜 멤버 함수를 호출하려고합니다. 그것은 물체없이 그것을 할 수 없다고 말합니다. 나는 배우고있다. 그리고 나는 이것이 쓰레기 haha ​​다라고 확신한다. 도움이되면 도움이 될 것입니다. 기본적으로 Time :: Tick의 끝에서 Class Date 멤버 함수를 inDay에 호출하려고합니다. 다른 클래스 멤버 함수에서 하나의 멤버 함수를 호출합니다. 다른 클래스에서 친구 기능을 수행하는 법을 모릅니다. 내 기능을 정적으로 만들려고했지만 오류가 길어지는 단점이 있습니다. 어떤 충고?다른 클래스 멤버 함수의 C++ 멤버 멤버 함수

#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

Stackoverflow 사이트 사용법 및 질문하는 방법은 [FAQ] (http://stackoverflow.com/faq)를 참조하십시오. [Short Self Contained/Compilable Example - SSCCE] (http://sscce.org/)의 개념에 익숙해지기를 원할 수도 있습니다. –

답변

3

클래스 TimeDate에서 내려하지 않습니다. 자식 클래스의 부모가 될 수도있는 클래스의 메서드를 호출 할 수 없습니다. Time 만 인스턴스화 한 경우를 상상해보십시오. 어떤 Date이 작동합니까? 롤오버 후 모두 볼 수있는 곳에 Watch에있을 필요가 Date를 증가 할 때까지 Time를 증가

방법.

+0

고맙습니다. 나는 그것을 시도 할 것이다. –

+0

변형 : 메소드 틱은 Date 유형의 인수를 포인터 또는 참조로 가질 수 있습니다. 그러나 둘 다 통제하는 것이 더 나은 해결책입니다. – scones

+0

그 작품! 도와 줘서 고마워. –

관련 문제