C++
Lab 14: Multiple files
Attached Files:
·
lab14.cpp (953 B)
The attached file (lab14.cpp) contains a program that includes a Date class, a Time class, and a main-function. This program should be separated into five files and submitted to turnin. A primer on how to do this is below.
Primer for breaking an application into multiple files.
Attached Files:
· Student.cpp (497 B)
· test_student.cpp (166 B)
· Student.h (305 B)
Two rules that must never be broken:
· Never, ever #include a source (.cpp) file
· Never, ever use g++ to compile a header (.h) file
Very roughly speaking, there are three components of an application that should be divided into separate files:
1. each class declaration is put into its own header (.h) file
2. each class definition is put into its own source (.cpp) file
3. other functions, including the main-function are put into a source (.cpp) file
Here is a sample application in a single file that includes one class. This application will be used as an example in this primer:
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 //Student CLASS DECLARATION
6 class Student {
7 public:
8 Student(string nm, string yr, float g); //constructor
9 void boost_grade();
10 void display();
11
12 private:
13 string name;
14 string year;
15 float gpa;
16 };
17
18 //Student CLASS DEFINITION
19 Student::Student(string nm, string yr, float g) //constructor
20 : name(nm), year(yr), gpa(g) //initialization list
21 {
22 }
23
24 void Student::boost_grade()
25 {
26 gpa+= 1.0;
27 if(gpa > 4.0)
28 gpa= 4.0;
29 }
30
31 void Student::display()
32 {
33 cout << "Student Record" << endl;
34 cout << "--------------" << endl;
35 cout << "Name: " << name << endl;
36 cout << "Academic Level: " << year << endl;
37 cout << "GPA: " << gpa << endl;
38 }
39
40 //OTHER FUNCTIONS, INCLUDING THE MAIN-FUNCTION
41 int main()
42 {
43 Student s( "Bobby Teenager", "Sophomore", 2.5);
44 s.boost_grade();
45 s.display();
46 }
Step 1, partition program into multiple files
The Student class declaration is on lines 5-16. These lines should go into a header file named after the class: Student.h
The Student class definition is on lines 18-38. These lines should go into a source file named after the class: Student.cpp
The other functions (if any), including main are on lines 40-46. These should go into a file with an application appropriate name. test_student.cpp
The easiest method for breaking test_student.cpp into multiple files is to simply make a copy for each needed file. Then, edit each copy, deleting unneded lines. For example, the above file is already named test_student.cpp. Use cp make copies for the other two files:
cp test_student.cpp Student.cpp
cp test_student.cpp Student.h
Edit each of the files deleting the portions that don't belong. So, for example, edit Student.cpp and delete all lines before line 18 and after line 38.
Step 2, add preprocessor directives to each header file
Preprocessor directives must surround the contents of each header file (this example ony has one header file). For Student.h the following should be added as the first two lines in the file:
#ifndef STUDENT_H
#define STUDENT_H
As shown provide the filename but use uppercase letters and replace periods with underscores.
The last line in the header file should be:
#endif
Step 3, add #include statements in each file as required
In the above example each file needs the following #include statements:
1. Student.h
a. #include<string> because strings are used in this file
2. Student.cpp
a. #include"Student.h" because the functions of the Student class are defined here. Also, note the double quotes which specify "look for this file in the current directory"
b. #include<iostream> because cout statements are used in this file
c. #include<string> because strings are used in this file
3. test_student.cpp
a. #include"Student.h" because the Student class is used in this file. Again, note the double quotes which specify "look for this file in the current directory"
Step 4, compile the application by putting all .cpp files on the command line
g++ Student.cpp test_student.cpp -o student_app
Step 5, don't forget the two rules that must never be broken
· Never, ever #include a source (.cpp) file
· Never, ever use g++ to compile a header (.h) file