Chapter 3 of 'C++ How to Program' discusses the initialization of objects using constructors, which are special member functions that cannot return values and are called automatically when an object is created. It also emphasizes the importance of separating class definitions into header files for reusability and maintaining a clear interface from implementation. Additionally, the chapter covers the use of member initializer lists and the significance of using the scope resolution operator when defining member functions outside the class definition.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views46 pages
chtp8_16
Chapter 3 of 'C++ How to Program' discusses the initialization of objects using constructors, which are special member functions that cannot return values and are called automatically when an object is created. It also emphasizes the importance of separating class definitions into header files for reusability and maintaining a clear interface from implementation. Additionally, the chapter covers the use of member initializer lists and the significance of using the scope resolution operator when defining member functions outside the class definition.
1 RESERVED. Initializing Objects with Constructors Each class can provide one or more constructors that can be used to initialize an object of the class when the object is created. A constructor is a special member function that must be defined with the same name as the class, so that the compiler can distinguish it from the class’s other member functions. An important difference between constructors and other functions is that constructors cannot return values, so they cannot specify a return type (not even void). Normally, constructors are declared public.
2 Initializing Objects with Constructors (cont.) C++ automatically calls a constructor for each object that is created, which helps ensure that objects are initialized properly before they’re used in a program. The constructor call occurs when the object is created. If a class does not explicitly include constructors, the compiler provides a default constructor with no parameters.
7 Initializing Objects with Constructors (cont.) A member initializer consists of a data member’s variable name followed by parentheses containing the member’s initial value. In this example, courseName is initialized with the value of the parameter name. If a class contains more than one data member, each data member’s initializer is separated from the next by a comma. The member initializer list executes before the body of the constructor executes.
8 Initializing Objects with Constructors (cont.) Line 47 creates and initializes a GradeBook object called gradeBook1. When this line executes, the GradeBook constructor (lines 14–18) is called with the argument "CS101 Introduction to C++ Programming" to initialize gradeBook1’s course name. Line 48 repeats this process for the GradeBook object called gradeBook2, this time passing the argument "CS102 Data Structures in C++" to initialize gradeBook2’s course name.
9 Initializing Objects with Constructors (cont.) Any constructor that takes no arguments is called a default constructor. A class gets a default constructor in one of several ways: The compiler implicitly creates a default constructor in every class that does not have any user-defined constructors. The default constructor does not initialize the class’s data members, but does call the default constructor for each data member that is an object of another class. An uninitialized variable contains an undefined (“garbage”) value. You explicitly define a constructor that takes no arguments. Such a default constructor will call the default constructor for each data member that is an object of another class and will perform additional initialization specified by you. If you define any constructors with arguments, C++ will not implicitly create a default constructor for that class.
10 Initializing Objects with Constructors (cont.) Like operations, the UML models constructors in the third compartment of a class in a class diagram. To distinguish a constructor from a class’s operations, the UML places the word “constructor” between guillemets (« and ») before the constructor’s name. It’s customary to list the class’s constructor before other operations in the third compartment.
13 Placing a Class in a Separate File for Reusability (cont.) 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’s 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. Programs use #include preprocessing directives to include header files and take advantage of reusable software components.
14 Placing a Class in a Separate File for Reusability (cont.) Our next example separates 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 7–38) and the headers on which the class depends. The main function that uses class GradeBook is defined in the source-code file fig03_10.cpp (Fig. 3.10) in lines 8–18. To help you prepare for the larger programs you’ll 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).
19 Placing a Class in a Separate File for Reusability (cont.) To help the compiler 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.
20 Placing a Class in a Separate File for Reusability (cont.) The compiler creates only one copy of the class’s member functions and shares that copy among all the class’s objects. Each object, of course, needs its own data members, because their contents can vary among objects. The member-function code, however, is not modifiable, so it can be shared among all objects of the class. Therefore, the size of an object depends on the amount of memory required to store the class’s data members. By including GradeBook.h in line 4, we give the compiler access to the information it needs to determine the size of a GradeBook object and to determine whether objects of the class are used correctly.
21 Placing a Class in a Separate File for Reusability (cont.) A #include directive instructs the C++ preprocessor to replace the directive with a copy of the contents of GradeBook.h before the program is compiled. When the source-code file fig03_10.cpp is compiled, it now contains the GradeBook class definition (because of the #include), and the compiler is able to determine how to create GradeBook objects and see that their member functions are called correctly. Now that the class definition is in a header file (without a main function), we can include that header in any program that needs to reuse our GradeBook class.
22 Placing a Class in a Separate File for Reusability (cont.) Notice that the name of the GradeBook.h header file in line 4 of Fig. 3.10 is enclosed in quotes (" ") rather than angle brackets (< >). Normally, a program’s source-code files and user-defined header files are placed in the same directory. When the preprocessor encounters a header file name in quotes, it attempts to locate the header file in the same directory as the file in which the #include directive appears. If the preprocessor cannot find the header file in that directory, it searches for it in the same location(s) as the C++ Standard Library header files. When the preprocessor encounters a header file name in angle brackets (e.g., <iostream>), it assumes that the header is part of the C++ Standard Library and does not look in the directory of the program that is being preprocessed.
25 Separating Interface from Implementation (cont.) In our prior examples, each class definition contained the complete definitions of the class’s public member functions and the declarations of its private data members. It’s better software engineering to define member functions outside the class definition, so that their implementation details can be hidden from the client code. Ensures that you do not write client code that depends on the class’s implementation details. The program of Figs. 3.11–3.13 separates class GradeBook’s interface from its implementation by splitting the class definition of Fig. 3.9 into two files—the header file GradeBook.h (Fig. 3.11) in which class GradeBook is defined, and the source-code file GradeBook.cpp (Fig. 3.12) in which GradeBook’s member functions are defined.
26 Separating Interface from Implementation (cont.) By convention, member-function definitions are placed in a source-code file of the same base name (e.g., GradeBook) as the class’s header file but with a .cpp filename extension. Figure 3.14 shows how this three-file program is compiled from the perspectives of the GradeBook class programmer and the client- code programmer—we’ll explain this figure in detail.
27 Separating Interface from Implementation (cont.) Header file GradeBook.h (Fig. 3.11) is similar to the one in Fig. 3.9, but the function definitions in Fig. 3.9 are replaced here with function prototypes (lines 11–14) that describe the class’s public interface without revealing the class’s member-function implementations. A function prototype is a declaration of a function that tells the compiler the function’s name, its return type and the types of its parameters. Including the header file GradeBook.h in the client code (line 5 of Fig. 3.13) provides the compiler with the information it needs to ensure that the client code calls the member functions of class GradeBook correctly.
39 Validating Data with set Functions (cont.) Figure 3.17 demonstrates the modified version of class GradeBook (Figs. 3.15–3.16) featuring validation. In previous versions of the class, the benefit of calling setCourseName in the constructor was not evident. Now, however, the constructor takes advantage of the validation provided by setCourseName. The constructor simply calls setCourseName, rather than duplicating its validation code.