Unit - 2
Unit - 2
Functions: Function Overloading, Inline Functions, Default Argument, Pass by Reference, Return by
Reference, Scope and Storage Class.
Pointers: Pointer variables declaration & initialization, Operators in pointers, Pointers and Arrays, Pointer and
Function.
• Character Set: Includes letters, digits, special symbols (+ - * /, etc.), and whitespace.
• Tokens: Smallest elements of a program.
o Keywords: int, return, if, etc.
o Identifiers: User-defined names (main, cout)
o Constants: 5, 3.14, 'A'
o Operators: +, -, =, ==
o Punctuators: ;, {}, ()
✅ 3. Data Types
Built-in Types:
cpp
int a = 10;
float b = 5.5;
char c = 'A';
bool d = true;
Derived/User-defined:
✅ 4. Type Conversion
• Implicit (Automatic):
cpp
int a = 10;
float b = a; // a is converted to float automatically
cpp
float a = 5.5;
int b = (int) a; // a is explicitly cast to int
✅ 5. Preprocessor Directives
cpp
#include <iostream>
#define PI 3.14159
✅ 6. Namespace
cpp
namespace MySpace {
int val = 10;
}
int main() {
cout << MySpace::val;
}
#include <iostream>
#include <iomanip> // for manipulators
using namespace std;
In C++, dynamic memory allocation allows you to allocate memory at runtime using the
new and delete operators. This is useful when the size of memory needed is not known at
compile time or when you want to manage memory explicitly.
cpp
✅ 9. Control Statements
Conditional
cpp
if (a > b) { ... }
else if (a == b) { ... }
else { ... }
Loops
cpp
Jump Statements
cpp
✅ 1. Function Overloading
cpp
void show(int a) {
cout << "Integer: " << a << endl;
}
void show(double b) {
cout << "Double: " << b << endl;
}
✅ 2. Inline Functions
• Suggests the compiler to insert the function code directly where it's called.
cpp
✅ 3. Default Arguments
cpp
✅ 4. Pass by Reference
#include <iostream>
using namespace std;
x += 5;
}
int main() {
int a = 10;
update(a);
cout << a; // Output will be 15
return 0;
}
• int &x means x is a reference to an int variable passed into the function.
✅ 5. Return by Reference
cpp
Avoid returning reference to local variables—they are destroyed after function ends.
Explanation:
• int &x: The parameter x is passed by reference, so the function receives a reference to the
original variable.
• int& getRef(...): The function returns a reference to the original variable, not a copy.
Returning a reference allows the caller to access or modify the original variable.
#include <iostream>
using namespace std;
int main() {
int a = 10;
getRef(a) = 20; // Modifies 'a' directly through the reference
cout << a; // Output will be 20
return 0;
In this example, getRef(a) = 20; sets a to 20 because the function returns a reference to a.
Scope Types:
Storage Classes:
cpp
int a = 10;
int* p; // Declaration
p = &a; // Initialization with address of 'a'
✅ 2. Operators in Pointers
cpp
cpp
Arrays and pointers are closely related in C++, though not the same.
a) Pass by Pointer
cpp
void update(int* p) {
*p = *p + 1;
}
int main() {
int a = 5;
update(&a); // Pass address
cout << a; // 6
}
b) Pointer to Function
cpp
void greet() {
cout << "Hello!" << endl;
}
int main() {
Er. Sital Prasad Mandal 7|Page
B.Sc. CSIT 2nd C++
void (*funcPtr)() = greet;
funcPtr(); // Calls greet
}
• Declares a function pointer funcPtr that can point to functions returning void and taking no
parameters.
• Initializes funcPtr with the address of greet.
• Invokes the function through the pointer, so it calls greet().
#include <iostream>
using namespace std;
void greet(int a) {
cout << "Hello!" << a << endl;
}
int main() {
void (*funcPtr)(int) = greet; // Correct pointer type
funcPtr(5); // Output: Hello!5
return 0;
}
---------------------------------------------------------------------------------------------------------------------
Review
• Character Set: Includes letters (A-Z, a-z), digits (0-9), special characters (e.g., @, #), and
whitespace.
• Tokens: Basic units of a program, including:
o Keywords: Reserved words (e.g., int, if).
o Identifiers: User-defined names (e.g., variable names).
o Literals: Constants (e.g., 42, "hello").
o Operators: Symbols (e.g., +, =).
o Punctuators: Braces, parentheses, semicolons, etc.
3. Data Types
C++ supports:
4. Type Conversion
5. Preprocessor Directives
6. Namespace
9. Control Statements