简体   繁体   English

C ++成员函数

[英]C++ Member Functions

I am trying to write a simple program that records the date. 我正在尝试编写一个简单的程序来记录日期。 This program uses a Struct called Date. 该程序使用名为Date的结构。 Within the Struct is a parameterized constructor Date. Struct中有一个参数化的构造函数Date。 The constructor also makes sure the date is roughly valid (makes sure months are between 1 and 12, and days are between 1 - 31). 构造函数还确保日期大致有效(确保月份在1到12之间,天在1到31之间)。 A later assignment addresses the issues with this type of validation. 以后的任务解决了这种验证类型的问题。

Also within the Struct is an add_day function. 同样在Struct中的是add_day函数。 This is where I am having issues. 这就是我遇到的问题。 I cannot seem to call the struct variables to the function to add the day. 我似乎无法将struct变量调用到函数中以添加日期。

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.
};

You can refer to the member variables of a class inside its member functions simply by naming them, eg: 您可以通过简单地命名类的成员函数来引用类的成员变量,例如:

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

Here, d would refer to the int d member variable that you declared on top of your snippet: 在这里, d会引用您在代码段顶部声明的int d成员变量:

struct Date
{
    int y, m, d;

    // ...
}

However, it is the shadowing of the member variables that is probably confusing you. 但是,成员变量的阴影可能使您感到困惑。 Also, please note that you have other design issues and several ways that you can improve your code. 另外,请注意,您还有其他设计问题,还有几种改进代码的方法。 Take a look at this version for some inspiration: 看看这个版本的一些启示:

#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;
}

The member variables are accessible in the member functions and constructor implicitly by referring to them with their name ( y, m, d ), or explicitly with this pointer ( this->y, this->m, this->d ). 成员变量可以在成员函数和构造函数中隐式访问,方法是使用它们的名称( y, m, d )隐式引用它们,或使用this指针显式地引用它们( this->y, this->m, this->d )。

So for example to add a day: 因此,例如添加一天:

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.
};

You also have a issue in the constructor. 您在构造函数中也有问题。 Because the argument variables to the constructor share the same names with the member variables. 因为构造函数的参数变量与成员变量共享相同的名称。 For example: m = m; d = d; 例如: m = m; d = d; m = m; d = d;

With these assignments, the compiler will assume you mean to assign the local argument variable to the local argument argument variable. 有了这些分配,编译器将假定您要将局部参数变量分配给局部参数变量。 So you are actually not assigning the member variable any value at all. 因此,您实际上根本没有为成员变量分配任何值。 One solution is to to explicitly specify them as follows: this->m = m; this->d = d; 一种解决方案是显式指定它们,如下所示: this->m = m; this->d = d; this->m = m; this->d = d; The same goes for the y variable. y变量也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM