Class Inheriting with Vectors

I just finished a C++ class and we ended with basic inheriting and vectors with one class/struct, and I'm trying to combine the two. The code below, in a nutshell, is one class for customer information (first and last name, and phone number) and address information (street, city, state, zip). I can get the vector working on just the customer, and I can get the main program to input the address information, but I'm trying to figure out how to include the address information into the vector. I'm trying to get to a point where the user would enter in (or pull from a text file) a number of customers and their addresses, and the program would sort everything using a system like the CompareCustomer in the customer.h file (sorting by city, state, then going through last name, first, then running through the phone number). I'm sorry the code is so long, its actually spread over 5 files. Thank you for any assistance

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
[code]//customer.h
#pragma once
#include <string>
using namespace std;

class Customer
{
private:
	string firstName, lastName;
	string areacode, prefix, number;

public:
	Customer(void);
	Customer(string, string, string, string, string);
	~Customer(void);

	void setFirstName(string);
	string getFirstName(void);
	void setLastName(string);
	string getLastName(void);
	void setAreaCode(string);
	string getAreaCode(void);
	void setPrefix(string);
	string getPrefix(void);
	void setNumber(string);
	string getNumber(void);
	void displayCustInfo(void);
};

class CompareCustomer
{
public:
	bool operator()(Customer& c1, Customer& c2)
	{
		if (c1.getAreaCode().compare(c2.getAreaCode()) < 0)
			return false;
		else if (c1.getAreaCode().compare(c2.getAreaCode()) > 0)
			return true;
		else if ((c1.getAreaCode().compare(c2.getAreaCode()) == 0) && (c1.getPrefix

().compare(c2.getPrefix()) < 0))
			return false;
		else if ((c1.getAreaCode().compare(c2.getAreaCode()) == 0) && (c1.getPrefix

().compare(c2.getPrefix()) > 0))
			return true;
		else if ((c1.getPrefix().compare(c2.getPrefix()) == 0) && (c1.getNumber().compare

(c2.getNumber()) < 0))
			return false;
		else if ((c1.getPrefix().compare(c2.getPrefix()) == 0) && (c1.getNumber().compare

(c2.getNumber()) > 0))
			return true;
		else if ((c1.getNumber().compare(c2.getNumber()) == 0) && (c1.getLastName

().compare(c2.getLastName()) < 0))
			return false;
		else if ((c1.getNumber().compare(c2.getNumber()) == 0) && (c1.getLastName

().compare(c2.getLastName()) > 0))
			return true;
	}
};

//customer.cpp
#include "stdafx.h"
#include "customer.h"
#include <iostream>
using namespace std;

Customer::Customer(void)
{
	firstName = "";
	lastName = "";
	areacode = "";
	prefix = "";
	number = "";
}

Customer::Customer(string fn, string ln, string ac, string pr, string nm)
{
	firstName = fn;
	lastName = ln;
	areacode = ac;
	prefix = pr;
	number = nm;
}

Customer::~Customer(void)
{
	//Clears customer
}

void Customer::setFirstName(string fn)
{
	firstName = fn;
}

string Customer::getFirstName(void)
{
	return firstName;
}
//There are another 4 sets of code here that work like the
//firstname above, getting and setting lastName, areacode,
//prefix, and number

//address.h
#pragma once
#include "customer.h"
#include <string>
using namespace std;

class Address : public Customer
{
private:
	string street;
	string city;
	string state;
	string zipcode;

public:
	Address(void);
	Address(string, string, string, string, string);
	~Address(void);

	void setStreet(string);
	string getStreet(void);
	void setCity(string);
	string getCity(void);
	void setState(string);
	string getState(void);
	void setZipCode(string);
	string getZipCode(void);
	void displayAddressInfo(void);
};

//address.cpp
#include "stdafx.h"
#include "address.h"
#include <iostream>
using namespace std;

Address::Address(void) : Customer()
{
	street = "";
	city = "";
	state = "";
	zipcode = "";
}

Address::Address(string fn, string ln, string ac, string pr, string nm)
	: Customer(getFirstName(), getLastName(), getAreaCode(), getPrefix(), getNumber())
{
	street = "";
	city = "";
	state = "";
	zipcode = "";
}

Address::~Address(void)
{
	//Clears Address
}

void Address::setStreet(string str)
{
	street = str;
}

string Address::getStreet(void)
{
	return street;
}
//There are three more blocks like the two street ones
//above, setting and getting city, state, and zip code

//mainprogram.cpp
// SingleClassVectorArray.cpp

#include "stdafx.h"
#include "customer.h"
#include "address.h"
#include <iomanip>
#include <iterator>
#include <iostream>
#include <string>
#include <queue>
using namespace std;

void main()
{
	string input = "";
	priority_queue<Customer, vector<Customer>, CompareCustomer> pq;
	Customer customerArray[3];
	Address addressArray[3];
	for (int ctr = 0; ctr < 3; ctr++)
	{
		cout << "Enter the first name of customer " << ctr + 1 << ": ";
		getline(cin, input);
		customerArray[ctr].setFirstName(input);
		cout << "Enter the last name of customer " << ctr + 1 << ": ";
		getline(cin, input);
		customerArray[ctr].setLastName(input);
		cout << "Enter the area code for customer " << ctr + 1 << ": ";
		getline(cin, input);
		customerArray[ctr].setAreaCode(input);
		cout << "Enter the prefix for customer " << ctr + 1 << ": ";
		getline(cin, input);
		customerArray[ctr].setPrefix(input);
		cout << "Enter the number for customer " << ctr + 1 << ": ";
		getline(cin, input);
		customerArray[ctr].setNumber(input);
		cout << "Enter the street address for customer " << ctr + 1 << ": ";
		getline(cin, input);
		addressArray[ctr].setStreet(input);
		cout << "Enter the city for customer " << ctr + 1 << ": ";
		getline(cin, input);
		addressArray[ctr].setState(input);
		cout << "Enter the state for customer " << ctr + 1 << ": ";
		getline(cin, input);
		addressArray[ctr].setCity(input);
		cout << "Enter the zip code for customer " << ctr + 1 << ": ";
		getline(cin, input);
		addressArray[ctr].setZipCode(input);
		system("CLS");
	}
	vector <Customer> customers(begin(customerArray), end(customerArray));
	vector<Customer>::iterator customerItr;
	for (customerItr = customers.begin(); customerItr != customers.end(); customerItr++)
		pq.push(*customerItr);
	while (!pq.empty())
	{
		Customer c2 = pq.top();
		cout << c2.getFirstName() << " " <<
			setw(10) << left << c2.getLastName() << " " << "(" <<
			setw(3) << left << c2.getAreaCode() << ") " << c2.getPrefix() << "-" << 

c2.getNumber() << endl << endl;
		pq.pop();
	}
}

Last edited on
Put the address on the customer, not the customer on the address. But maybe I have missed something.
Topic archived. No new replies allowed.