Initialize an array of objects and show them on screen

Hello. I need to make an array (in the main function) of objects (class Person), initialize them and show them on the screen with the operator in line 129. But right now it only shows like this:
Name:   Date of birth: 0/0/0
Name:   Date of birth: 0/0/0
Name:   Date of birth: 0/0/0
Name:   Date of birth: 0/0/0
Name:   Date of birth: 0/0/0


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
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <string>

using namespace std;

class Birth_date
{
private:
	unsigned int day;
	unsigned int month;
	unsigned int year;

public:
	Birth_date() { day = 0; month = 0; year = 0; }
	
	Birth_date(int day, int month, int year)
	{
		this->day = day;
		this->month = month;
		this->year = year;
	}
	
	Birth_date(const Birth_date& birth) 
	{
		day = birth.day;
		month = birth.month;
		year = birth.year;
	}

	void set_day(int day)
	{
		this->day = day;
	}
	void set_month(int month)
	{
		this->month = month;
	}
	void set_year(int year)
	{
		this->year = year;
	}

	int get_day()
	{
		return day;
	}
	int get_month()
	{
		return month;
	}
	int get_year()
	{
		return year;
	}

	friend bool operator == (const Birth_date& a, const Birth_date& b)
	{
		return (a == b);
	}
	bool operator < (const Birth_date& bd)
	{
		int i1 = (bd.day, bd.month, bd.year);
		int i2 = (bd.day, bd.month, bd.year);

		return (i1 < i2);
	}
	bool operator > (const Birth_date& bd)
	{
		int i1 = (bd.day, bd.month, bd.year);
		int i2 = (bd.day, bd.month, bd.year);

		return (i1 > i2);
	}
	
	ostream& Output(ostream& out)
	{
		out << "Date of birth: " << day << "/" << month << "/" << year << endl;
		return out;
	}
};

class Person
{
private:
	string name;
	Birth_date dob;

public:
	Person() { name = "N/A"; dob = Birth_date(0, 0, 0); }

	Person(string, Birth_date) {}

	Person(string name, Birth_date& dob)
	{
		this->name = name;
		this->dob = dob;
	}

	Person(const Person& p)
	{
		name = p.name;
		dob = p.dob;
	}

	string get_name()
	{
		return name;
	}
	Birth_date get_dob()
	{
		return dob;
	}

	bool operator < (const Person& p)const
	{
		Birth_date bd1 = p.dob;
		Birth_date bd2 = p.dob;

		return (bd1 < bd2);
	}
	bool operator == (const Person& p)const
	{
		Birth_date i1 = p.dob;
		Birth_date i2 = p.dob;

		return (i1 == i2);
	}

	const ostream& output(ostream& out)
	{
		out << "Name: " << name << "\t" ;
		dob.Output(out);
		return out;
	}

};

int main()
{
	Person students[5] = { Person("Ivan Petkov",Birth_date(10,6,1999)),
	                       Person("Gabe Trent",Birth_date(20,12,1996)),
	                       Person("Maggy Sommer",Birth_date(5,2,2000)),
	                       Person("Cameron Dallas",Birth_date(1,4,2001)),
	                       Person("Catherine Crumb",Birth_date(28,8,2000)) };

	for (int i = 0; i < 5; i++)
	{
		students[i].output(cout);
	}

	return 0;
}
Last edited on
Have you tried debugging this?
Your constructor at line 91 is being invoked, which does not set anything.
You're probably expecting the constructor at line 93 to be invoked.

Lines 140-144 pass Birth_date by value.
Your constructor at line 93 is expecting Birth_date to be passed by reference.
Thank you. I wrote the constructors again to be safe and got rid of the reference, such a simple error.
Topic archived. No new replies allowed.