C++ C++;从其他类成员函数调用成员函数

C++ C++;从其他类成员函数调用成员函数,c++,C++,我正在写一个简单的程序来模拟手表。上课日期,上课时间,上课时间。手表是从日期和时间派生出来的。试图从时间成员函数调用日期成员函数。它说没有目标我做不到。我正在学习,我相信这大部分都是废话哈哈。任何帮助都将受到感谢。基本上在Time::Tick的末尾,我试图调用类Date成员函数inDay。从另一个类成员函数调用一个成员函数。我不知道如何从另一个类中执行友元函数。我试图使我的函数保持静态,但这会导致我走上一条漫长的错误之路。有什么建议吗 #include <iostream> #inc

我正在写一个简单的程序来模拟手表。上课日期,上课时间,上课时间。手表是从日期和时间派生出来的。试图从时间成员函数调用日期成员函数。它说没有目标我做不到。我正在学习,我相信这大部分都是废话哈哈。任何帮助都将受到感谢。基本上在Time::Tick的末尾,我试图调用类Date成员函数inDay。从另一个类成员函数调用一个成员函数。我不知道如何从另一个类中执行友元函数。我试图使我的函数保持静态,但这会导致我走上一条漫长的错误之路。有什么建议吗

#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
#包括
#包括
#包括
使用名称空间std;
上课日期
{
公众:
日期();
void inDay();//递增日,测试月是否递增,如果为真,则递增
void inMonth();//递增月份,测试年份是否递增,如果为true,则递增
void inYear();//增量年
void displayWatch();//调用daysinmonthwith,调用inDay,显示日期
int daysInMonthSwitch();//根据使用开关的月份确定月份中的天数
//void testPrint();//仅用于测试目的
void setDay();
void setMonth();
无效设置年份();
私人:
国际日;
整月;
国际年;
int daysInMonth;
};
上课时间{
公众:
时间();
void setTime(int,int,int);//设置小时、分钟、秒
void printStandard();//以标准时间格式打印时间12h
void setHour();
void setMin();
void setSec();
无效勾号();
私人:
整小时;
整数分钟;
int秒;
};
班级值班:公共时间,公共日期{
公众:
手表();
int getDate();
无效设置时间(int,int,int);
int getTime();
私人:
};
//上课时间::
日期::日期()://类构造函数
第(27)天,
第(2)个月,
年份(2013年),
日每月(31)
{            
}//结束构造函数
作废日期::设置日期()
{
int tempDay;

cout你的类
Time
不是从
Date
派生出来的。它不能对可能也是子类父类的类调用方法。想象一下,如果你单独实例化一个
Time
。它将在什么
Date
上运行


一个方法需要在
Watch
中同时看到时间和日期,然后再增加
Time
直到滚动为止。

你的类
Time
并不是从
Date
派生出来的。它不能对可能也是子类父类的类调用方法。想象一下如果你在单独设定了一个
时间
。它将在什么
日期
上运行


一种方法,它增加
时间
直到它滚动,然后增加
日期
需要在
观察
中,它可以同时看到这两种情况。

请看一下这篇文章,了解Stackoverflow站点的使用情况以及如何提问。您可能还想熟悉A的概念。请看一下用于Stackoverflow站点的使用以及如何提问。您可能还想熟悉a的概念。谢谢。我会尝试一下。作为一个变体:方法tick可以有一个日期类型的参数作为指针或引用。但是在watch中控制这两个参数是更好的解决方案。谢谢。我会尝试。作为一个变量选项:方法tick可以有一个Date类型的参数作为指针或引用。但是在watch中控制两者是更好的解决方案。