Join Books.org — it's free

C++ and Object Oriented Programming by Kip R. Irvine β€” book cover
Computers & the Internet, Programming

C++ and Object Oriented Programming

by Kip R. Irvine
Write a review
Log in to track your reading progress.

Synopsis

Chapter 1 Summary:
C++ is what might be called an industrial-strength language. It combines the power and efficiency of C with object-oriented features such as classes, inheritance, and polymorphism. C++ is particularly well-suited to writing large, complex software systems that involve teams of programmers. It is also an ideal language for developing libraries that can be easily installed and modified in application programs. C++ is what we call a strongly typed language, meaning that the compiler helps to prevent the use of incorrect expressions involving incompatible data types. Also, function calls across module boundaries are checked against function prototypes to make sure the number and types of the passed arguments agree with the function parameters.
C++ is called a hybrid language because it allows you to mix procedural code and object-oriented code in the same program. This has helped win it acceptance in the C programming community.
Objects represent the real-world entities that play an active part in an application. Designers try to model object-oriented programs around these real-world entities. An object encapsulates (contains) both attributes and operations (also called behaviors). The attributes represent the state of the object, the values of which change at run time. The operations represent the way the object communicates with other objects. A class object is an instance of some class.
A class is a description of the common characteristics shared by all objects of the same type. It is in fact a user-defined data type, with all the rights and privileges (from a language point of view) given to standard data types such as int, float, or double. As we learnto use the tools of C++, we will find that the classes we define can incorporate the same types of operators and implicit conversions available to standard data types.
Inheritance is a powerful tool in object-oriented programming that lets us derive new classes by refining and adding to the attributes and behaviors of existing classes. With inheritance, we can begin with an existing class library and customize it for a particular application.
Polymorphism allows the same name to denote objects of different types. In C++, these types must be classes that are related by inheritance.
We described some of the differences between C and C++, for programmers with a background in C who are moving to C++.
We introduced stream I/O, showing how to write to the standard cout stream object, and how to read from the standard cin stream object. Although the keyboard is the default input device and the screen is the default output device, both input and output can be redirected to other devices.
We briefly introduced reference parameters, a topic that we will cover thoroughly in Chapter 3.

Chapter 2 Summary:
This chapter introduced the fundamentals of defining and using classes. The emphasis was on creating simple, useful classes in this chapter, so that in future chapters we can deal with the more subtle aspects of class design.
An object has three properties: its state, consisting of the current values of its data members; its behavior, implemented as member functions; and, its identity, which makes it distinct from all other instances of the same class.
Object-oriented programs attempt to model real-world problems and applications. Because of this, objects in a program usually have a close correlation to the real-world objects in a certain problem domain.
A link relationship exists when a class member function sends messages to an object of another class. A composition relationship exists when a class contains data members that are class objects themselves.
Objects in programs hide their details, presenting only an outside view, called an interface. An interface is an abstracted or simplified view of an object that is presented to the object''s user.
Classes can be given the same rich set of behaviors that characterize C/C++ built-in data types.
A class name whose definition is not nested inside a function or another class has file scope and external linkage. For such a class, the class name defines a type, and objects of that type can be declared anywhere in the same program.
By default, class members are private, meaning that they have class scope and can only be accessed by functions in the same class or by friends of the class. On the other hand, class members that are declared public are accessible from outside the class. Each class member name must be unique within the class scope.
Member functions are categorized as constructors, destructors, operators, selectors, modifiers, and iterators. A constructor is a member function that runs automatically whenever a class variable is defined. The compiler automatically generates a default constructor if and only if you have not defined any constructor for the class. A destructor is a member function that executes when a class variable is logically deleted. A destructor has no parameters or return type. A class can have only one destructor.
Rarely should a class contain public data members, as this makes it impossible for the class to regulate changes to its own data. Member functions that will only be called from other member functions and friend functions in the same class should be made private.
While a program is in its develpment and testing phases, some programmers believe that inline functions should be avoided. Runtime efficiency is rarely important until after a program has been thoroughly debugged and tested, at which point, critical areas of code can be optimized to improve runtime efficiency.
Coding Style Suggestions
Most professional programmers adopt a coding style that is consistent and readable to aid themselves and others who must read their code. Here are some suggested style guidelines which you may choose to follow. The following are strong suggestions, which are widely followed:
Avoid using numeric literals in program code. Instead, use named constants such as
const int MaxPoints = 10;
const float HighRange = 3.5;
Use Get and Set prefixes on member functions that get and set the values of data members, e.g. GetX and SetX.
Choose function names that clearly express what they do, such as GetLastName, SetLastName, or CalcGrossPay. Avoid ambiguous names such as LastName, Calculate, or GrossPay.
Place constructors and destructors first in the list of class member functions.
Place comments below member functions in class definitions if they help the reader to understand what the functions do. The following are suggestions that we generally follow:
Place members in the following order: public first, then protected, then private. If possible, avoid switching back and forth between private and public members, as it makes a class definition harder to read.
Avoid implementing functions inside a class definition block because it unneccessarily reveals class implementation details in what should be the class interface. In addition, function implementations inside a class definition make the class hard to read.
Either use underscores to separate words in function names or don''t use them. Don't mix styles.
Either capitalize member function names or don't captialize them. Don't mix styles.
Place the opening brace for the list of class members on the same line as the class name (some programmers place the brace on a line by itself).

