Modifying Our First C++ Program

This section continues our introduction to C++ programming with two examples, showing how to modify the program in Fig. 2.1 to print text on one line by using multiple statements and to print text on several lines by using a single statement.

Printing a Single Line of Text with Multiple Statements

Welcome to C++! can be printed several ways. For example, Fig. 2.3 performs stream insertion in multiple statements (lines 89), yet produces the same output as the program of Fig. 2.1. [Note: From this point forward, we use a darker shade of gray than the code table background to highlight the key features each program introduces.] Each stream insertion resumes printing where the previous one stopped. The first stream insertion (line 8) prints Welcome followed by a space, and the second stream insertion (line 9) begins printing on the same line immediately following the space. In general, C++ allows the programmer to express statements in a variety of ways.

Figure 2.3. Printing a line of text with multiple statements.

 1 // Fig. 2.3: fig02_03.cpp
 2 // Printing a line of text with multiple statements.
 3 #include  // allows program to output data to the screen
 4
 5 // function main begins program execution
 6 int main()
 7 {
 8 std::cout << "Welcome "; 
 9 std::cout << "to C++!
";
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main
 
 Welcome to C++!
 

Printing Multiple Lines of Text with a Single Statement

A single statement can print multiple lines by using newline characters, as in line 8 of Fig. 2.4. Each time the (newline) escape sequence is encountered in the output stream, the screen cursor is positioned to the beginning of the next line. To get a blank line in your output, place two newline characters back to back, as in line 8.


Figure 2.4. Printing multiple lines of text with a single statement.

 1 // Fig. 2.4: fig02_04.cpp
 2 // Printing multiple lines of text with a single statement.
 3 #include  // allows program to output data to the screen
 4
 5 // function main begins program execution
 6 int main()
 7 {
 8 std::cout << "Welcome
to

 C++!
";
 9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
 
 Welcome
 to

 C++!
 

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net