C++ Language

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

-----------------------------C++ Language--------------------------

Machine code: It is basically the same as machine language and it is a set of instructions that a
CPU natively understands (Zeroes and ones).

Assembly Language: Higher level language in which each instruction is identified by a short name,
needing to be translated (assembled) into machine code.

Languages Types

Compiler: is a program that reads code and produces a stand-alone executable program that the
CPU can directly understand.

C, C++ and Pascal are compiled.

Interpreter: is a program that directly executes your code without compiling it into machine code
first.

Pear & Javascript are interpreted.


CRUCIAL STEPS FOR DEVELOPING PROGRAMS IN C++

1. DEFINE the problem to be solved.


2. DESIGN a solution.
3. WRITE a program that implements the solution.
4. COMPILE the program.
5. LINK object files.
6. TEST & DEBUG program.

When compiling (.o) files are created which are the compiled version of the Source File (.cpp)

The LINKER is a program which also comes to play an important part in the compiling process since
it links all the object files generated by the compiler into a single executable.

IDE TIPS

When creating a project in an IDE (Integrated Developement Environment) you are set by default
to the DEBUG configuration. This includes all the extra code and repositories needed to debug the
program. Once the program code has been completed, the RELEASE configuration should be used.
This setting will compile your program without the debugging tools, making it a much smaller file
size andf thus increasing compiling speed.

STATEMENTS

Terminated ussualy with a semicolon (;)

int x; is a declaration statement since it declares that X will hold and integer.

int x = 5; is an assignment statement. It assigns a value (5) to a variable (x).

std::cout << x; is an output statement. It outputs the value of x (which we set to 5 in the
previous statement) to the screen.

A RETURN STATEMENT indicates to the OS whether the program was run successfully or not.

EXPRESSIONS - Mathematical entity that evaluated to a value.

x = 2 + 3;

FUNCTIONS - Collection of statements that executes sequentially

Every C++ program must contain a special function called main. When the C++ program is run,
execution starts with the first statement inside of function main.
PREPROCESSOR DIRECTIVES (or libraries)

Tell the compiler to perform a special task like in #include <iostream> which orders the compiler
to include the iostream library.

COMMENTS

A comment is a line (or multiple lines) of text that are inserted into the source code to explain
what the code is doing. In C++ there are two kinds of comments.

The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to
the end of the line.

The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the
symbols is ignored.

OBJECTS

An object is a piece of memory that can be used to store values. When an object is defined, a
piece of that memory is set aside for the object.

Most of the objects in C++ come in the form of variables.

VARIABLES

A variable in C++ is simply an object that has a name. All variables in C++ as treated as l-values
(refers to a value that has an address in memory).

Variables always go on the left side of statements.

The opposite are R-values, which refers to any value that can be assigned to an l-value.
ASSIGNMENT VS INITIALIZATION

After a variable is defined, a value may be assigned to it via the assignment operator (the = sign):

int x; This is a variable definition

x = 5; Assign the value 5 to variable x

C++ will let you both define a variable AND give it an initial value in the same step. This is called
initialization.

int x = 5; Initialize variable x with the value 5

A variable that has not been given a known value (through initialization or assignment) is called an
uninitialized variable.

std::cout

As noted in previous sections, the std::cout object (in the iostream library) can be used to output
text to the console. To print more than one thing on the same line, the output operator (<<) can
be used multiple times. For example:
std::endl

If we want to print things to more than one line, we can do that by using std::endl. When used
with std::cout, std::endl inserts a newline character (causing the cursor to go to the start of the
next line).

std::cin

Is the opposite of std::cout -- whereas std::cout prints data to the console using the output
operator (<<), std::cin reads input from the user at the console using the input operator (>>).
FUNCTIONS

A function is a reusable sequence of statements designed to do a particular job.

A function call is an expression that tells the CPU to interrupt the current function and execute
another function. The function initiating the function call is called the caller, and the function
being called is the callee or called function.

A function can only return 1 value.

RETURN TYPES OF FUNCTIONS

The actual value returned from a function is called the return value. The word void is used for
functions that do not return a value. We can set the return type of a function as shown:

ARGUMENTS OF FUNCTIONS

An argument is a value that is passed from the caller to the function when a function call is made:
When a function is called, all of the parameters of the function are created as variables, and the
value of each of the arguments is copied into the matching parameter. This process is called pass
by value.

When a function becomes too long, too complicated, or hard to understand, it should be split into
multiple sub-functions. This is called refactoring.

KEYWORDS

C++ reserves a set of 84 words (as of C++14) for its own use. These words are called keywords (or
reserved words), and each of these keywords has a special meaning within the C++ language.
IDENTIFIER NAMING RULES

The name of a variable, function, type, or other kind of object in C++ is called an identifier.

The identifier cannot be a keyword. Keywords are reserved.

The identifier can only be composed of letters (lower or upper case), numbers, and the
underscore character. That means the name cannot contain symbols (except the underscore) nor
whitespace (spaces or tabs). The identifier must begin with a letter (lower or upper case) or an
underscore. It cannot start with a number.

C++ distinguishes between lower and upper case letters. nvalue is different than nValue is
different than NVALUE.

LOCAL SCOPE

A variable’s scope determines when a variable can be seen and used during the time it is
instantiated. Function parameters and variables defined inside the function body both have local
scope. That is, those variables can only be seen and used within the function that defines them.

Local variables are created at the point of definition, and destroyed when they go out of scope
(usually at the end of the function).

EXPRESSIONS

An expression is a combination of literals, variables, functions, and operators that evaluates to a


value.

