Skip to main content
main-content

Über dieses Buch

The C++ Quick Syntax Reference is a condensed code and syntax reference to the C++ programming language. It presents the essential C++ syntax in a well-organized format that can be used as a handy reference.

You won’t find any technical jargon, bloated samples, drawn out history lessons, or witty stories in this book. What you will find is a language reference that is concise, to the point and highly accessible. The book is packed with useful information and is a must-have for any C++ programmer.

In the C++ Quick Syntax Reference, you will find:

A concise reference to the C++ language syntax. Short, simple, and focused code examples. A well laid out table of contents and a comprehensive index allowing easy review.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Hello World

Abstract
To begin developing in C++ you should download and install an Integrated Development Environment (IDE) that supports C++. A good choice is Microsoft's own Visual Studio. If you do not have Visual Studio but would like to try out the examples in this book in a similar environment you can download Visual Studio Express from Microsoft's website. This is a lightweight version of Visual Studio that is available for free. Alternatively, you can develop using a simple text editor – such as Notepad – although this is less convenient than using an IDE. If you choose to do so, just create an empty document with a .cpp file extension and open it in the editor of your choice.
Mikael Olsson

Chapter 2. Compile and Run

Abstract
Continuing from the last chapter, the Hello World program is now complete and ready to be compiled and run. You can do this by going to the Debug menu and clicking on Start Without Debugging (Ctrl + F5). Visual Studio then compiles and runs the application which displays the text in a console window.
Mikael Olsson

Chapter 3. Variables

Abstract
Variables are used for storing data during program execution.
Mikael Olsson

Chapter 4. Operators

Abstract
The numerical operators in C++ can be grouped into five types: arithmetic, assignment, comparison, logical and bitwise operators.
Mikael Olsson

Chapter 5. Pointers

Abstract
A pointer is a variable that contains the memory address of another variable, called the pointee.
Mikael Olsson

Chapter 6. References

Abstract
References allow a programmer to create a new name for a variable. They provide a simpler, safer and less powerful alternative to pointers.
Mikael Olsson

Chapter 7. Arrays

Abstract
An array is a data structure used for storing a collection of values that all have the same date type.
Mikael Olsson

Chapter 8. String

Abstract
The string class in C++ is used to store string values. Before a string can be declared the string header must first be included. The standard namespace can also be included since the string class is part of that namespace.
Mikael Olsson

Chapter 9. Conditionals

Abstract
Conditional statements are used to execute different code blocks based on different conditions.
Mikael Olsson

Chapter 10. Loops

Abstract
There are three looping structures available in C++, all of which are used to execute a specific code block multiple times. Just as with the conditional if statement, the curly brackets for the loops can be left out if there is only one statement in the code block.
Mikael Olsson

Chapter 11. Functions

Abstract
A function is a reusable code block that will only execute when called.
Mikael Olsson

Chapter 12. Class

Abstract
A class is a template used to create objects. To define one the class keyword is used followed by a name, a code block and a semicolon. The naming convention for classes is mixed case, meaning that each word should be initially capitalized.
Mikael Olsson

Chapter 13. Constructor

Abstract
In addition to fields and methods, a class can contain a constructor. This is a special kind of method used to construct, or instantiate, the object. It always has the same name as the class and does not have a return type. To be accessible from another class the constructor needs to be declared in a section marked with the public access modifier.
Mikael Olsson

Chapter 14. Inheritance

Abstract
Inheritance allows a class to acquire the members of another class. In the example below, Square inherits from Rectangle. This is specified after the class name by using a colon followed by the public keyword, and the name of the class to inherit from. Rectangle then becomes a base class of Square, which in turn becomes a derived class of Rectangle. In addition to its own members, Square gains all accessible members in Rectangle, except for its constructors and destructor.
Mikael Olsson

Chapter 15. Overriding

Abstract
A new method in a derived class can redefine a method in a base class in order to give it a new implementation.
Mikael Olsson

Chapter 16. Access Levels

Abstract
Every class member has an accessibility level that determines where the member will be visible. There are three of them available in C++: public, protected and private. The default access level for class members is private. To change the access level for a section of code, an access modifier is used followed by a colon. Every field or method that comes after this label will have the specified access level, until another access level is set or the class declaration ends.
Mikael Olsson

Chapter 17. Static

Abstract
The static keyword is used to create class members that exist in only one copy, which belongs to the class itself. These members are shared among all instances of the class. This is different from instance (non-static) members, which are created as new copies for each new object.
Mikael Olsson

Chapter 18. Enum

Abstract
Enum is a user-defined type consisting of a fixed list of named constants. In the example below, the enumeration type is called Color and contains three constants: Red, Green and Blue.
Mikael Olsson

Chapter 19. Struct and Union

Abstract
A struct in C++ is equivalent to a class, except that members of a struct default to public access, instead of private access as in classes. By convention, structs are used instead of classes to represent simple data structures that mainly contain public fields.
Mikael Olsson

Chapter 20. Operator Overloading

Abstract
Operator overloading allows operators to be redefined and used where one or both of the operands are of a user-defined class. When done correctly, this can simplify the code and make user-defined types as easy to use as the primitive types.
Mikael Olsson

Chapter 21. Custom Conversions

Abstract
This chapter covers how custom type conversions for an object can be defined. In the example below, there is a class called MyNum with a single integer field. With custom type conversions it is possible to allow integer types to be implicitly converted to this object's type.
Mikael Olsson

Chapter 22. Namespaces

Abstract
Namespaces are used to avoid naming conflicts by allowing entities, such as classes and functions, to be grouped under a separate scope. In the example below there are two classes that belong to the global scope. Since both classes share the same name and scope the code will not compile.
Mikael Olsson

Chapter 23. Constants

Abstract
A constant is a variable that has a value which cannot be changed once the constant has been assigned.
Mikael Olsson

Chapter 24. Preprocessor

Abstract
The preprocessor is a program invoked by the compiler that modifies the source code before the actual compilation takes place. This modification is done according to the preprocessor directives that are included in the source files. The directives are easily distinguished from normal programming code in that they all start with a hash sign (#). They must always appear as the first non-whitespace character on a line. Below is a table of the preprocessor directives available in C++ and their function.
Mikael Olsson

Chapter 25. Exception Handling

Abstract
Exception handling allows programmers to deal with unexpected situations that may occur in a program.
Mikael Olsson

Chapter 26. Type Conversions

Abstract
Converting an expression from one type to another is known as type-conversion. This can be done either implicitly or explicitly.
Mikael Olsson

Chapter 27. Templates

Abstract
Templates provide a way to make a class or function operate with different data types without having to rewrite the code for each type.
Mikael Olsson

Chapter 28. Headers

Abstract
When a project grows it is common to split the code up into different source files. When this happens the interface and implementation are generally separated. The interface is placed in a header file, which commonly has the same name as the source file and a .h file extension. This header file contains forward declarations for the source file entities that need to be accessible to other files in the project.
Mikael Olsson

Backmatter

Weitere Informationen

Premium Partner

    Bildnachweise