Purpose: Create a C++ class; use operators, overloading, member functions, friend functions, constructors and private data.

• Make the data members private. (This means that the non-member functions below will have to be friends of the Data).
• Create a member function called ‘set’ with the same arguments, and return type void; and create a constructor with the same arguments.
Note that the member function accesses the data members of the object that called it, and a constructor sets the data members of the Date object being created.

A member function is called like ‘d.set(12,31,1999)’. A constructor is not called like a member function. It is used to create an object, which did not yet exist, either in a variable declaration, “Date d(12,31,1999);’

• Create operator << to output a date to an ostream.
• Write operator++() and operator++(int) as member functions that do the same thing—change the value that is stored in the object of that called them, so that the object contains a date one day later.
In addition to doing this, they also return a value. The operator with no formal arguments is called as ‘++x’, and returns the value that is stored in the date after it is incremented. The one with a dummy int argument is called as ‘x++’, and returns the value that was stored in the date before it was incremented. Thus,
Date a(2,7,2001), b(1,1,2001), c, d;
c = a++; d = ++b;
cout << a << “,” << c << “,” << b << “,” << d;

will print “February 8, 2001, February 7, 2001, January 2, 2001, January 2, 2001”.

• Write operator==, so that ‘a==b’ returns true if two dates are the same and false otherwise. It should not be a member function. (use friend function).
Finally, write a main function that tests all of the functions you wrote. If a function has a return value, you must demonstrate that the return value is correct. If a function is supposed to change the value stored in a variable, you must test that it did so. Be careful to test both of these properties for both operator++’s.

//Output:
Default constructor: January 1, 2000
3 argument constructor: February 27, 2000
d2++ returns February 27, 2000 and changes d2 to February 28, 2000
++d2 returns February 29, 2000 and changes d2 to February 29, 2000
January 1, 2000, != February 29, 2000
January 1, 2000 == January 1, 2000.

This is what I have so far:

#include <iostream>
#include <iomanip>
using namespace std;
class date {
    friend ostream &operator << (ostream&, const date &);
    friend istream &operator >> (istream&, date &);

private:
    date ();
    void set ();
};

ostream &operator << (ostream &output, const date &num)
{

    return output;
}

istream &operator >> (istream &input, date &num)
{

    return input;
}
date::date()
{
// I have no idea what goes in the constructor
}
int main()
{

    return 0;
}

// I have no idea what goes in the constructor

Do you have some idea what the members of the class will be? So far, you haven't specified any. Also, your constructor and set() methods should not be private, otherwise there's no way for any code outside of the class to call them. The data members should be private, once you include them, as specified in the assignment. If you aren't clear on the difference between members and methods, go back to your notes from the very first course session. The following questions should get you going a bit more:

  • What is a date?
  • What does a date include? (these will become members of the class)
  • What does it mean?
  • How is it represented?
  • What can you do to/with a date? (these will beomce methods of the class, and friend functions)
  • How do you "increment" a date to the next date? In particular, how do you increment "January 31, 2012"?

You may also wish to implement additional getter and setter methods that allow you to retrieve / modify individual pieces of a date.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.