代码之家  ›  专栏  ›  技术社区  ›  Denise Bruce

C++成员函数

  •  0
  • Denise Bruce  · 技术社区  · 7 年前

    我想写一个简单的程序来记录日期。此程序使用名为Date的结构。结构中有一个参数化的构造函数日期。 构造函数还确保日期大致有效(确保月份在1到12之间,天数在1到31之间)。稍后的分配解决了此类验证的问题。

    结构中还有一个add\u day函数。这就是我遇到的问题。我似乎无法调用函数的结构变量来添加日期。

    struct Date
    {
        int y, m, d;
    
      public:
        Date(int y, int m, int d);
    
        void add_day(int n);
    
        int month()
        {
            return m;
        }
    
        int day()
        {
            return d;
        }
    
        int year()
        {
            return y;
        }
    };
    
    /// main program calls the struct and add day function with parameters.
    int main()
    {
        Date today(1978, 6, 26);
        today.add_day(1);
    
        keep_window_open();
        return 0;
    }
    
    // Function definition for the Constructor. Checks values to make sure they
    are dates and then returns them.
    Date::Date(int y, int m, int d)
    {
    
        if ((m < 1) || (m > 12))
            cout << "Invalid Month\n";
        if ((d < 1) || (d > 31))
            cout << "Invalid Day\n";
        else
            y = y;
    
        m = m;
        d = d;
    
        cout << "The date is " << m << ',' << d << ',' << y << endl;
    
        // This function will accept the integers to make a date
        // this function will also check for a valid date
    }
    
    void Date::add_day(int n)
    {
        // what do I put in here that will call the variables in Date to be
        // modified to add one to day or d.
    };
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Acorn    7 年前

    您只需命名类的成员函数,即可在其成员函数中引用类的成员变量,例如:

    void Date::add_day(int n)
    {
        d += n;
    }
    

    在这里 d 将参考 int d 在代码段顶部声明的成员变量:

    struct Date
    {
        int y, m, d;
    
        // ...
    }
    

    然而,成员变量的阴影可能会让您感到困惑。此外,请注意,您还有其他设计问题和几种改进代码的方法。看看这个版本,可以获得一些灵感:

    #include <iostream>
    #include <stdexcept>
    
    class Date
    {
        int year_, month_, day_;
    
    public:
        explicit Date(int year, int month, int day)
            : year_(year), month_(month), day_(day)
        {
            if (month_ < 1 || month_ > 12)
                throw std::logic_error("Invalid Month");
            if (day_ < 1 || day_ > 31)
                throw std::logic_error("Invalid Day");
        }
    
        void add_days(int days) { /* ... */ }
    
        int year() const { return year_; }
        int month() const { return month_; }
        int day() const { return day_; }
    };
    
    std::ostream & operator<<(std::ostream & os, const Date & date)
    {
        return os << date.year() << '-' << date.month() << '-' << date.day();
    }
    
    int main()
    {
        Date date(1978, 6, 26);
        date.add_days(1);
        std::cout << date << '\n';
        return 0;
    }
    
        2
  •  -1
  •   h0od    7 年前

    成员变量在成员函数和构造函数中可以通过使用名称来隐式引用它们来访问( y, m, d ),或显式使用 this 指针( this->y, this->m, this->d )。

    例如,添加一天:

    void Date::add_day(int n)
    {
        d += n; // Modifies the 'd' member variable
        // ... below here we must handle what 
        // happens when the number of days exceeds t
        // he number of days in the particular month.
    };
    

    构造函数中也有问题。因为构造函数的参数变量与成员变量的名称相同。例如: m = m; d = d;

    通过这些赋值,编译器将假定您的意思是将局部参数变量赋值给局部参数变量。因此,实际上根本没有为成员变量赋值。一种解决方案是显式指定它们,如下所示: this->m = m; this->d = d; 这同样适用于 y 变量