Chapter 3 Summary:
A function signature consists of a function name and the order and types of its parameters, but not its return type.
We use the term linkage to describe the visibility of names between program modules. When a function in one module is called from another module, for example, the function must have external linkage. By default, names of functions and variables are external. The opposite of external linkage is internal linkage, meaning that a name is visible only in its declared module.
Function name encoding is the process whereby the C++ compiler creates unique identifiers out of functions that have identical names but different parameter lists.
Preconditions are used when we want to state input requirements for function arguments. Postconditions explain what a function will do, given that the preconditions were satisfied. The assert macro can be used to enforce preconditions.
If a class object is passed to a function, it should be passed by reference to prevent the inefficient construction of a temporary object. Whenever possible, pass an object by constant reference to ensure that it cannot be modified.
A function with a non-constant reference parameter cannot receive a constant argument. But a function with a constant reference parameter can receive a non-constant argument.
Although it may be tempting to do so, never cast a constant object into a non-constant. The reason the object was declared const in the first place was to prevent it from being modified.
A function can be declared (with a function prototype) but not implemented, as long as the function is never called, and its address is never taken. Because of this, whenever you test a new class, be sure to call every member function to verify that the function was implemented.
Default function arguments are useful as a way to make functions more flexible, thus allowing them to be called with varying numbers of arguments. They can, for example, reduce the number of constructors in a class.
Never return a non-constant reference to a private class member; it would improperly allow a class client to have direct write access to the member.
A friend function is a global function that has been granted priveleged access to the private members of a specific class. Friend functions are often used to overload stream I/O operators.
Function overloading allows multiple functions with the same signature to exist in the same scope. Overloading is particularly useful for different functions that perform similar actions but have different parameter lists.
We discussed implicit conversions of function arguments. In general, when an argument of one type is passed to a function whose parameter list contains a similar type, the argument is converted to the parameter''s type.
We demonstrated a way of preventing an input stream from entering an error state, by using an input string stream. This makes it much easier to recover from bad user input.
We demonstrated the stream manipulators setw, setfill, setprecision, oct, dec, and hex to control the formatting of numeric and string output.
We designed and implemented a Student Registration case study, demonstrating the specifications, analysis, design, and implementation steps. We plan to use this format for presenting case studies throughout the book.
We discussed recursion and showed an example of a recursive function that calculates the factorial of an integer. We also showed how a program can retrieve its command-line arguments.

