using operators in class

The goal is to create a class operator that will compare the values of different birth dates.
"Overload operator > that takes a Birthdate, compares against the current date values (years are the most significant down to least significant day)"
I am a little lost as to how to use it after I have created the operator.
The goal is to use a sort function and the operator> to sort the data by date.

Also if someone could give me some guidance on the sort function. I dont have any getYear, getMonth or getDay methods anymore, but I believe there is a way to use the assignment class operator instead.
Any ideas?
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//header
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#endif
#include<string>
#include<iostream>
const int MAX_SIZE = 10;
using std::string;

class Birthday
{
private:
	int month;
	int day;
	int year;
	string name;
public:
	Birthday();
	string getName();
	bool operator==(Birthday&);
	bool operator>(Birthday&);
	void setBirthDate(int month, int day, int yr);
	void setName(string person);
	
};


void bsort(int[], int);
void prints(string message, int values[], int size);

//main
#include"Birthdate.h"
#include<iostream>
#include<fstream>

using namespace std;


int main()
{

	Birthday bdClub[MAX_SIZE];
	int month, day, year;
	string name;
	int size = 0;
	ifstream inFile("data.txt");
	if (inFile.fail())
	{
		cout << "Could not open data.txt\n";
		exit(1);
	}
	inFile >> month; //prime the pump
	while (!inFile.eof()){
		inFile >> day >> year >> name;
		//bdClub[size].setBirthdate(month, day, year);
		bdClub[size].setName(name);
		size++;
		inFile >> month; //try next birthdate
	}
	cout << "Unsorted\n";
	for (int x = 0; x < size; x++) // This loop does not display all of the array.
	{
		Birthday b = bdClub[x];
		
	}
	//bsort(bdClub, size);
	cout << "Sorted\n";
	for (Birthday b : bdClub){
	
	}
	system("pause");
	return 0;

//Implementation
  #include"Birthdate.h"


Birthday::Birthday()
{
	month = day = year = 0;
	name = "";
}
void Birthday::setName(string person)
{
	name = person;

}
string Birthday::getName()
{
	return name;

}
bool Birthday::operator==(Birthday& birthdate)
{
	if (day == birthdate.day && month == birthdate.month && year == birthdate.year)
		return true;
	else
		return false;
}
bool Birthday::operator>(Birthday& birthdate2)
{
	if (day > birthdate2.day && month > birthdate2.month && year > birthdate2.year)
		return true;
	else
		return false;
}
void Birthday::setBirthDate(int m, int d, int yr)
{
	if (m > 0 && m <= 12)
		month = m;
	if (d > 0 && d <= 31)
		day = d;
	if (yr > 0)
		year = yr;

}
void bsort(int values[], int size)
{
	bool swap;
	int temp;
	do
	{
		swap = false;
		for (int x = 0; x < size - 1; x++)
		{	//check for year >
			if (values[x] > values[x + 1] ||
				//check for year the same but month >
				(values[x].getYear() == values[x + 1].getYear() &&
				values[x].getMonth() > values.[x + 1].getMonth()) ||
				//check for year and month the same but day
				(values[x].getYear() == values[x + 1].getYear() &&
				values[x].getMonth() == values[x + 1].getMonth()) ||
				(values[x].getDay() > values[x + 1].getDay())
			{
				temp = values[x];
				values[x] = values[x + 1];
				values[x + 1] = temp;
				swap = true;
			};
		}
	} while (swap);
}
Last edited on
closed account (E0p9LyTq)
Overloading the comparison operators
http://www.learncpp.com/cpp-tutorial/96-overloading-the-comparison-operators/
You don't need getYear() and similar functions once you have a correct operator>.

Your operator>() isn't correct. In fact, it can't work in one line. You need to do the comparisons for year, month, and day separately. Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Birthday::operator>(Birthday& birthdate2)
{
   if(this->year > birthdate2.year)
   {
      return true;
   }
   elif(birthdate2.year > this->year)
   {
      return false;
   } // if the birthdays are in different years, then you can already tell which one comes first

   // similar for month and day after determining that the birthdays are in the same year

   return false; // can only be reached if (*this) == birthdate2
}


Then, the if-statement inside the for-loop of bsort() can just be if(values[x] > values[x+1]).
Ok so I changed around the logic of my operator function, as well as the bsort function.

But now the compiler is telling me that my Birthday class does not have a member "setBirthDate" when it clearly does. I have my prototype in the header file under public, and it is defined in my .cpp file with what it should do.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//header
#ifndef BIRTHDAY_H
#define BIRTHDAY_H
#endif
#include<string>
#include<iostream>
const int MAX_SIZE = 10;
using std::string;

class Birthday
{
private:
	int month;
	int day;
	int year;
	string name;
public:
	Birthday();
	string getName();
	bool operator==(Birthday&);
	bool operator>(Birthday&); 
	void setBirthDate(int month, int day, int yr);
	void setName(string person);
	
};


// .cpp filfe
#include"Birthdate.h"


Birthday::Birthday()
{
	month = day = year = 0;
	name = "";
}
void Birthday::setName(string person)
{
	name = person;

}
string Birthday::getName()
{
	return name;

}
bool Birthday::operator==(Birthday& birthdate)
{
	if (day == birthdate.day && month == birthdate.month && year == birthdate.year)
		return true;
	else
		return false;
}
bool Birthday::operator>(Birthday& birthdate2)
{
	if (this->year > birthdate2.year)
		return true;
	else if (birthdate2.year > this->year)
		return false;
	if (this->month > birthdate2.month)
		return true;
	else if (birthdate2.month > this->month)
		return false;
	if (this->day > birthdate2.day)
		return true;
	else if (birthdate2.day > this->day)
		return false;
}
void Birthday::setBirthDate(int m, int d, int yr)
{
	if (m > 0 && m <= 12)
		month = m;
	if (d > 0 && d <= 31)
		day = d;
	if (yr > 0)
		year = yr;

}
But now the compiler is telling me that my Birthday class does not have a member "setBirthDate" when it clearly does.

Show us the exact error message and where you call setBirthDate().

A better way to read the file is
while(inFile >> month >> day >> year >> name)
Each read returns the file stream, which casts to a bool indicating if there is more data in the file and if there are no errors in reading.

Also, the #endif line in your header file goes at the end of the file. This is essential for header files that are included in multiple source files.
Topic archived. No new replies allowed.