A literal is a fixed value that has been inserted (hardcoded) directly into the source code, such as
5, or 3.14159.
Literals, variables, and function calls that return values are all known as operands. Operands
supply the data that the expression works with. We just introduced literals, which evaluate to
them. Variables evaluate to the values they hold. Functions evaluate to produce a value of the
function’s return type (unless the return type is void).

Unary operators act on one operand. An example of a unary operator is the - operator. In the
expression -5, the - operator is only being applied to one operand (5) to produce a new value (-5).

Binary operators act on two operands (known as left and right). An example of a binary operator is
the + operator. In the expression 3 + 4, the + operator is working with a left operand (3) and a right
operand (4) to produce a new value (7).

Ternary operators act on three operands. There is only one of these in C++, which we’ll cover
later.

FUNCTION PROTOTYPES AND FORWARD DECLARATION OF FUNCTIONS

A forward declaration allows us to tell the compiler about the existence of an identifier before
actually defining the identifier.

To write a forward declaration for a function, we use a declaration statement called a function
prototype. The function prototype consists of the function’s return type, name, parameters, but
no function body (the part between the curly braces).

HEADER FILES

Header files usually have a .h extension, but you will sometimes see them with a .hpp extension or
no extension at all. The purpose of a header file is to hold declarations for other files to use.

Rule: Use angled brackets to include header files that come with the compiler.
Use double quotes to include any other header files.

Rule: Each .cpp file should explicitly #include all of the header files it needs to
compile.

Rule: use the non .h version of a library if it exists, and access the functionality
through the std namespace. If the non .h version does not exist, or you are
creating your own headers, use the .h version
PREPROCESSOR

When the preprocessor runs, it simply scans through each code file from top to bottom, looking
for directives. Directives are specific instructions that start with a # symbol and end with a newline
(NOT a semicolon).

INCLUDES

When you #include a file, the preprocessor copies the contents of the included file into the
including file at the point of the #include directive.

The #include command has two forms:

#include <filename> tells the preprocessor to look for the file in a special place defined by the
operating system where header files for the C++ runtime library are held. You’ll generally use this
form when you’re including headers that come with the compiler (e.g. that are part of the C++
standard library).

#include "filename" tells the preprocessor to look for the file in the directory containing the
source file doing the #include. If it doesn’t find the header file there, it will check any other include
paths that you’ve specified as part of your compiler/IDE settings. That failing, it will act identically
to the angled brackets case. You’ll generally use this form for including your own header files.

MACRO DEFINES

The #define directive can be used to create a macro. A macro is a rule that defines how an input
sequence (e.g. an identifier) is converted into a replacement output sequence (e.g. some text).
CONDITIONAL COMPILATION

The conditional compilation preprocessor directives allow you to specify under what conditions
something will or won’t compile. The only conditional compilation directives we are going to cover
in this section are #ifdef, #ifndef, and #endif.

ADDRESSING MEMORY

The smallest unit of memory is a binary digit (bit), which can hold a value of 0 or 1.

Memory is organized into sequential units called memory addresses (or addresses for short).

The smallest addressable unit of memory is known as a byte. The modern standard is that a byte is
comprised of 8 sequential bits.

FUNDAMENTAL DATA TYPES

C++ comes with built-in support for certain data types. These are called fundamental data types
(in the C++ specification), but are often informally called basic types, primitive types, or built-in
types.

UNIFORM INITIALIZATION IN C++11

Because C++ grew organically, the copy initialization and direct initialization forms only work for
some types of variables (for example, you can’t use either of these forms to initialize a list of
values).

In an attempt to provide a single initialization mechanism that will work with all data types, C++11
adds a new form of initialization called uniform initialization (also called brace initialization):

Initializing a variable with an empty brace indicates default initialization. Default initialization
initializes the variable to zero (or empty, if that’s more appropriate for a given type).
DATA TYPES

VOID:

Void is the easiest of the data types to explain. Basically, it means “no type”!

Consequentially, variables cannot be defined with a type of void.

Void is typically used in several different contexts:

1. Most commonly, as a way to indicate that a function does not return a value
2. In C, as a way to indicate that a function does not take any parameters.

Rule: Use an empty parameter list instead of void to indicate no function parameters are expected.

VARIABLE SIZES AND THE SIZEOF OPERATOR

2 bits can hold 4 possible values:

bit 0 bit 1

0 0
0 1
1 0
1 1

3 bits can hold 8 possible values:

bit 0 bit 1 bit 2

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

To generalize, a variable with n bits can hold 2n (2 to the power of n, also commonly written 2^n)
possible values. With an 8-bit byte, a byte can store 28 (256) possible values.
THE SIZE OF C++ BASIC DATA TYPES

C++ guarantees that the basic data types will have a minimum size:

Category Type Minimum Size Note

boolean bool 1 byte


May be signed or unsigned
character char 1 byte
Always exactly 1 byte
wchar_t 1 byte
char16_t 2 bytes C++11 type
char32_t 4 bytes C++11 type
integer short 2 bytes
int 2 bytes
long 4 bytes
long long 8 bytes C99/C++11 type
floating point float 4 bytes
double 8 bytes
long double 8 bytes

The sizeof operator is a unary operator that takes either a type or a variable, and returns its size in
bytes.

INTEGERS

An integer type (sometimes called an integral type) variable is a variable that can only hold non-
fractional numbers (e.g. -2, -1, 0, 1, 2). C++ has five different fundamental integer types available
for use:

Category Type Minimum Size Note

character char 1 byte


integer short 2 bytes
int 2 bytes Typically 4 bytes on modern architectures
long 4 bytes
long long 8 bytes C99/C++11 type

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy