background preloader

Learning Cpp

Facebook Twitter

Tutorials - The Static Keyword in C. Most C++ keywords allow you to do one thing.

Tutorials - The Static Keyword in C

You use int to declare an int, or that a function returns an int or expects an int as an argument. You use new to allocate memory, and delete to free memory. You use const to indicate that a variable's value cannot be changed. Ironically, the keyword static, though the word means "unchanging," has multiple (and apparently unrelated) uses. The keyword static can be used in three major contexts: inside a function, inside a class definition, and in front of a global variable inside a file making up a multifile program. The use of static inside a function is the simplest.

You can also use static in this fashion to prevent a variable from being reinitialized inside a loop. For(int x=0; x<10; x++) { for(int y=0; y<10; y++) { static int number_of_times = 0; number_of_times++; } } Const Correctness - C++ Tutorials. The const keyword allows you to specify whether or not a variable is modifiable.

Const Correctness - C++ Tutorials

You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced). But why do you care? Const gives you the ability to document your program more clearly and actually enforce that documentation. By enforcing your documentation, the const keyword provides guarantees to your users that allow you to make performance optimizations without the threat of damaging their data. For instance, const references allow you to specify that the data referred to won't be changed; this means that you can use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. Documentation and Safety The primary purpose of constness is to provide documentation and prevent programming mistakes. Syntax Note. The C++ 'const' Declaration: Why & How. The 'const' system is one of the really messy features of C++.

The C++ 'const' Declaration: Why & How

It is simple in concept: variables declared with ‘const’ added become constants and cannot be altered by the program. However it is also used to bodge in a substitute for one of the missing features of C++ and there it gets horridly complicated and sometimes frustratingly restrictive. The following attempts to explain how 'const' is used and why it exists. Simple Use of ‘const’ The simplest use is to declare a named constant. To do this, one declares a constant as if it was a variable but add ‘const’ before it. Const int Constant1=96; will create an integer constant, unimaginatively called ‘Constant1’, with the value 96. Such constants are useful for parameters which are used in the program but do not need to be changed after the program is compiled. C++ Tutorial: Stream Format States and Stream Manipulators (Page 1) This tutorial introduces ANSI/ISO C++'s stream formatting capabilties, including stream manipulators, justification, padding, integer formats, floating-point number formats, uppercase/lowercase control, formatting booleans as strings, and setting and restoring stream format states.

C++ Tutorial: Stream Format States and Stream Manipulators (Page 1)

This tutorial is intented for students and developers who are familiar with basic C++ input and output techniques using cin and cout. The techniques shown here can also be applied to file-based streams or other streams that extend the standard C++ stream class hierarchy. Download the code examples for this tutorial. [Note: This tutorial is an excerpt (Section 15.7) of Chapter 15, Stream Input/Output, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Character Traits. Description Several library components, including strings, need to perform operations on characters.

Character Traits

A Character Traits class is similar to a function object: it encapsulates some information about a particular character type, and some operations on that type. Note that every member of a Character Traits class is static. There is never any need to create a Character Traits object, and, in fact, there is no guarantee that creating such objects is possible. Refinement of Character Traits is not a refinement of any other concept. Associated types Notation Valid Expressions Expression semantics Complexity guarantees length, find, move, copy, and the range version of assign are linear in n. All other operations are constant time.

Models Notes See also string STL Main Page. C++ STL Tutorial. The Standard Template Libraries (STL's) are a set of C++ template classes to provide common programming data structures and functions such as doubly linked lists (list), paired arrays (map), expandable arrays (vector), large string storage and manipulation (rope), etc.

C++ STL Tutorial

The STL library is available from the STL home page. This is also your best detailed reference for all of the STL class functions available. Also see our C++ Templates tutorial STL can be categorized into the following groupings: Container classes: Sequences: vector: (this tutorial) Dynamic array of variables, struct or objects. Also see the YoLinux.com GDB tutorial on dereferencing STL containers. vector: Dynamic array of variables, struct or objects.

Cplusplus.com - The C++ Resources Network.