36
C++ A Beginner’s Guide by Herbert Schildt
7.
How does & differ from &&?
8.
What does this statement do?
x *= 10;
9.
Using the rrotate( ) and lrotate( ) functions from Project 7-1, it is possible to encode and decode a
string. To code
the string, left-rotate each letter by some amount that is specified by a key. To decode,
right-rotate each character by the same amount. Use a key that consists of a string of characters. There
are many ways to compute the number of rotations from the key. Be creative. The
solution shown in the
online answers is only one of many.
10.
On your own, expand show_binary( ) so that it shows all bits within an unsigned int rather than just
the first eight.
1
C++ A Beginner’s Guide by Herbert Schildt
Module8
Classes and Objects
Table of Contents
CRITICAL SKILL 8.1: The General Form of a Class .......................................................................................... 2
CRITICAL SKILL 8.2: Defining a Class and Creating Objects ........................................................................... 2
CRITICAL SKILL 8.3: Adding Member Functions to a Class ............................................................................ 6
Project 8-1 Creating a Help Class .................................................................................................................. 9
CRITICAL SKILL 8.4: Constructors and Destructors ..................................................................................... 14
CRITICAL SKILL 8.5: Parameterized Constructors ........................................................................................ 17
CRITICAL SKILL 8.6: Inline Functions ........................................................................................................... 22
CRITICAL SKILL 8.7: Arrays of Objects ......................................................................................................... 31
CRITICAL SKILL 8.8: Initializing Object Arrays .............................................................................................. 32
CRITICAL SKILL 8.9: Pointers to Objects ...................................................................................................... 34
U
p to this point, you have been writing programs that did not use any of C++’s object-oriented
capabilities. Thus, the programs in the preceding modules reflected structured programming, not
object-oriented programming. To write object-oriented programs, you will need to use classes. The class
is C++’s basic unit of encapsulation. Classes are used to create objects. Classes and objects are so
fundamental to C++ that much of the remainder of this book is devoted to them in one way or another.
Class Fundamentals
Let’s begin by reviewing the terms class and object. A class is a template
that defines the form of an
object. A class specifies both code and data. C++ uses a class specification to construct objects. Objects
are instances of a class. Thus, a class is essentially a set of plans that specify how to build an object. It is
important to be clear on one issue: a class is a logical abstraction. It is not until an object of that class
has been created that a physical representation of that class exists in memory.
When you define a class, you declare the data that it contains and the code that operates on that data.
While very simple classes might contain only code or only data, most real-world classes contain both.
2
C++ A Beginner’s Guide by Herbert Schildt
Data is contained in instance variables defined by the class, and code is contained in functions. The code
and data that constitute a class are called members of the class.
Do'stlaringiz bilan baham: