C ++从不同的类成员函数调用成员函数

时间:2013-04-15 22:28:54

标签: c++

我正在编写一个简单的程序来模拟手表。课程日期,课堂时间,课堂观看。 Watch来自日期和时间。尝试从Time成员函数调用日期成员函数。它说我不能没有一个物体。我正在学习,我相信大部分都是垃圾哈哈。任何帮助都会得到满足。基本上在Time :: Tick结束时,我试图在Day中调用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 个答案:

答案 0 :(得分:4)

您的班级Time不会来自Date。它不能调用可能恰好是子类父类的类。想象一下,如果你单独实例化TimeDate会对其进行操作?

一种递增Time直到它翻转然后递增Date的方法需要在Watch中才能看到它们。