C++ First Example

Throughout this book we will use code examples to explain and illustrate important programming and OOP issues. Our aim in each case is to use a minimal example that will illustrate the ideas and techniques briefly and efficiently. Example 1.3 provides a quick overview of some elementary C++ syntax.

Example 1.3. src/early-examples/fac.cpp

// Computes and prints n! for a given n.
// Uses several basic elements of C++.

#include  <-- 1

int main() { <-- 2
 using namespace std; <-- 3
 /* Declarations of variables */
 int factArg = 0 ; <-- 4
 int fact (1) ; <-- 5
 do { <-- 6
 cout << "Factorial of: "; <-- 7
 cin >> factArg; <-- 8
 if( factArg < 0 ) {
 cout << "No negative values, please!" << endl;
 } <-- 9
 } while(factArg < 0) ; <-- 10
 int i = 2;
 while( i <= factArg ) { <-- 11
 fact = fact * i;
 i = i + 1;
 } <-- 12
 cout << "The Factorial of " << factArg << " is: " << fact << endl;
 return 0; <-- 13
} <-- 14
 

(1)Standard c++ library. In older versions of C++, you might find instead, but that version is regarded as obsolete or "deprecated."

(2)start of function "main," which returns int

(3)Permits us to use the symbols cin, cout, and endl without prefixing each name with std::

(4)C style initialization syntax

(5)C++ style initialization syntax

(6)start of "do..while" loop

(7)write to standard output

(8)read from standard input and convert to int

(9)end of if block

(10)if false, break out of do loop

(11)start of while loop

(12)end of while block

(13)When main returns 0, that normally means "no errors."

(14)end of main block

On most platforms it is simple to compile and run this program using the ubiquitous GNU C compiler, gcc. The command to compile a C++ program is g++, but this is simply an alias to gcc with some C++ options switched on.

When invoking the compiler, we recommend always maximizing the amount of diagnostic information that is available from the compilation process. Accordingly, we include three switches on the command line: (1) -ansi (which turns off GNU extensions that conflict with ISO C++), (2) -pedantic (which issues all the warnings that are demanded by strict ISO C++ and rejects any program that uses forbidden extensions), and (3) -Wall (which enables all possible warnings about constructions that might be considered questionable even if they conform to the standard).

src/early-examples> g++ -ansi -pedantic -Wall fac.cpp

or

src/early-examples> g++ -ansi -pedantic -Wall -o execFile fac.cpp

In the second version shown above, -o execFile is an optional switch that tells the compiler to name the executable result of the compilation "execFile." If we omit that switch, as in the first version, the compiler will produce an executable file named a.out. In either case, if there already exists a file in the same directory with the name of our target executable, then the compiler will quietly and automatically overwrite it.

We have mentioned here just a few of the most commonly used compiler switches. On a *nix system you can view the manual pages, a summary of the g++ command options and how they are used, by typing the command

man g++

The command

info g++

often displays more readable documentation. One advantage is that the info command allows you to do an incremental search for a word in the documentation by typing ctrl-s. On most systems these commands allow you to browse the online documentation for g++ one screen at a time. For more complete gcc documentation, visit the GNU online document center.[8]

[8] http://www.gnu.org/software/gcc/onlinedocs/

After it has been compiled successfully, our program can be run by typing the name of the executable file. Here is an example done on a *nix platform:

src/early-examples> ./a.out
Factorial of: -3
No negative values, please!
Factorial of: 5
The Factorial of 5 is: 120
src/early-examples>

This short program has several of the language elements that show up in most C++ programs. Some interesting differences between C++ and other languages (especially C) can be seen in this example.

Comments

C++ has single-line comments as in Java. Any text between the // and the end of the line is a comment. C-style comment delimiters for multiline comments can also be used: The text between /* and */ is a comment.

#include

To reuse functions, types, or identifiers from libraries, we use the preprocessor directive #include (see Section C.1). As in C, all preprocessor directives begin with the pound sign character, #, and are evaluated just before the compiler compiles your code. In this example, the included header contains the Standard Library definitions for input/output.

The Using Namespace Directive

Symbols from the Standard Library (see Appendix B) are all enclosed in the namespace std.

A namespace (see Section 20.4) is a collection of classes, functions, and objects that can be addressed with a name prefix. The using declaration tells the compiler to add all symbols from the namespace std to our global namespace.

Declaring and Initializing Variables

Variable declarations come in three styles in C++:

type-expr variableName;
type-expr variableName = init-expr;
type-expr variableName (init-expr);

In the first form, the variable might not be initialized. The third form is an alternate syntax for the second.

Selection

C++ provides the usual assortment of syntax variations for selection, which we discuss in Section 21.2.

Iteration

We used two of the three iteration structures provided by C++ in our example. All three are discussed in Section 21.3.


Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

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