Practical C++ Programming by manish baranwal

Start from the beginning
                                        

strings.

One of the things new to this edition is the introduction of safety code. Starting with the section Bounds

Errors (page 54) the assert statement explained. Since I wrote the first edition, I have encounter a huge

mass of badly written code. It was only the extensive use of asserts which made it possible to debug the

thing. These statements prevented array boundary errors which could cause random problems in the

code.

The use of error checking code and asserts should be highly encouraged.

The book discusses C Style strings because there are a lot of programs out there that use them. Your

students should avoid them because they force you to handle your own storage. (C++ Style strings

automatically allocate their own storage.)

This chapter introduces the student to all the modifiers such as short, long, and register. Some, like

short and double, the student can use immediately. Some, like register, will be discussed in later

chapters. A few, like volatile, are so advanced that this course does not cover them at all.

At this stage of the game, reference declarations are not useful. In fact putting them in a program tends

to confuse things. However, you will find them very useful when we start using them to declare function

Page 14

parameters later in the book.

All of the modifiers have been included in this chapter for completeness.

C++ contains a large number of shortcut operators. This makes programming more compact and

concise. Unfortunately, the overuse of these operators can cause problems.

Verbose programs that work are much better than terse programs that do not.

I believe that the goal of programming is not compactness, but clarity and a working program. For that

reason I don’t allow my students to use shortcut operators such as ++ and -- inside other statements.

Side effects such as those caused by ++ and -- inside other statements can easily cause trouble. What’s

worse, the type of trouble they cause is difficult to detect. Avoid side effects.

Live Demonstration

Slide 3 five/five.cpp

Slide 24 name2/name2.cpp

Slide 26 len/len.cpp

Classroom Presentation Suggestions

On Slide 20 we ask the question “Are all ‘C Style strings’ ‘arrays of characters’?” The answer is “Yes.”

A string is a special form of an array of characters. A string starts at the first element of the array and

continues until the end-of-string character (‘\0’).

Note: The declaration

char data[10];

defines a 10-character array. As an array it holds 10 characters, no more, no less. It can hold strings of

length 0 to 9. Why not 10? Because we need one location to store the end-of-string character.

Are all “character arrays” “C Style strings”?

You've reached the end of published parts.

⏰ Last updated: Nov 04, 2012 ⏰

Add this story to your Library to get notified about new parts!

Practical C++ Programming by manish baranwalWhere stories live. Discover now