Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Summary

Section 3.2 Defining a Class with a Member Function

  • A class definition (p. 66) contains the data members and member functions that define the class’s attributes and behaviors, respectively.

  • A class definition begins with the keyword class followed immediately by the class name.

  • By convention, the name of a user-defined class (p. 67) begins with a capital letter and, for readability, each subsequent word in the class name begins with a capital letter.

  • Every class’s body (p. 66) is enclosed in a pair of braces ({ and }) and ends with a semicolon.

  • Member functions that appear after access specifier public (p. 66) can be called by other functions in a program and by member functions of other classes.

  • Access specifiers are always followed by a colon (:).

  • Keyword void (p. 67) is a special return type which indicates that a function will perform a task but will not return any data to its calling function when it completes its task.

  • By convention, function names (p. 67) begin with a lowercase first letter and all subsequent words in the name begin with a capital letter.

  • An empty set of parentheses after a function name indicates that the function does not require additional data to perform its task.

  • Every function’s body is delimited by left and right braces ({ and }).

  • Typically, you cannot call a member function until you create an object of its class.

  • Each new class you create becomes a new type in C++.

  • In the UML, each class is modeled in a class diagram (p. 68) as a rectangle with three compartments, which (top to bottom) contain the class’s name, attributes and operations, respectively.

  • The UML models operations as the operation name followed by parentheses. A plus sign (+) preceding the name indicates a public operation (i.e., a public member function in C++).

Section 3.3 Defining a Member Function with a Parameter

  • A member function can require one or more parameters (p. 68) that represent additional data it needs to perform its task. A function call supplies an argument (p. 68) for each function parameter.

  • A member function is called by following the object name with a dot (.) operator (p. 67), the function name and a set of parentheses containing the function’s arguments.

  • A variable of C++ Standard Library class string (p. 69) represents a string of characters. This class is defined in header <string>, and the name string belongs to namespace std.

  • Function getline (from header <string>, p. 70) reads characters from its first argument until a newline character is encountered, then places the characters (not including the newline) in the string variable specified as its second argument. The newline character is discarded.

  • A parameter list (p. 70) may contain any number of parameters, including none at all (represented by empty parentheses) to indicate that a function does not require any parameters.

  • The number of arguments in a function call must match the number of parameters in the parameter list of the called member function’s header. Also, the argument types in the function call must be consistent with the types of the corresponding parameters in the function header.

  • The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter type between the parentheses following the operation name.

  • The UML has its own data types. Not all the UML data types have the same names as the corresponding C++ types. The UML type String corresponds to the C++ type string.

Section 3.4 Data Members, set Functions and get Functions

  • Variables declared in a function’s body are local variables (p. 71) and can be used only from the point of their declaration in the function to the immediately following closing right brace (}).

  • A local variable must be declared before it can be used in a function. A local variable cannot be accessed outside the function in which it’s declared.

  • Data members (p. 71) normally are private (p. 73). Variables or functions declared private are accessible only to member functions of the class in which they’re declared, or to friends of the class.

  • When a program creates (instantiates) an object, its private data members are encapsulated (hidden, p. 74) in the object and can be accessed only by member functions of the object’s class.

  • When a function that specifies a return type other than void is called and completes its task, the function returns a result to its calling function.

  • By default, the initial value of a string is the empty string (p. 75)—i.e., a string that does not contain any characters. Nothing appears on the screen when an empty string is displayed.

  • A class often provides public member functions to allow the class’s clients to set or get (p. 75) private data members. The names of these member functions normally begin with set or get.

  • Set and get functions allow clients of a class to indirectly access the hidden data. The client does not know how the object performs these operations.

  • A class’s set and get functions should be used by other member functions of the class to manipulate the class’s private data. If the class’s data representation is changed, member functions that access the data only via the set and get functions will not require modification.

  • A public set function should carefully scrutinize any attempt to modify the value of a data member to ensure that the new value is appropriate for that data item.

  • The UML represents data members as attributes by listing the attribute name, followed by a colon and the attribute type. Private attributes are preceded by a minus sign () in the UML.

  • The UML indicates the return type of an operation by placing a colon and the return type after the parentheses following the operation name.

  • UML class diagrams do not specify return types for operations that do not return values.

Section 3.5 Initializing Objects with Constructors

  • Each class should provide a constructor (p. 77) to initialize an object of the class when the object is created. A constructor must be defined with the same name as the class.

  • A difference between constructors and functions is that constructors cannot return values, so they cannot specify a return type (not even void). Normally, constructors are declared public.

  • C++ requires a constructor call at the time each object is created, which helps ensure that every object is initialized before it’s used in a program.

  • A constructor with no parameters is a default constructor (p. 77). If you do not provide a constructor, the compiler provides a default constructor. You can also define a default constructor explicitly. If you define a constructor for a class, C++ will not create a default constructor.

  • The UML models constructors as operations in a class diagram’s third compartment with the word “constructor” between guillemets (« and ») before the constructor’s name.

Section 3.6 Placing a Class in a Separate File for Reusability

  • Class definitions, when packaged properly, can be reused by programmers worldwide.

  • It’s customary to define a class in a header (p. 81) that has a .h filename extension.

Section 3.7 Separating Interface from Implementation

  • If the class’s implementation changes, the class’s clients should not be required to change.

  • Interfaces define and standardize the ways in which things such as people and systems interact.

  • A class’s public interface (p. 85) describes the public member functions that are made available to the class’s clients. The interface describes what services (p. 85) clients can use and how to request those services, but does not specify how the class carries out the services.

  • Separating interface from implementation (p. 84) makes programs easier to modify. Changes in the class’s implementation do not affect the client as long as the class’s interface remains unchanged.

  • A function prototype (p. 85) contains a function’s name, its return type and the number, types and order of the parameters the function expects to receive.

  • Once a class is defined and its member functions are declared (via function prototypes), the member functions should be defined in a separate source-code file.

  • For each member function defined outside of its corresponding class definition, the function name must be preceded by the class name and the binary scope resolution operator (::, p. 86).

Section 3.8 Validating Data with set Functions

  • Class string’s length member function (p. 91) returns the number of characters in a string.

  • Class string’s member function substr (p. 92) returns a new string containing a copy of part of an existing string. The first argument specifies the starting position in the original string. The second specifies the number of characters to copy.