Im having a problem with my program

Im supposed to turn this function into a friend function but im having trouble doing it. I could only know how to convert the function declaration into a friend but i dont know how to do it to the function. Can someone please help me fix it or give me some advice on what i should be doing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream.h>
#include <conio.h>

class Date 
{
  private:
    int month;
    int day;
    int year;   
  public:
    Date(int = 7, int = 4, int = 2001);
    friend int operator==(const Date &);
};

Date::Date(int mm, int dd, int yyyy)
{
  month = mm;
  day = dd;
  year = yyyy;
}

int Date::operator==(const Date &date2)
{
  if(day == date2.day && month == date2.month && year == date2.year)
    return 1;
  else
    return 0;
}

int main ()
{
  Date a(4, 1, 1999), b(12, 18, 2001), c(4, 1, 1999);
  
  if (a == b)
    cout << "\nDates a and b are the same." << endl;
  else
    cout << "\nDates a and b are not the same." << endl;
  if (a == c)
    cout << "Dates a and c are the same.\n" << endl;
  else
    cout << "Dates a and c are not the same.\n" << endl;
 
  getch();
  return 0;
}
Last edited on
it should probably return a bool, not an int.

anyway, in order for it to be a friend, it has to be global. Which means it would need to take 2 parameter (one for the left, and one for the right)

so... bool operator == (const Date& l, const Date& r);
Topic archived. No new replies allowed.