0% found this document useful (0 votes)
7 views19 pages

Fundamentals

The document provides an overview of C++ programming concepts, including preprocessor directives, input/output operators, namespaces, scope resolution, keywords, identifiers, constants, strings, operators, memory management, and data types. It explains the purpose of various operators and their precedence, as well as the differences between primitive, derived, and user-defined data types. Additionally, it covers arrays and references, highlighting their syntax and functionality in C++.

Uploaded by

Aryaman Singh
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 views19 pages

Fundamentals

The document provides an overview of C++ programming concepts, including preprocessor directives, input/output operators, namespaces, scope resolution, keywords, identifiers, constants, strings, operators, memory management, and data types. It explains the purpose of various operators and their precedence, as well as the differences between primitive, derived, and user-defined data types. Additionally, it covers arrays and references, highlighting their syntax and functionality in C++.

Uploaded by

Aryaman Singh
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/ 19

Pre-processor

• These are the directives, which give instructions to the compiler to preprocess the information before
actual compilation starts.
• Lines included in the code of programs preceded by a hash sign (#). They are not program statements but
directives for the preprocessor.

Directive Purpose
#include Links a header file in the source code
#define Creates a symbolic or macro constant Check the content
#undef Deletes an existing macro of *.i file

#if / #elif / #else / #endif Instruct the preprocessor whether to


include a block of code or not depending on
certain conditions.
#error Halts compilation process and produce
error notice
Header File
• Contains definitions of functions that you can include or import using a
preprocessor directive #include.
• The preprocessor directive tells the compiler that the header file needs to be
processed prior to the compilation.

Comments
• Used to improve readablity of the program
• // (double slash) single line comments
• /*, */ - Multiline comments
I/P operator
• The statement ”cin >> number;” is an input statement. The operator << is known as the get-from operator. It
extracts value from the keyboard and assigns it to the variable on its right.

• The program waits for the user to type in a number. The number keyed in is placed in the variable number.

• The identifier cin is a predefined object in C++ that corresponds to the standard input stream. Here this stream
represents the keyboard.
cin >> number1 >> number2;

O/P operator
• The statement ”cout << number;” is an input statement. The operator << is known as the insertion operator. It
prints the output on its right on the screen.

• The identifier cout is a predefined object in C++ that corresponds to the standard output stream. Here this stream
represents the screen.
cout << number1 << number2;
Namespace
• Namespace is a new concept introduced by the ANSI C++ standards committee.

• The C++ Standard Library is vast, encompassing a wide range of classes, functions, and objects.

• These components are encapsulated within the std namespace to avoid naming conflicts with user-
defined code and other libraries.

• When you write using namespace std;, you are essentially bringing all these names into the global
scope, which can lead to several issues.

• scope resolution operator operator gives you more freedom in naming your variables by letting you
distinguish between variables with the same name.
Scope resolution operator

Blocks and scopes can be used in constructing programs. We


know same variables can be declared in different blocks
because the variables declared in blocks are local to that
function.

In C, the global version of a variable can’t be accessed from within the


inner block.

C++ resolves this problem by introducing a new operator (::) called the
scope resolution operator.

-> Accessing Global Variables


Scope resolution operator

Blocks and scopes can be used in constructing programs. We


know same variables can be declared in different blocks
because the variables declared in blocks are local to that
function.

In C, the global version of a variable can’t be accessed from within the


inner block.

C++ resolves this problem by introducing a new operator (::) called the
scope resolution operator.

-> Accessing Global Variables


-> Namespace resolution
-> Define Class Member Function Outside Class
-> Access Class’s Static Members
Keywords
• The keywords implement specific C++ language features.

• They are explicitly reserved and can’t be used as names for user-defined program
elements (such as variables and function names).

• Only lowercase and alphabetical characters (int, void, public, private, namespace)

Identifiers
• Identifiers are names given to variables, functions, arrays, and other user-defined items.

• An identifier can consist of alphabetical characters, digits, and underscores. It can be both
uppercase and lowercase.

• No special symbols or punctuations in keywords and identifiers. The special character ‘_’ can be
used in identifiers.
Constants
• Constants in C++ refer to variables with fixed values that cannot be changed. They remain
constant throughout the execution of the program.
• Constants can be integers, floating-point numbers, characters, or strings.

const int c = 24;

Strings
• Strings are nothing but an array of characters ended with a null character (‘\0’).

• This null character indicates the end of the string.

• Strings are always enclosed in double quotes. Whereas a character is enclosed in single quotes in
C and C++.
Operators
• These are symbols that trigger an action when applied to variables and other objects.
• The data on which they operate are called operands.
• C++ has a rich set of operators. All C operators are valid in C++ also. Ex:- Arithmetic operators,
relation operators, logical operators, bitwise operators, Assignmet operators.
• In addition, C++ introduces some new operators.

Additional operators Function


<< Insertion operator
>> extraction operator
:: scope resolution operator
::* Pointer to member
Delete Memory release operator
New memory allocation operator
Setw field width operator
endl line feed operator
Operator Precedence and Associativity in C++
• In C++, operator precedence gives the priority of operators,
• Associativity specifies the order of evaluation when multiple operators of the same
precedence level are present in an expression.
• An expression is a combination of operators, operands, and variables that produces a value.

Operator Name Associativity


() [] -> . Function call, subscript, member access Left-to-right
++ /-- Incement/Decrement Right-to-left
!~-+ Logical/Bitwise NOT, Unary plus/minus Right-to-left

*/% Multiplication, Division, Modulus Left-to-right


+- Addition, Substraction Left-to-right
<< >> Bitwise shift Left-to-right
< <= > >= Relational Operators Left-to-right
Operator Precedence and Associativity in C++

Operator Name Associativity


== != Equality Operators Left-to-right
& Bitwise AND Left-to-right
^ Bitwise XOR Left-to-right
| Bitwise OR Left-to-right
&& Logical AND Left-to-right

|| Logical OR Left-to-right
?: Ternary conditional Right-to-left
= += -= *= /= %= &= ^= |= <<= >>= Assignment and Right-to-left
compound assignment
comma Left-to-right
Memory Management operator
• C uses malloc and calloc functions to allocate memory dynamically at run time.

• Similarly, it uses the function Free( ) to free dynamically allocated memory. We use dynamic
allocation techniques when it is not known in advance how much memory space is needed.

• C++ also supports those functions it also defines two unary operators new and delete that
perform the task of allocating and freeing the memory in a better and easier way.

• new can be used to create a memory space for any data type including user-defined such as
arrays, structures, and classes.

Syntax: pointerVariable = new datatype;


Syntax: pointerVariable = new datatype[size]; // For an array

Example: p = new int; q = new int;


Example: *p = 25; *q = 7.5;
If a data object is no longer needed, it is destroyed to release the memory
space for reuse.

Syntax: delete pointerVariable;


Example: delete p; delete q;

To free a dynamically allocated array, we must use the following form of delete.

delete[size] pointerVariable;
delete[] pointerVariable;
Data types in C++

• Primitive - These are the built in data types to be used directly by user
Integer, Character, Boolean, Float, Double, Void, wide character

• Derived - These are derived from the primitive data types


Function, Array, Pointer, Reference

• User-defined or Abstract
Class, Structure, Union, Enumeration, Typedef
Primitive data types
Both C and C++ compilers support all the built-in types.

Datatype Meaning Size (in Bytes)

int Integer 2 or 4

float Floating point 4 Except void, all the basic datatypes may
have several modifiers preceding them
double Double floating point 8
The modifiers are signed, unsigned,
char Character 1 long, and short and may apply to
character and integer basic data types.
wchar_t Wide Character 2

bool Boolean 1 However, the modifier long may also be


applied to double.
void Empty 0
Datatypes after adding modifiers
Data type Size (in bytes) Range Format specifier
short int 2 -32,768 to 32767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 %f
double 8 %lf
long double 16 %LF
Derived Datatypes

• Arrays
• Reference
• Functions
• Pointers
Arrays
• In C/C++, an array is a data structure that is used to store multiple values of similar data types in a
contiguous memory location.

DataType ArrayName[Size];

int arr[5]; // Declaring an array

int arr[5] = {1, 2, 3, 4, 5}; // initialize with size

int arr[] = {1, 2, 3, 4, 5}; // initialize without size

int arr[5] = {1, 2}; // Partial initialization

int zero_array[5] = {0}; // initialize all values with 0

arr[5] = 100; // Update array element

cout << arr[6]; // Access array element


Reference
• C++ interfaces a new kind of derived datatype known as reference. A reference acts as an alias
(alternative name) for an existing variable.

• For example, if we make the variable sum a reference to the variable total, then sum and total
can be used interchangeably to represent the variable.

int total = 1500;


int& sum = total;

• Reference variables points to the same memory location as the original variable. Thus, changing
one variable results in the value of the other variable also getting changed.

• Reference cannot be null or reassigned to another variable after initialization.

You might also like