Introduction to Variables in C++
Introduction to Variables in C++
Table of Contents
2.0 Introduction ...................................................................................................................................... 1
2.0 Introduction
Why C++? You can not learn to program without a programming language, the
purpose of a programming language is to allow you to express your ideas in code.
C++ is the language that most directly allows you to express ideas from the largest
number of application areas. Programming concepts that you learn using C++ can
be used fairly directly in other languages, including C, Java, C sharp and (less
directly) Fortran.
http://www.cplusplus.com/info/history/.
Bjarne has written several books about programming and C++ such as the ones
shown bellow. Programming - Principles and Practices Using C++ is the one
used in this course.
Figure 2.1: Bjarne Stroustroup, creator of C++ language.
2.1 Standardization
As one of the most frequently used languages in the world and as an open language,
C++ has a wide range of compilers that run on many different platforms that support
it.
Sometimes the latest standard is not 100% supported by all compilers. Check
http://en.cppreference.com/w/cpp/compiler to see features, versions and several
compilers compliance.
C++ is a compiled language. That means you will need some tools to work with C++:
• Debugger: helps you step through code, shows you variables and flow of execution
• Linker: connects code from libraries with your code to make one executable
1. Windows:
• Eclipse › http://www.eclipse.org/cdt/
• QtCreator › http://www.qt.io/ide/
3. Web based:
2.3.1 Types
In typed languages, such as C++, every operation defines types of data to which
an opera- tion is applicable, with the implication that it is not applicable to other
types. For example, ”this text between the quotes” is a string, and 10 is a number.
In most languages the division of a number by a string (or vice-versa) has no
meaning and the compiler will reject it. This is static type checking. In C++ you
can have:
• built-in types: bool, char, float, int (short and long), etc.
For each type an operand have a particular semantics. The type of a variable
determines which operations are valid and what their meanings are for that type. For
example:
Strings (STD) Integers (built-in)
cin >> reads a word cin >> reads a number
cout << writes a word cout << writes a word
+ concatenates + adds
+= s adds the string s at += n increments the int by
end n
++ is an error ++ is n increments by 1
— is an error — subtracts
2.3.2 Objects, declaration and initialization
In the computer memory, everything is just bits; type is what gives meaning to the bits.
Some types and literals in C++ can be seen in Table 2.1. An object is some
memory that can hold a value of a given type.
Type Literal
bool true,
false
int 12
float 10.2 11.3
char ’c’
string "abcd"
The C++ Standard Library is a collection types and functions, which are written in
the core language and part of the C++ ISO Standard itself. It provides a ready-made
set of common and highly used features for C++. Program 5 shows a very simple
using the Standard Library.
• comments are identified ”//” (single line) or ”/* ... */” (multiple lines).
• the main scope defines the piece of code that will be the final program.
Only lines 6 to 9 directly do anything. That’s normal. Much of the code in our
programs come from someone else (libraries). Would you rather write 1,000,000 lines of
machine code? Code that exclusively uses C++’s standard library will run on many
platforms with few to no changes and is upwards compatible with C.
C++ supports separate compilation, that can be used to organize a program into a set of
semi- independent fragments. An executable (final application) is the result of linking
and compiling some piece of code that has one main block of statements. Pieces of code
that do not have a main (libraries), when compiled produce object code to be linked with
other code to produce a final application. Figure 2.4 shows how is the building process
happens to generate an executable program.
C++ Linker
The interface is placed in a header file that is included by the users, whereas
its implementation is defined in another file. To use a library you have to
have access to its interface files at compilation time and to its object code at
linking time. Open source libraries also make its implementation files
available.
2.4 Computation
Computation:
(input) data (output) data
code (a lot, and often messy)
• Input: data from keyboard, files, other input devices, other programs,
other parts of a program
• Computation: what our program will do with the input to produce the output
Expression Example
Literals 10, ’a’, 3.14, "Norah"
Names of variables int lenght;
Combinations perimeter = (length+width)*2;
Constant expressions constexpr double pi=3.141516;
Most Operators are conventional and you can look up details if and when you find a
need.
But a list of the most common operators include:
• logical operators: == (equal), != (not equal), && (logical AND), || (logical OR)