Syllabus (Unit-4)
Programming in C++
C++ Programming Language (Basics)
C++ was developed as an extension of C, and both languages
have almost the same syntax.
C++ is a general-purpose, object-oriented programming
language, developed by Bjarne Stroustrup
Cross-platform language
C++ gives programmers a high level of control over system
resources and memory.
C++ is an object-oriented programming language which gives
a clear structure to programs and allows code to be reused,
lowering development costs.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Program structure
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Namespace std in C++
std stands for Standard Namespace, used in C++ to
encapsulate all standard library features like functions,
objects, and classes.
Without std, you must prefix standard elements (e.g., cout,
cin, string) with std::.
Why Use Namespace?
Prevents naming conflicts in large programs or libraries.
Example: Multiple libraries might define a function named
print. With namespaces, std::print and myLib::print can
coexist.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Using Namespace
Direct Prefix (Recommended for large programs):
Explicitly specify std:: before using standard library objects.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Using Namespace
Global Declaration (Use Carefully):
Declare using namespace std; to avoid repeatedly typing std::.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Using Namespace
Selective Declaration (Best Practice):
Import specific identifiers from the namespace.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Many Namespaces
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Variables
Variables in C++ are named storage locations in memory that hold
data of specific types during program execution.
Syntax
Data_Type variable_name;
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Data Types
1. Integer Types: Used for whole numbers.
Types:
int: Standard integer (e.g., int x = 10;)
short: Smaller integer.
long: Larger integer.
long long: Even larger integer.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Data Types
2. Floating-Point Types:
Used for real numbers with decimal points.
Types:
float: Single-precision floating-point.
double: Double-precision floating-point.
long double: Extended precision.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Data Types
3. Character Type: Used for storing single characters.
Type:
char: Stores a single ASCII character or small integer.
Example: char c = 'A';
Size: 1 byte
Range: -128 to 127 or 0 to 255 (depends on signed/unsigned).
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Data Types
4. Boolean Type:
Used for true/false values.
Type: bool
Example: bool isTrue = true;
Size: 1 byte
Values: true or false.
5. Void Type:
Represents the absence of a type.
Used for functions that do not return a value.
Example: void display();
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Data Types (Exapmle)
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Identifiers
All C++ variables must be identified with unique
names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more
descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in
order to create understandable and maintainable code.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Identifiers
The general rules for naming variables are:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case-sensitive (myVar and myvar are different
variables)
Names cannot contain whitespaces or special characters
like !, #, %, etc.
Reserved words (like C++ keywords, such as int) cannot
be used as names
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Constants
The const keyword will declare the variable as
"constant", which means unchangeable and read-only):
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Enumeration (enum)
An enum is a special type that represents a group of
constants (unchangeable values).
To create an enum, use the enum keyword, followed by
the name of the enum, and separate the enum items with
a comma.
Example:
enum Level {LOW, MEDIUM, HIGH};
By default, the first item (LOW) has the value 0, the
second (MEDIUM) has the value 1, and so on.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
C++ Enumeration (enum)
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
enum with Custom Values
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Advantages of Enumerators
Improves readability and maintainability
of the code.
Prevents the use of arbitrary integers,
making logic more intuitive.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Operators in C++
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Ternary or Conditional Operators
Scope resolution operator ::
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Function Overloading
With function overloading, multiple functions
can have the same name with different
parameters:
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Function Overloading
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Type Casting in C++
Type casting in C++ is the process of converting a
variable from one data type to another.
Types of Type Casting:
Implicit Type Casting (Type Promotion):
Automatically performed by the compiler when
converting a smaller type to a larger type.
Example:
int a = 5;
float b = a; // Implicitly converts int to float
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Type Casting in C++
Type casting in C++ is the process of converting a
variable from one data type to another.
Types of Type Casting:
Explicit Type Casting (Type Conversion):
Requires manual intervention using casting syntax.
Example:
float a = 5.67;
int b = (int)a; // Explicitly converts float to int
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Control Structures in C++
Broadly categorized into
selection,
iteration, and
jump statements.
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Selection Statements (Decision-making)
if statement: Executes code if a condition is true.
if (condition)
{ // Code to execute if condition is true }
if-else statement: Executes one block of code if condition is true,
another if false.
if (condition)
{ // Code if true }
else
{ // Code if false }
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Selection Statements (Decision-making)
else if ladder: Multiple conditions evaluated sequentially.
if (condition1) { // Code for condition1 }
else if (condition2) { // Code for condition2 }
else { // Default code }
switch statement: Selects a block of code to execute based on the value
of an expression.
switch (variable)
{ case value1: // Code for value1 break;
case value2: // Code for value2 break;
default: // Default code }
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Iteration Statements (Loops)
for loop: Iterates a block of code a specific number of times.
for (int i = 0; i < n; i++) { // Code to execute }
while loop: Repeats code while a condition is true.
while (condition) { // Code to execute }
do-while loop: Executes code at least once before checking the
condition.
do { // Code to execute } while (condition);
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Jump Statements
break: Exits the nearest loop or switch.
for (int i = 0; i < 10; i++)
{ if (i == 5) break; // Exits loop when i == 5 }
continue: Skips the rest of the loop iteration and moves
to the next.
for (int i = 0; i < 10; i++)
{ if (i == 5) continue; // Skips iteration when i == 5 }
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)
Jump Statements
return: Exits a function and optionally returns a value.
int sum(int a, int b)
{ return a + b; // Returns sum }
goto: Transfers control to a labeled statement
(discouraged due to poor readability).
int x = 1;
label: x++;
if (x < 5) goto label;
OOSD (BCS-054)
(Mr Deepak Vishwakarma, Assistant Professor, IT, KIET)