首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++成员函数

C++成员函数
EN

Stack Overflow用户
提问于 2018-04-29 15:16:25
回答 2查看 73关注 0票数 0

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

结构中还有一个add_day函数。这就是我遇到麻烦的地方。我似乎不能调用函数的struct变量来添加日期。

代码语言:javascript
复制
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.
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-29 15:43:29

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

例如,添加一天:

代码语言:javascript
复制
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变量也是如此。

票数 -1
EN

Stack Overflow用户

发布于 2018-04-29 15:42:51

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

代码语言:javascript
复制
void Date::add_day(int n)
{
    d += n;
}

在这里,d将引用您在代码段之上声明的int d成员变量:

代码语言:javascript
复制
struct Date
{
    int y, m, d;

    // ...
}

但是,成员变量的隐藏可能会使您感到困惑。此外,请注意,您有其他设计问题和几种方法,您可以改进您的代码。看看这个版本,可以得到一些启示:

代码语言:javascript
复制
#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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50088117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档