Proper use of constructors

I am working on a project and have completed the code, which sorts names from an array alphabetically. The problem is, we are required to use a constructor but I am not sure why I would need a constructor for this program, much less how to incorporate it into the program. Any help or guidance is greatly appreciated, thank you.

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
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Names
{
private:
	ifstream inNames;

	string aNames[45];

public:

	//prototypes
	void openFile();
	void testFile();
	void readFile();
	void sortArray();
	void display();
	void closeFile();

	//constructor
	Names()				
	{
		//unsure about what I shoould put i here
	}

	void driver()
	{
		openFile();
		readFile();
		sortArray();
		display();
		closeFile();
	}
};

void Names::openFile()						//opens input file
{
	inNames.open("lineUp.txt");
}

void Names::readFile()						//reads the input file
{
	if (inNames.is_open())
	{
		cout << "Reading the input file...\n\n";
	}
	else
		testFile();
}

void Names::testFile()						//ensures that the input file is found/opened
{
	cout << "Unable to open input file";
	exit(1);
}

void Names::sortArray()
{
	int i, j;
	for (int i = 0; i < 45; i++)						//reads the input file into an array
	{
		string names;
		getline(inNames >> ws, names);
		aNames[i] = names;
	}

	//sorts the array
	for (int i = 0; i <= 45; i++)
	{
		for (j = i; j <= 45; j++)
		{
			if (aNames[i] > aNames[j])
			{
				swap(aNames[i], aNames[j]);
			}
		}
	}


}

void Names::display()
{
	cout << "The alphabetical list: \n";
	for (int i = 0; i < 45; i++)
	{
		cout << aNames[i] << endl;
	}
	cout << "\n\n\n\n";
}

void Names::closeFile()
{
	inNames.close();
}

int main()
{
	Names object;
	object.driver();
	system("pause");
	return 0;
}
Can you post the full assignment? It's hard to know what you're supposed to construct.

Also, lines 63-68 should be inside readFile()
Constructors are supposed to initialize object variables with values. You can create a get and set method to do the same thing. Maybe the answer to your question lies in "you have to know how to do it, rather than not being able to do/not doing at all."
Part of the problem is that there is no real need for a constructor in the code you supplied. All of the variables in your class don't really need to be "constructed" since the variable types have their own constructors already.

Also note that in C++ not everything should be contained within a class. In the case of the code you provided I would recommend that the ifstream be created in main() not inside the class itself.

I am going to echo jlb but assume you are new to all this... so 'why' ..
a class should mostly do one thing. your class appears to store a list of sorted names.
At a commercial/job in coding level, then, you might like it if your class could be given a list of names from {user input, a disk file, a network socket, a gui window, ...} and maintain a sorted list of names without knowing where they came from. Putting all that file I/O in ther e locks you to just this one task, and limits the ability to reuse it. Conversely, you often want to open disk files and check if that was successful, read the file into some sort of container, ... a class that did only that could be used for many things, and only need to be coded once. By doing 2 things in your class, you limit it severely. This is not critical for homework, but its an eye opener when you move on from early assignments.

If you know vector, consider a vector of strings instead of an array [45]. If not, keep it in mind for later: vectors are a c++ tool like arrays but they can grow to fit the data rather than be allocated to a fixed size and stuck there.

lets say you took the advice and made a file reading class ...
your constructor could take a string and open the file by that name, just like the IO stream constructor does:
myfileclass("file.txt");
that could be a useful constructor example to think about.
Topic archived. No new replies allowed.