Waite Group's C++ Primer Plus

Waite Group's C++ Primer Plus

4.2 7
by Stephen Prata

MWSS: C++ Primer Plus has been skillfully adapted as part of the Signature Series, personally developed by Mitchell Waite. It delivers clear, thorough coverage of a classic programming topic in tutorial format. MWSS: C++ Primer Plus Second Edition covers important OOP concepts like classes, inheritance, templates and exceptions. It shows how to handle input and…  See more details below


MWSS: C++ Primer Plus has been skillfully adapted as part of the Signature Series, personally developed by Mitchell Waite. It delivers clear, thorough coverage of a classic programming topic in tutorial format. MWSS: C++ Primer Plus Second Edition covers important OOP concepts like classes, inheritance, templates and exceptions. It shows how to handle input and output, make programs perform repetitive tasks and select choice, manipulate data, hide information, use functions, and build flexible, easily modified programs. MWSS: C++ Primer Plus includes lessons on namespaces, friends, and RTTI, and introduces string classes and the new Standard Template Library to keep the reader current with the latest C++ additions.

  • Developed by Mitchell Waite, this book walks readers through the basics of object-oriented programming while covering the essentials of C++
  • Coverage of new library features such as string class and Standard Template Library (STL), one of the most significant changes since the second edition
  • STL is a library of classes and functions that provide useful and efficient set of generic containers and algorithms

Read More

Product Details

Publication date:
Edition description:
Older Edition
Product dimensions:
7.42(w) x 9.09(h) x 2.04(d)

Read an Excerpt

From Chapter Ten: Working with Classes

You will learn about the following in this chapter:

  • Operator overloading

  • Firend functions

  • Overloading the << operator for output

  • State members

    Using rand() to generate random values

  • Automatic conversions and type casts for classes

  • Class conversion functions

C++ classes are feature-rich, complex, and powerful In Chapter 9, 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 rewardedwith 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 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 its 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, 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 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

We'll do just that in Chapter 12. (Why not now? Because you also have to overload the [ ] operator, and that's a bit more involved than overloading the + operator.) This simple addition notation conceals the mechanics and emphasizes what is essential, and that is another 00P goal.

To overload in 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 in 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);

The function will then use the sid object implicitly (because it invoked the method) and the sara object explicitly (because it's passed as an argument) to calculate the sum, which it then returns. Of course, the nice part is that you can use the nifty + operator notation instead of the clunky function notation.

C++ imposes some restrictions on operator overloading, but they're easier to understand alter you've seen how overloading works. So let's develop a few examples first to clarify the process and then discuss the limitations.

Time on Our Hands

If you worked on the Priggs account 2 hours 35 minutes in the morning and 2 hours 40 Minutes in the afternoon, how long did you work altogether on the account? Here's an example where the concept of addition makes sense, but the units that you are adding (a mixture of hours and minutes ) doesn't match a built-in type. Chapter 7 handled a similar case by defining a travel_time structure and a sum( ) function for adding such structures. Now we can generalize that to a Time class using a method to handle addition, Let's begin with an ordinary method, then see how to convert it to an overloaded operator. Listing 10.1 shows the class declaration...

Read More

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >

Waite Group's C++ Primer Plus 4.3 out of 5 based on 0 ratings. 7 reviews.
Guest More than 1 year ago
This is a well written and easy to understand book. With it I was able to understand several concepts which had evaded me while reading other books on C++. This is a must have.
Guest More than 1 year ago
Mr. Prata is absolutely a brilliant teacher. He takes the subject of programming in C++, something many others have made look very difficult, and makes it a walk in the park. His book is filled with humerous analogies and he really takes the care to teach you from the viewpoint that the reason you're reading this book is because you 'don't know how to program in c++' and that you want to learn. And he walks you through it in little baby steps, if you will, but that's what makes it easy to learn. Many authors have no trouble teaching and illustrating how to write a program in C++ that does the 'hello world' thing. But then by the time you get to later chapters in their book, your like, 'This isn't so simple anymore.' But Mr. Prata keeps it just as simple in the later chapters of his book as he does at the beginning. He's a rarity amonth teachers in his effortless ability to simply the complexe. My only complaint is that I was only able to rate his book with 5 stars at the most, when I really believe his work deserves 100 stars!
Guest More than 1 year ago
I'm an intermediate C++ programmer and I found this book really helpful. It explains everything in a clear and easy to understand manner. Anyone could literally pick up this book and know a lot of C++ in one day. The only possible flaw I can find is sometimes the book gets a little ahead of itself, for example on page 120 the program example uses an if-else statement, before there was any mention of what it was. This is minor, as you'd have to be not too smart to not figure out what was going on there. In short, if you're a C++ student, buy this book! Its well worth the money, you won't regret it.
Guest More than 1 year ago
This book will provide a great introduction to a major programming language, C++. No prior knowledge is required. Buy this and do the BN C++ Intro course.
Guest More than 1 year ago
Most of the information found in here, is not very straight forward. I learned more about programming from reading the help files on my compiler.
Guest More than 1 year ago
I had been looking for this book as preferred reading for a class on C++. Initially I was hesitant because of a couple reviews indicating this book was poorly written. Having examined other tutorials & so-called Dummies books, I finally purchased this one and am not disappointed at all!! Just the first two chapters have me up and running (pun intended). I don't what the negative reviewers were looking for, but doubt if they will ever find it!!
Guest More than 1 year ago
I am a C++ student and must disagree with Andrew Kim. This book explains the concepts, code, and logic in plain English and is extremely easy to understand. Just don't try to move on to the complicated sujects until you get a good grasp on the first 6 chapters. It is also a very good idea to do the excercises at the end of each chapter. The book also takes you into more advanced topics which require better than average knowledge on C++. I strongly recommend this to any beginning programmer.