Tải bản đầy đủ (.pdf) (20 trang)

C++ Primer Plus (P31) pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (55.65 KB, 20 trang )

<< " Total Worth: $" << this->total_val << '\n';
}
That is, it converts a Stock:: qualifier to a function argument that is a pointer to Stock and
then uses the pointer to access class members.
Similarly, the front end converts function calls like
top.show();
to:
show(&top);
In this fashion, the this pointer is assigned the address of the invoking object. (The actual
details might be more involved.)
Class Scope
Chapter 9 discusses global, or file, scope and local, or block, scope. You can use a
variable with global scope, recall, anywhere in the file that contains its definition, whereas a
variable with local scope is local to the block that contains its definition. Function names,
too, can have global scope, but they never have local scope. C++ classes introduce a new
kind of scope: class scope. Class scope applies to names defined in a class, such as the
names of class data members and class member functions. Items that have class scope
are known within the class but not outside the class. Thus, you can use the same class
member names in different classes without conflict: The shares member of the Stock
class is a variable distinct from the shares member of a JobRide class. Also, class scope
means you can't directly access members of a class from the outside world. This is true
even for public function members. That is, to invoke a public member function, you have to
use an object:
Stock sleeper("Exclusive Ore", 100, 0.25); // create object
sleeper.show(); // use object to invoke a member function
show(); // invalid can't call method directly
Similarly, you have to use the scope resolution operator when you define member
functions:
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
void Stock::update(double price)
{



}
In short, within a class declaration or a member function definition you can use an
unadorned member name (the unqualified name), as when sell() calls the set_tot()
member function. A constructor name is recognized when called because its name is the
same as the class name. Otherwise, you must use the direct membership operator (.), the
indirect membership operator (->), or the scope resolution operator (::), depending on the
context, when you use a class member name. The following code fragment illustrates how
identifiers with class scope can be accessed:
class Ik
{
private:
int fuss; // fuss has class scope
public:
Ik(int f = 9) { fuss = f; } // fuss is in scope
void ViewIk() const; // ViewIk has class scope
};
void Ik::ViewIk() const //Ik:: places ViewIk into scope
{
cout << fuss << endl; // fuss in scope within class methods
}

