Main Page

Previous Page
Next Page

[Page 118 (continued)]


  • Performing a task in a program requires a function. The function hides from its user the complex tasks that it performs.

  • A function in a class is known as a member function and performs one of the class's tasks.

  • You must create an object of a class before a program can perform the tasks the class describes. That is one reason C++ is known as an object-oriented programming language.

  • Each message sent to an object is a member-function call that tells the object to perform a task.

  • An object has attributes that are carried with the object as it is used in a program. These attributes are specified as data members in the object's class.

  • A class definition 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 begins with a capital letter and, for readability, each subsequent word in the class name begins with a capital letter.

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

  • Member functions that appear after access specifier public 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 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 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.

  • [Page 119]
  • Each new class you create becomes a new type in C++ that can be used to declare variables and create objects. This is one reason why C++ is known as an extensible language.

  • A member function can require one or more parameters that represent additional data it needs to perform its task. A function call supplies arguments for each of the function's parameters.

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

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

  • Function getline (from header <string>) 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 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.

  • Variables declared in a function's body are local variables and can be used only from the point of their declaration in the function to the immediately following closing right brace (}). When a function terminates, the values of its local variables are lost.

  • 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 is declared.

  • Data members normally are private. Variables or functions declared private are accessible only to member functions of the class in which they are declared.

  • When a program creates (instantiates) an object of a class, its private data members are encapsulated (hidden) 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 stringi.e., a string that does not contain any characters. Nothing appears on the screen when an empty string is displayed.

  • Classes often provide public member functions to allow clients of the class to set or get private data members. The names of these member functions normally begin with set or get.

  • Providing public set and get functions allows clients of a class to indirectly access the hidden data. The client knows that it is attempting to modify or obtain an object's data, but the client does not know how the object performs these operations.

  • The set and get functions of a class also should be used by other member functions within the class to manipulate the class's private data, although these member functions can access the private data directly. If the class's data representation is changed, member functions that access the data only via the set and get functions will not require modificationonly the bodies of the set and get functions that directly manipulate the data member will need to change.

  • 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.

  • Each class you declare should provide a constructor 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.

  • 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.

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

  • A constructor that takes no arguments is a default constructor. In any class that does not include a constructor, the compiler provides a default constructor. The class programmer can also define a default constructor explicitly. If the programmer defines a constructor for a class, C++ will not create a default constructor.

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

  • It is customary to define a class in a header file that has a .h filename extension.

  • 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.

  • The interface of a class describes the public member functions (also known as public services) that are made available to the class's clients. The interface describes what services clients can use and how to request those services, but does not specify how the class carries out the services.

  • A fundamental principle of good software engineering is to separate interface from implementation. This makes programs easier to modify. Changes in the class's implementation do not affect the client as long as the class's interface originally provided to the client remains unchanged.

  • A function prototype 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 (::).

  • Class string's length member function returns the number of characters in a string object.

  • Class string's member function substr (short for "substring") returns a new string object created by copying part of an existing string object. The function's first argument specifies the starting position in the original string from which characters are copied. Its second argument specifies the number of characters to copy.

  • In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top compartment contains the class name, centered horizontally in boldface. The middle compartment contains the class's attributes (data members in C++). The bottom compartment contains the class's operations (member functions and constructors in C++).

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

  • 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.

  • 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.

  • The UML models constructors as operations in a class diagram's third compartment. To distinguish a constructor from a class's operations, the UML places the word "constructor" between guillemets (« and ») before the constructor's name.

Previous Page
Next Page