Week 14 Final Hearder Files & Separate Interfacing
Week 14 Final Hearder Files & Separate Interfacing
OOP’S
HEADER FILES,
PLACING SEPARATE
INTERFACING
Placing a Class in a Separate File for
Reusability
When we have successfully developed a class XYZ as far as we
need to for now from a programming perspective, so let's
consider some software engineering issues. One of the benefits
of creating class definitions is that, when packaged properly,
our classes can be reused by programmers potentially
worldwide. For example, we can reuse C++ Standard Library
type string in any C++ program by including the header file
<string> in the program (and, as we will see, by being able to
link to the library's object code).
Header Files
Each of the previous examples in the chapter consists of a
single .cpp file, also known as a source-code file that contains a
GradeBook class definition and a main function. When building
an object-oriented C++ program, it is customary to define
reusable source code (such as a class) in a file that by
convention has a .h filename extension known as a header file.
Programs use #include preprocessor directives to include
header files and take advantage of reusable software
components, such as type string provided in the C++
Standard Library and user-defined types like class
GradeBook.
Reference: [Page 96] OF THE BOOK C++ HOT TO PROGRAM (DIETEL & DIETEL)
In our next example, we separate the code from Fig. 3.7 into
two files GradeBook.h (Fig. 3.9) and fig03_10.cpp (Fig. 3.10). As
you look at the header file in Fig. 3.9, notice that it contains
only the GradeBook class definition (lines 1141) and lines 38,
which allow class GradeBook to use cout, endl and type string.
The main function that uses class GradeBook is defined in the
source-code file fig03_10.cpp (Fig. 3.10) at lines 1021. To help
you prepare for the larger programs you will encounter later in
this book and in industry, we often use a separate source-code
file containing function main to test our classes (this is called a
driver program). You will soon learn how a source-code file
with main can use the class definition found in a header file to
create objects of a class.
The compiler knows what fundamental data types like int are,
the compiler does not know what a GradeBook is because it is a
user-defined type. In fact, the compiler does not even know the
classes in the C++ Standard Library. To help it understand how
to use a class, we must explicitly provide the compiler with the
class's definition that's why, for example, to use type string, a
program must include the <string> header file. This enables the
compiler to determine the amount of memory that it must
reserve for each object of the class and ensure that a program
calls the class's member functions correctly.
Figure 3.10. Including class GradeBook from file
GradeBook.h for use in main.
1 // Fig. 3.10: fig03_10.cpp
2 // Including class GradeBook from file GradeBook.h for use in
main.
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "GradeBook.h" // include definition of class
GradeBook
8
9 // function main begins program execution
10 int main()
11 {
12 // create two GradeBook objects
13 GradeBook gradeBook1( "CS101 Introduction to C++
Programming" );
14 GradeBook gradeBook2( "CS102 Data Structures in C++" );
15
16 // display initial value of courseName for each GradeBook
17 cout << "gradeBook1 created for course: " <<
gradeBook1.getCourseName()
18 << "\ngradeBook2 created for course: " <<
gradeBook2.getCourseName()
19 << endl;
20 return 0; // indicate successful termination
21 } // end main
gradeBook1 created for course: CS101 Introduction to C++
Programming
gradeBook2 created for course: CS102 Data Structures in C++
Interface of a Class
Interfaces define and standardize the ways in which things such
as people and systems interact with one another. For example,
a radio's controls serve as an interface between the radio's
users and its internal components. The controls allow users to
perform a limited set of operations (such as changing the
station, adjusting the volume, and choosing between AM and
FM stations). Various radios may implement these operations
differently some provide push buttons, some provide dials and
some support voice commands. The interface specifies what
operations a radio permits users to perform but does not
specify how the operations are implemented inside the radio.
[Page 101]
Figure 3.11. GradeBook class definition containing function prototypes that specify the
interface of the class.
(This item is displayed on page 100 in the print version)
[Page 102]
1. the first line of each member function (lines 11, 17, 23 and
29) matches its prototype in the GradeBook.h file for
example, the compiler ensures that getCourseName
accepts no parameters and returns a string.
2. each member function knows about the class's data
members and other member functions for example, lines
19 and 25 can access variable courseName because it is
declared in GradeBook.h as a data member of class
GradeBook, and lines 13 and 32 can call functions
setCourseName and getCourseName, respectively,
because each is declared as a member function of the class
in GradeBook.h (and because these calls conform with the
corresponding prototypes).
[Page 103]
[Page 104]
[Page 105]
1. https://www.geeksforgeeks.org/c-classes-and-objects/
2. https://www.w3schools.com/cpp/cpp_access_specifiers.asp
3. https://www.geeksforgeeks.org/playing-with-destructors-in-c/