Chapter 4: Class Features Summary:
Enumerated constants help to document the use of constants in a class and should be used whenever possible. Enumerated types are usually given public access, and when used outside of the class scope, must be qualified by the class name.
Classes often contain other class objects. We express this as a composition relationship and recongnize that it allows us to build powerful and complex data structures. Most applications are comprised of groups of related objects, many containing other objects.
A constructor-initializer list is used by a constructor to initialize class data members, particularly ones that are class objects. We presented the Point and Figure classes as examples of this.
Classes can contain reference and const-qualified data members. These must be initialized by the enclosing class constructor, using a constructor-initializer list. References are useful for forming a connection to an object that already exists.
Static data members are variables that belong to all instances of the same class. We showed an example of a Student class that kept a reference count of the number of instances of students created by the program. We showed how a static data member can be effectively used by a class constructor.
Static member functions provide access to static data members. They are useful for operations that involve an entire class, such as initializing static data members. We showed an example of a Rectangle class that used a static member function to set the default drawing color of all Rectangle objects.
We introduced the FString class, which implements operations on fixed-length strings. This class makes it easier to assign, initialize, copy, compare, and display strings, without having to deal directly with character arrays. We will use the FString class throughout the book.
The Robot Wars simulation program was an example of a larger program involving several interacting classes. We followed the same program development steps introduced in Chapter 2, including specifications, analysis, design, and implementation. Recognizing that no useful program is ever completely finished, we discussed ways in which the program could be improved.

Chapter 5: Designing Classes Chapter Summary:
We defined a number of terms in this chapter related to object-oriented analysis, design, and programming. We explained how analysis, design, and programming are complementary activities that produce better programs.
The object model is an essential way of looking at object-oriented programs that includes abstraction, encapsulation, modularity, hierarchy, and typing. Object-oriented analysis, design, and programming are based on the object model.
Abstraction clarifies the essential characteristics of an object that make it different from other types of objects. It is a way of providing a clarified, more solution-oriented view of what had previously been viewed as a complex, problematic entity.
Enclosing both the interface and implementation in a class is called encapsulation. Information hiding is a technique used to achieve encapsulation.
Modularity is the technique one uses to cluster related program code into separate compilation units.
Inheritance describes a parent-child relationship between classes. This allows us to derive new classes from existing ones, where the derived classes contain all characteristics of their parents plus additional features that make them unique. It is for this reason that a derived class is called a superclass in some object-oriented programming languages.
Typing, or type checking is the mechanism by which a language controls whether objects of different types can be interchanged.
In this chapter, we emphasize the importance of analysis and design because object-oriented programming involves a deeper understanding of the principles of object-oriented programming than just the syntax and semantics of C++. It usually takes professional programmers a considerable amount of time before they can internalize the principles of object-oriented design well enough to produce nontrivial applications.
Well-designed object-oriented programs generally have easy-to-understand class interfaces and clear relationships between classes. Portability is often easier to achieve in an object-oriented environment because classes can be clustered into libraries that allow cross-platform software development. In object-oriented programming, extensibility is achieved by adding new classes to programs, and using inheritance to refine the behavior of existing classes.
We discussed a number of design principles. First, an object-oriented design should model the application problem that it is trying to solve. Second, object-oriented design is best viewed as an iterative process, where design and programming work hand-in-hand.
We discussed ways of designing components (groups of related classes). The basic steps were:
* Find the classes that most closely resemble the real-world entities in the application.
* Specify which operations each class can perform, including its responses to messages sent by other objects.
* Specify class dependencies, showing how classes in the application depend on each other.
* Specify class interfaces, describing the public members of each class.
We finished the chapter with a complete case study, called Doctor''s Office Scheduling. The specifications, analysis, design, and implementation steps were explained, showing how they fit in with the design principles from this chapter.

Reviews

There are no reviews yet. Log in to write one.

Book Details

Published
November 1, 1996
Publisher
Prentice Hall Professional Technical Reference
Format
Paperback
ISBN
9780023598524

More by Kip R. Irvine

Similar books