Question: Programming Strategies After you create your class and struct definitions in the header file, create a separate file called exerciseImplemenation.cpp or implementation.cpp, or some other
Programming Strategies
After you create your class and struct definitions in the header file, create a separate file called exerciseImplemenation.cpp or implementation.cpp, or some other descriptive name. Place all of the member functions in this file. Then create another source code file called exerciseMain.cpp, or just main.cpp. Place the main function in this file. You will have to include your header file in both of the source code files in order to get the project to compile correctly. Suppose the name of your header file is exerciseHeader.hpp (or you can use the extension .h). Then to include the header file in both source code files, use:
#include exerciseHeader.hpp // (or exerciseHeader.h)
at the top of both source code files. Notice the double quotes there instead of angled brackets (<>). The double quotes direct g++ to look inside of the current directory to find the header file. Remember that because the header file is included in the source code files, it is not necessary to list the header file on the command line during compilation. So, to compile your project, it may look like this:
g++ main.cpp implementation.cpp o exerciseProgram
Remember that headers for member functions have to include the name of the class and the scope resolution operator. So the loadData() method header might look like:
int exerciseJournal::loadData()
Remember that all class members are accessible inside of member functions. So even though countAndIndex is private, its not private inside of loadData(), because loadData() is a member function. So inside of your member functions, you can access all private members directly. Inside of loadData(), set countAndIndex equal to zero, and increment it when you load an exercise, just like normal: countAndIndex = 0; countAndIndex++; Notice that loadData() still returns a value, so you can say how many exercises were loaded when control returns back to main() (dont do any output inside of loadData; no cout statements there). So loadData() should return countAndIndex.
Once you have created your exerciseJournal class and implementation, you will have to instantiate it, or create an exerciseJournal object, in the main function. Then you will use the object to call the member functions. See the textbox to the right and above for an example.
Notice that I have placed the fileName c-string inside of the class, along with the countAndIndex member variable. I did this so that the file name will be accessible to all of the member functions that need it, such as loadData() and writeData(). Since the file name is part of the class, it wont have to be passed as an argument. You may add any other member variables to the class that you feel you need, such as the fstream object, other c-strings, counting variables, or whatever, but make sure you add them as private members. You may also create other member functions if you find that you need them. For example, if you wish to have access to the countAndIndex value in main, you will need a getter (accessor) function for it. So the prototype (inside of the class) would be: int getCountAndIndex(); and the header would be:
int exerciseJournal::getCountAndIndex()
Remember that any member functions that you want the client (main function in this case) to use must be declared as public, so if you create any, put the getter functions after the public label.
As mentioned above, you have two options for reading and writing to the same file. The first (simpler) option is to open the file in loadData using an ifstream, read all the data into memory, and then close the file. At program termination, open the file using an ofstream, write all of the data in memory back to the file, and then close the file. Remember that when you open a file for output, if the file already exists, all of the data in the file is deleted. Your second option is to make the file object a member of the class, and then open the file for reading and appending. That way, you can open the file in loadData(), read all of the data, and then leave the file open. Then at program termination, just write the new data to the file. So if the user enters new exercise data, only that new data should be written to the file. If the user doesnt enter new data, then nothing new gets written to the file. Remember to close all files either way. To open a file for reading and appending, create a file object and open it with the ios::in and ios::app options:
fstream file;
// ask the user for the fileName.
file.open(fileName, ios::in | ios::app); // one pipe (|) for bitwise or.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
