Denise Bruce
Denise Bruce

Reputation: 13

C++ Member Functions

I am trying to write a simple program that records the date. This program uses a Struct called Date. Within the Struct is a parameterized constructor 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). A later assignment addresses the issues with this type of validation.

Also within the Struct is an add_day function. This is where I am having issues. I cannot seem to call the struct variables to the function to add the 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.
};

Upvotes: 0

Views: 80

Answers (2)

h0od
h0od

Reputation: 69

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).

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;

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; The same goes for the y variable.

Upvotes: -1

Acorn
Acorn

Reputation: 26066

You can refer to the member variables of a class inside its member functions simply by naming them, e.g.:

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:

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

Upvotes: 2

Related Questions