int main()
{
Ik * pik = new Ik;
Ik ee = Ik(8); // constructor in scope because has class name
ee.ViewIk(); // class object brings ViewIk into scope
pik->ViewIk(); // pointer-to-Ik brings ViewIk into scope

Class Scope Constants

This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Sometimes it would be nice to have symbolic constants of class scope. For example, the
Stock class declaration used a literal 30 to specify the array size for company. Also,
because the constant is the same for all objects, it would be nice to create a single
constant shared by all objects. You might think the following would be a solution:
class Stock
{
private:
const int Len = 30; // declare a constant? FAILS
char company[Len];

But this won't work because declaring a class describes what an object looks like but
doesn't create an object. Hence, until you create an object, there's no place to store a
value. There are, however, a couple of ways to achieve essentially the same desired
effect.
First, you can declare an enumeration within a class. An enumeration given in a class
declaration has class scope, so you can use enumerations to provide class scope symbolic
names for integer constants. That is, you can start off the Stock declaration this way:
class Stock
{
private:
enum {Len = 30}; // class-specific constant
char company[Len];

Note that declaring an enumeration in this fashion does not create a class data member.
That is, each individual object does not carry an enumeration in it. Rather, Len is just a
symbolic name that the compiler replaces with 30 when it encounters it in code in class
scope.
Because this uses the enumeration merely to create a symbolic constant with no intent of
creating variables of the enumeration type, you needn't provide an enumeration tag.

Incidentally, for many implementations, the ios_base class does something similar in its
public section; that's the source of identifiers such as ios_base::fixed. Here fixed is
typically an enumerator defined in the ios_base class.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
More recently, C++ has introduced a second way of defining a constant within a
class—using the keyword static:
class Stock
{
private:
static const int Len = 30; // declare a constant! WORKS
char company[Len];

This creates a single constant called Len that is stored with other static variables rather
than in an object. Thus, there is only one Len constant shared by all Stock objects.
Chapter 12, "Classes and Dynamic Memory Allocation," looks further into static class
members. You only can use this technique for declaring static constants with integral and
enumeration values. You can't store a double constant this way.
An Abstract Data Type
The Stock class is pretty specific. Often, however, programmers define classes to
represent more general concepts. For example, classes are a good way to implement what
computer scientists describe as abstract data types, or ADTs, for short. As the name
suggests, an ADT describes a data type in a general fashion, without bringing in language
or implementation details. Consider, for example, the stack. The stack is a way of storing
data in which data is always added to or deleted from the top of the stack. C++ programs,
for example, use a stack to manage automatic variables. As new automatic variables are
generated, they are added to the top of the stack. When they expire, they are removed
from a stack.
Let's describe the properties of a stack in a general, abstract way. First, a stack holds
several items. (That property makes it a container, an even more general abstraction.)
Next, a stack is characterized by the operations you can perform on one.

You can create an empty stack.
You can add an item to the top of a stack (push an item).
You can remove an item from the top (pop an item).
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
You can check to see if the stack is full.
You can check to see if the stack is empty.
You can match this description with a class declaration in which the public member
functions provide an interface that represents the stack operations. The private data
members take care of storing the stack data. The class concept is a nice match to the ADT
approach.
The private section has to commit itself to how to hold the data. For example, you can use
an ordinary array, a dynamically allocated array, or some more advanced data structure,
such as a linked list. The public interface, however, should hide the exact representation.
Instead, it should be expressed in general terms, such as creating a stack, pushing an
item, and so on. Listing 10.10 shows one approach. It assumes that the bool type has
been implemented. If it hasn't been on your system, you can use int, 0, and 1 rather than
bool, false, and true.
Listing 10.10 stack.h
// stack.h class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
enum {MAX = 10}; // constant specific to class
Item items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();

bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
bool pop(Item & item); // pop top into item
};
#endif
Compatibility Note
If your system hasn't implemented the bool type, you can
use int, 0, and 1 rather than bool, false, and true.
Alternatively, your system might support an earlier,
non-standard form, such as boolean or Boolean.
In this example, the private section shows that the stack is implemented by using an array,
but the public section doesn't reveal that fact. Thus, you can replace the array with, say, a
dynamic array without changing the class interface. That means changing the stack
implementation doesn't require that you recode programs using the stack. You just
recompile the stack code and link it with existing program code.
The interface is redundant in that pop() and push() return information about the stack
status (full or empty) instead of being type void. This provides the programmer with a
couple of options as to how to handle exceeding the stack limit or emptying the stack. He
or she can use isempty() and isfull() to check before attempting to modify the stack, or
else use the return value of push() and pop() to determine if the operation is successful.
Rather than define the stack in terms of some particular type, the class describes it in terms
of a general Item type. In this case, the header file uses typedef to make Item the same
as unsigned long. If you want, say, a stack of double or of a structure type, you can
change the typedef and leave the class declaration and method definitions unaltered.
Class templates (see Chapter 14, "Reusing Code in C++") provide a more powerful method
for isolating the type of data stored from the class design.

Next, let's implement the class methods. Listing 10.11 shows one possibility.
Listing 10.11 stack.cpp
// stack.cpp Stack member functions
#include "stack.h"
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Stack::Stack() // create an empty stack
{
top = 0;
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::pop(Item & item)
{
if (top > 0)

{
item = items[ top];
return true;
}
else
return false;
}
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
The default constructor guarantees that all stacks are created empty. The code for pop()
and push() guarantees that the top of the stack is managed properly. Guarantees like this
are one of the things that make object-oriented programming more reliable. Suppose,
instead, you create a separate array to represent the stack and an independent variable to
represent the index of the top. Then, it is your responsibility to get the code right each time
you create a new stack. Without the protection that private data offers, there's always the
possibility of making some program blunder that alters data unintentionally.
Let's test this stack. Listing 10.12 models the life of a clerk who processes purchase orders
from the top of his in-basket, using the LIFO (last-in, first-out) approach of a stack.
Listing 10.12 stacker.cpp
// stacker.cpp test Stack class
#include <iostream>
using namespace std;
#include <cctype> // or ctype.h
#include "stack.h"
int main()
{
Stack st; // create an empty stack
char c;
unsigned long po;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";

while (cin >> c && toupper != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha)
{
cout << '\a';
continue;
}
switch
{
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
case 'A':
case 'a': cout << "Enter a PO number to add: ";
cin >> po;
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p': if (st.isempty())
cout << "stack already empty\n";
else {
st.pop(po);
cout << "PO #" << po << " popped\n";
}
break;
}
cout << "Please enter A to add a purchase order,\n"

<< "P to process a PO, or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
The little while loop that gets rid of the rest of the line isn't absolutely necessary here, but it
will come in handy in a modification of this program in Chapter 14. Here's a sample run:
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 17885
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #17885 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
A
Enter a PO number to add: 17965
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
A
Enter a PO number to add: 18002
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
PO #18002 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.

P
PO #17965 popped
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
P
stack already empty
Please enter A to add a purchase order,
P to process a PO, or Q to quit.
Q
Bye
Real World Note: Minimizing Class Size with Selective Data Typing
When designing your classes, give careful thought to the
data types used for your class members. Imprudent use of
nonstandard or platform-dependent data types will inflate
the size of your classes, thereby increasing the memory
footprint, or working set, of your programs. This is both
inefficient and considered bad form.
A classic example, which demonstrates this point, involves
using a nonstandard BOOL typedef instead of the
standard bool data type. Consider these simple classes:
typedef int BOOL;
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
class BadClassDesign
{
BOOL m_b1;
BOOL m_b2;
BOOL m_b3;
BOOL m_b4;
BOOL m_b5;
BOOL m_b6;

};
class GoodClassDesign
{
bool m_b1;
bool m_b2;
bool m_b3;
bool m_b4;
bool m_b5;
bool m_b6;
};
Unlike bool, which usually occupies only one byte on most
platforms, each BOOL typically will occupy four bytes. If
the intent for the class members is to manage a true
Boolean value of true or false, only one byte is actually
required. For the typical Intel platform machine, class
BadClassDesign occupies 24 bytes while class
ClassGoodDesign uses only 6 bytes. That's a 400%
savings!
You should always strive to use appropriate data types that
minimize the amount of memory your program requires.
Summary
Object-oriented programming emphasizes how a program represents data. The first step
toward solving a programming problem by using the OOP approach is describing the data
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
in terms of its interface with the program, specifying how the data is used. Next, design a
class that implements the interface. Typically, private data members store the information,
whereas public member functions, also called methods, provide the only access to the
data. The class combines data and methods into one unit, and the private aspect
accomplishes data hiding.
Usually, you separate the class declaration into two parts, typically kept in separate files.

The class declaration proper goes into a header file, with the methods represented by
function prototypes. The source code that defines the member functions goes into a
methods file. This approach separates the description of the interface from the details of
the implementation. In principle, you only need to know the public class interface to use the
class. Of course, you can look at the implementation (unless it's been supplied to you in
compiled form only), but your program shouldn't rely on details of the implementation, such
as knowing a particular value is stored as an int. As long as a program and a class
communicate only through methods defining the interface, you are free to improve either
part separately without worrying about unforeseen interactions.
A class is a user-defined type, and an object is an instance of a class. That means an
object is a variable of that type or the equivalent of a variable, such as memory allocated
by new according to the class specification. C++ tries to make user-defined types as
similar as possible to standard types, so you can declare objects, pointers to objects, and
arrays of objects. You can pass objects as arguments, return them as function return
values, and assign one object to another of the same type. If you provide a constructor
method, you can initialize objects when they are created. If you provide a destructor
method, the program executes that method when the object expires.
Each object holds its own copies of the data portion of a class declaration, but they share
the class methods. If mr_object is the name of a particular object and try_me() is a
member function, you invoke the member function by using the dot membership operator:
mr_object.try_me(). OOP terminology describes this function call as sending a try_me
message to the mr_object object. Any reference to class data members in the try_me()
method then applies to the data members of the mr_object object. Similarly, the function
call i_object.try_me() accesses the data members of the i_object object.
If you want a member function to act on more than one object, you can pass additional
objects to the method as arguments. If a method needs to refer explicitly to the object that
evoked it, it can use the this pointer. The this pointer is set to the address of the evoking
object, so *this is an alias for the object itself.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Classes are well matched to describing abstract data types (ADTs). The public member

function interface provides the services described by an ADT, and the class's private
section and the code for the class methods provide an implementation hidden from clients
of the class.
Review Questions
.1:What is a class?
.2:How does a class accomplish abstraction, encapsulation, and data hiding?
.3:What is the relationship between an object and a class?
.4:In what way, aside from being functions, are class function members different
from class data members?
.5:Define a class to represent a bank account. Data members should include the
depositor's name, the account number (use a string), and the balance.
Member functions should allow the following:
Creating an object and initializing it.
Displaying the depositor's name, account number, and balance
Depositing an amount of money given by an argument
Withdrawing an amount of money given by an argument
Just show the class declaration, not the method implementations.
(Programming Exercise 1 provides you with an opportunity to write the
implementation.)
.6:When are class constructors called? When are class destructors called?
.7:Provide code for a constructor for the bank account class of question 5.
.8:What is a default constructor and what's the advantage of having one?
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
.9:Modify the Stock class (the version in stock2.h) so that it has member
functions that return the values of the individual data members. Note: A
member that returns the company name should not provide a weapon for
altering the array. That is, it can't simply return a char *. It could return a
const pointer, or it could return a pointer to a copy of the array, manufactured
by using new.
.10:What are this and *this?

Programming Exercises
1:Provide method definitions for the class described in review question 5 and write
a short program illustrating all the features.
2:Do programming exercise 1 from Chapter 9, but replace the code shown there
with an appropriate golf class declaration. Replace setgolf(golf &, const char
*, int) with a constructor with the appropriate argument for providing initial
values. Retain the interactive version of setgolf(), but implement it using the
constructor. (For example, for the code for setgolf(), obtain the data, pass the
data to the constructor to create a stemporary object, and assign the temporary
object to the invoking object, which would be *this.)
3:Do programming exercise 2 from Chapter 9, but convert the Sales structure and
its associated functions to a class and its methods. Replace the
setSales(Sales &, double [], int) function with a constructor. Implement the
interactive setSales(Sales &) method using the constructor. Keep the class
within the namespace SALES.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
4:Consider the following structure declaration:
struct customer {
char fullname[35];
double payment;
};
Write a program that adds and removes customer structures from a stack,
represented by a class declaration. Each time a customer is removed, his
payment is added to a running total and the running total is reported. Note: You
should be able to use the Stack class unaltered; just change the typedef
declaration so that Item is type customer instead of unsigned long.
5:Here's a class declaration:
class Move
{
private:

double x;
double y;
public:
Move(double a = 0, double b = 0); // sets x, y to a, b
showmove() const; // shows current x, y values
Move add(const Move & m) const;
// this function adds x of m to x of invoking object to get new x,
// adds y of m to y of invoking object to get new y, creates a new
// move object initialized to new x, y values and returns it
reset(double a = 0, double b = 0); // resets x,y to a, b
};
Supply member function definitions and a program that exercises the class.
6:A Betelgeusean plorg has these properties:
Data
A plorg has a name of no more than 19 letters.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
A plorg has a contentment index (CI), which is an integer.
Operations
A new plorg starts out with a name and a CI value of 50.
A plorg's CI can change.
A plorg can report its name and CI.
The default plorg has a name of "Plorga".
Write a Plorg class declaration (data members and member function
prototypes) that represents a plorg. Write the function definitions for the member
functions. Write a short program that demonstrates all the features of the Plorg
class.
7:We can describe a simple list as follows:
A simple list can hold zero or more items of some particular type.
You can create an empty list.
You can add items to a list.

You can determine if the list is empty.
You can determine if the list is full.
You can visit each item in a list and perform some action upon it.
As you can see, this list really is simple, not allowing insertion or deletion, for
example. The main use of such a list is to provide a simplified programming
project. In this case, create a class matching this description. You can
implement the list as an array or, if you're familiar with the data type, as a linked
list. But the public interface should not depend on your choice. That is, the
public interface should not have array indices, pointers to nodes, and so on. It
should be expressed in the general concepts of creating a list, adding an item to
the list, and so on. The usual way to handle visiting each item and performing an
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
action is to use a function that takes a function pointer as an argument:
void visit(void (*pf)(Item &));
Here pf points to a function (not a member function) that takes a reference to
Item argument, where Item is the type for items in the list. The visit() function
applies this function to each item in the list.
You also should provide a short program utilizing your design.
CONTENTS
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
CONTENTS
Chapter 11. WORKING WITH CLASSES
You will learn about the following in this chapter:
Operator Overloading
Time on Our Hands
Introducing Friends
Overloaded Operators: Member Versus Nonmember Functions
More Overloading: A Vector Class
Automatic Conversions and Type Casts for Classes
Summary

Review Questions
Programming Exercises
C++ classes are feature-rich, complex, and powerful. In Chapter 9, "Memory Models and
Namespaces," you began a journey toward object-oriented programming by learning to
define and use a simple class. You saw how a class defines a data type by defining the
type of data to be used to represent an object and by also defining, through member
functions, the operations that can be performed with that data. And you learned about two
special member functions, the constructor and the destructor, that manage creating and
discarding objects made to a class specification. This chapter will take you a few steps
further in the exploration of class properties, concentrating on class design techniques
rather than on general principles. You'll probably find some of the features covered here
straightforward, some a bit more subtle. To best understand these new features, you
should try the examples and experiment with them. What happens if I use a regular
argument instead of a reference argument for this function? What happens if I leave
something out of a destructor? Don't be afraid to make mistakes; usually you can learn
more from unraveling an error than by doing something correctly, but by rote. (However,
don't assume that a maelstrom of mistakes inevitably leads to incredible insight.) In the
end, you'll be rewarded with a fuller understanding of how C++ works and of what C++ can
do for you.
This chapter starts with operator overloading, which lets you use standard C++ operators
such as = and + with class objects. Then it examines friends, the C++ mechanism for
letting nonmember functions access private data. Finally, it looks at how you can instruct
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
C++ to perform automatic type conversions with classes. As you go through this and the
next chapter, you'll gain a greater appreciation of the roles class constructors and class
destructors play. Also, you'll see some of the stages you may go through as you develop
and improve a class design.
One difficulty with learning C++, at least by the time you've gotten this far into the subject,
is that there is an awful lot to remember. And it's unreasonable to expect to remember it all
until you've logged enough experience on which to hang your memories. Learning C++, in

this respect, is like learning a feature-laden word processor or spreadsheet program. No
one feature is that daunting, but, in practice, most people really know well only those
features they use regularly, such as searching for text or italicizing. You may recall having
read somewhere how to generate alternative characters or create a table of contents, but
those skills probably won't be part of your daily repertoire until you find yourself in a
situation in which you need them frequently. Probably the best approach to absorbing the
wealth of material in this chapter is to begin incorporating just some of these new features
into your own C++ programming. As your experiences enhance your understanding and
appreciation of these features, begin adding other C++ features. As Bjarne Stroustrup, the
creator of C++, suggested at a C++ conference for professional programmers: "Ease
yourself into the language. Don't feel you have to use all of the features, and don't try to
use them all on the first day."
Operator Overloading
Let's look at a technique for giving object operations a prettier look. Operator overloading
is another example of C++ polymorphism. In Chapter 8, "Adventures in Functions," you
saw how C++ enables you to define several functions having the same name as long as
they have different signatures (argument lists). That was function overloading, or functional
polymorphism. Its purpose is to let you use the same function name for the same basic
operation even though you apply the operation to different data types. (Imagine how
awkward English would be if you had to use a different verb form for each different type of
object—lift_lft your left foot, but lift_sp your spoon.) Operator overloading extends the
overloading concept to operators, letting you assign multiple meanings to C++ operators.
Actually, many C++ (and C) operators already are overloaded. For example, the * operator,
when applied to an address, yields the value stored at that address. But applying * to two
numbers yields the product of the values. C++ uses the number and type of operands to
decide which action to take.
C++ lets you extend operator overloading to user-defined types, permitting you, say, to use
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
the + symbol to add two objects. Again, the compiler will use the number and type of
operands to determine which definition of addition to use. Overloaded operators often can

make code look more natural. For example, a common computing task is adding two
arrays. Usually, this winds up looking like the following for loop:
for (int i = 0; i < 20; i++)
evening[i] = sam[i] + janet[i]; // add element by element
But in C++, you can define a class that represents arrays and that overloads the + operator
so that you can do this:
evening = sam + janet; // add two array objects
This simple addition notation conceals the mechanics and emphasizes what is essential,
and that is another OOP goal.
To overload an operator, you use a special function form called an operator function. An
operator function has the form:
operatorop(argument-list)
where op is the symbol for the operator being overloaded. That is, operator+() overloads
the + operator (op is +) and operator*() overloads the * operator (op is *). The op has to
be a valid C++ operator; you can't just make up a new symbol. For example, you can't
have an operator@() function because C++ has no @ operator. But the operator[]()
function would overload the [] operator because [] is the array-indexing operator. Suppose,
for example, that you have a Salesperson class for which you define an operator+()
member function to overload the + operator so that it adds sales figures of one
salesperson object to another. Then, if district2, sid, and sara all are objects of the
Salesperson class, you can write this equation:
district2 = sid + sara;
The compiler, recognizing the operands as belonging to the Salesperson class, will
replace the operator with the corresponding operator function:
district2 = sid.operator+(sara);
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×