0% found this document useful (0 votes)
7 views

Introduction to Variables in C++

This document serves as an introduction to C++, covering its standardization, programming environment, and basic programming concepts. It highlights the importance of C++ as a versatile programming language and discusses its features such as types, objects, and the C++ Standard Library. Additionally, it emphasizes the process of computation and the organization of data in programming.

Uploaded by

Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction to Variables in C++

This document serves as an introduction to C++, covering its standardization, programming environment, and basic programming concepts. It highlights the importance of C++ as a versatile programming language and discusses its features such as types, objects, and the C++ Standard Library. Additionally, it emphasizes the process of computation and the organization of data in programming.

Uploaded by

Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LECTURE 2: INTRODUCTION TO C++

Table of Contents
2.0 Introduction ...................................................................................................................................... 1

2.1 Standardization ..................................................................................................................... 2

2.2 Programming Environment .............................................................................................. 3

2.3 Basic Facilities .................................................................................................................... 4

2.3.1 Types ........................................................................................................................... 4

2.3.2 Objects, declaration, and initialization ......................................................... 5

2.3.3 C++ Standard Library ................................................................................... 5

2.3.4 Separate compilation ............................................................................................. 6

2.4 Computation .......................................................................................................................... 7

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.

Bjarne Stroustrup is the creator of the C++ language:(http://www.stroustrup.com/ )


you can have an overview of C++ history in

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.

Figure 2.2: C++ books.

2.1 Standardization

The C++ Language is an open ISO-standardized language. For a time, C++


had no official standard and was maintained by a de-facto standard, however since
1998, C++ is standardized by a committee of the ISO. Is a compiled language,
C++ compiles directly to a machine’s native code, allowing it to be one of the
fastest languages in the world, if optimized. Is a strongly-typed unsafe language,
C++ is a language that expects the programmer to know what he or she is doing,
but allows for incredible amounts of control as a result.
C++ is standardized. The C++ standard was finalized and adopted by ISO
(International Organization for Standardization) as well as several national standards
organizations. The ISO standard was finalized and adopted by unanimous vote in
November 1997. The latest (and current) standard version was ratified and
published by ISO in December 2014 as ISO/IEC 14882:2014 (informally known as
C++14), as can be seen in Figure 2.3.
Figure 2.3: C++ standards timeline. Source: https://isocpp.org.

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.

2.2 Programming environment

C++ is a compiled language. That means you will need some tools to work with C++:

• Editor: to write your code

• Compiler: translate the source code to machine code to be executed

• Interpreter: reads a little source code, translates it to machine code, and


executes it, than reads a little more, etc.

• 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

There are Integrated Development Environments (IDE) that provides


editors, compilers and linker in as a single package. Examples of IDEs for
C++:

1. Windows:

• Microsoft Visual C++ › http://www.visualstudio.com/


• MingW › http://www.mingw.org/
2. Linux:

• Eclipse › http://www.eclipse.org/cdt/
• QtCreator › http://www.qt.io/ide/

3. Web based:

• C++ Shell › http://cpp.sh/


• CodeChef › https://www.codechef.com/ide

2.3 Basic facilities

The purpose of learning a programming language is to become a better programmer;


that is, to become more e↵ective at designing and implementing new systems and at
maintaining old ones. The most important thing to do is to focus on concepts and not
get lost in language- technical details. This section introduces some C++ program
features necessary to the most basic programs.

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.

• Standard Library types: string, complex, input/output streams, etc.

• user-defined types (more about this later).

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.

Table 2.1: C++ types and literals

Type Literal
bool true,
false
int 12
float 10.2 11.3
char ’c’
string "abcd"

A variable is a named object. A declaration names an object.

2.3.3 C++ Standard Library

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.

Program 5 STD input and output


#include <iostream >
2 #include <st ri ng >
in t main ( ) // read f i r s t and second name
4 {
\
std : : cout << ” pl ea se ent er your f i r s t and second names n” ;
6 std : : s t r i n g f i r s t , second ;
std : : c i n >> f i r s t >> second ; // read two st ri n g s
8 std : : s t r i n g name = f i r s t + ’ ’ + second ; //
std : : cout << ” Hello , ”<< name \<< ’ n ’ ;
10 return 0;
}
12
• the macro #include give access external packages and libraries

• <iostream>: is the STL package for inputing and outputing

• <string>: is the STL package to handle strings of characters

• comments are identified ”//” (single line) or ”/* ... */” (multiple lines).

• the main scope defines the piece of code that will be the final program.

• a C++ statement end with ”;”

• ::std : indicates that a name is defined in the standard library namespace

• operator cin >> : reads from streams

• operator cout << : puts objects in streams.


• return a value indicating success (usually 0).

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.

2.3.4 Separate compilation

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.

your source your object


code:
hello.cpp C++ Compiler code:
hello.o

C++ Linker

executabl Library object code:


e:
hello.ex STD
e
Figure 2.4: Example of the building process

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 is what we do to manipulate objects. To program, we have to


think what is computable and how best to compute it. To do this we think
about abstractions, algorithms, heuristics, data structures. We use language to
construct these ideas in terms of sequential order of execution, expressions and
statements, selection, interation, functions and data structures (for example
vectors).

Computation:
(input) data (output) data
code (a lot, and often messy)

Figure 2.5: A typical program

• 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

• Output: data sent to screen, files, other output devices, other


programs, other parts of a program

Computations should be expressed, correctly, simply and efficiently. To


achieve that, different strategies can be adopted, e.g., divide and conquer
(breaking up big computations into many little ones), or abstraction
(provide a higher-level concept that hides details not relevant to the problem
solution). Organization of data is often the key to good code: input/output
formats; protocols; data structures.
Expressions are the most basic building blocks of a program. An
expression computes a value from a number of operands. An expression
produces a single value. Table 2.3 shows some examples of expressions.

Table 2.3: Expressions

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:

• mathematical operators: +, *, % (reminder) ... :

• logical operators: == (equal), != (not equal), && (logical AND), || (logical OR)

• increment/decrement operators: ++lval, --lval, ... (lval is short


for value that can appear on the left-hand side os an assignment)

Statements are language features used to produce several values, or to


do something many times, or choose among alternatives:

• selection: if-statements; switch-statements.

• iteration: while-statements; for-statements

Program 6 shows some computation using mathematical operators, such as


+, ⇤, / and a mathematical function (sqrt). Program 7 also shows some
computation using iteration statements.

You might also like