Unit-4 Complete Notes
Unit-4 Complete Notes
ORIENTED
PROGRAMMING
BOE-064
(As per the AKTU Syllabus)
UNIT-4
C++
Basics
Mr. Rahul Kumar Gupta Dr. Arun Kumar. G
Assistant Professor Professor & HOD
Department of Electronics & Communication Engg.
JSS Academy of Technical Education , Noida, UP.
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Unit-4: C++ Basics: Overview, Program structure, namespace, identifiers, variables,
constants, enum, operators, typecasting, control structures C++ Functions: Simple functions,
Call and Return by reference, Inline functions, Marco Vs. Inline functions, Overloading of
functions, default arguments, friend functions, virtual functions
PROGRAM STRUCTURE
Include files
Class declaration
Member function definition
Main function program
Figure 1: Structure of a C++ Program
1. Include Files
These are libraries that give ready-made functions.
Example:
#include <iostream>
It allows us to use:
• cout
• cin
• endl
2. Class Declaration
This tells what the class contains.
It only declares:
• Variables
• Function names
It does NOT define what the function does.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 1 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
(Think of it like: Writing a table of contents.)
Example:
class Calculator
{
public:
int add(int a, int b);
};
Example:
int Calculator::add(int a, int b)
{
return a + b;
}
(Think of it like: Writing the actual working steps.)
4. Main Function
This is the starting point of every C++ program.
Example:
int main()
{
Calculator c;
cout << [Link](5, 3);
return 0;
}
Example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Explanation:
• #include <iostream> → Used to take input and show output
• using namespace std; → Allows us to use standard names like cout
• int main() → Main function where program starts
• cout → Used to print output
• return 0; → Ends the program
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 2 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
CLIENT–SERVER MODEL
In C++:
• The Class + Member functions = Server
• The main() function = Client
WHAT IS SERVER?
The server provides services.
Example:
Calculator class can:
• Add
• Subtract
• Multiply
It provides these services.
WHAT IS CLIENT?
The client uses the services.
Here:
main() calls the functions of the class.
So:
main() asks → class provides.
main() says:
[Link](5,3);
Class gives answer: 8
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 3 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
COMPLETE PROGRAM:
#include <iostream>
using namespace std;
// Server
class Calculator
{
public:
int add(int a, int b);
};
// Function definition
int Calculator::add(int a, int b)
{
return a + b;
}
// Client
int main()
{
Calculator c;
cout << "Addition = " << [Link](5, 3);
return 0;
}
Example:
#include <iostream> // 1. Include files
using namespace std;
NAMESPACE
• In C++, a namespace is a named group used to keep variables, functions, and classes
together so that there is no confusion between same names.
• It helps C++ understand which variable or function we are using when two things
have the same name.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 4 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example: Standard Method
#include <iostream>
using namespace std;
namespace First {
int number = 15;
}
namespace Second {
int number = 25;
}
int main() {
cout << First::number << endl;
cout << Second::number;
return 0;
}
// Library 1
int value = 15;
// Library 2
int value = 25; // ❌ Error: redefinition of 'value'
int main() {
cout << value;
return 0;
}
C++ will give an error like:
error: redefinition of 'int value'
Because:
• We declared int value = 10;
• Again we declared int value = 20;
• Both are in the same global area
Note:
Without namespace → Name conflict (Error)
With namespace → No confusion
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 5 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
IDENTIFIERS
In C++, an identifier is the name given to a variable, function, class, array, or any other
user-defined item.
Example:
#include <iostream>
using namespace std;
int main()
{
// Rule 1: Must start with a letter or underscore (_)
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 6 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
// Rule 5: Identifiers are Case Sensitive
return 0;
}
VARIABLES
In C++, a variable is a name used to store data (a value) in memory.
Integer Types
• int
• short
• long
• long long
Character Types
• char
• wchar_t
Boolean Type
• bool
Void Type
• void
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 7 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
BASIC (BUILT-IN / PRIMITIVE) DATA TYPES
Data Memory
Theory Range (Signed) (Approx.) Example
Type (Approx.)
Most commonly used
integer type. Stores −2,147,483,648 to
int 4 bytes int age = 20;
positive and negative 2,147,483,647
whole numbers.
Used when numbers
short marks =
short are small. Takes less 2 bytes −32,768 to 32,767
100;
memory than int.
4 or 8 bytes long
Used to store larger −2,147,483,648 to
long (system population =
numbers than int. 2,147,483,647 (or larger)
dependent) 1000000;
−9,223,372,036,854,775,808 long long
long Used to store very
8 bytes to distance =
long large whole numbers.
9,223,372,036,854,775,807 1234567890;
Example:
#include <iostream>
using namespace std;
int main()
{
short marks = 100; // short type
int age = 20; // int type
long population = 1000000; // long type
long long distance = 1234567890; // long long type
cout << "Short value (marks): " << marks << endl;
cout << "Int value (age): " << age << endl;
cout << "Long value (population): " << population << endl;
cout << "Long long value (distance): " << distance << endl;
return 0;
}
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 8 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
#include <iostream>
using namespace std;
int main()
{
float num1 = 10.5f; // float type
double num2 = 20.12345; // double type
long double num3 = 30.123456789; // long double type
return 0;
}
CHARACTER TYPES
Data Memory What it One-Line
Meaning / Theory
Type (Typical)* Stores Example
Used to store a single
Letters, digits,
character. It stores char grade =
char 1 byte symbols (A, b,
normal English 'A';
5, @)
characters.
Used to store wide Unicode
characters. It supports 2 or 4 bytes characters wchar_t letter
wchar_t special characters and (system
(like ₹, தமிழ் , = L'₹';
other languages dependent)
中文)
(Unicode).
Note:
• char → Stores one normal character (English letters).
• wchar_t → Stores special or international characters (uses more memory).
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 9 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example:
#include <iostream>
using namespace std;
int main()
{
// Normal character
char grade = 'A';
char symbol = '@';
return 0;
}
Output:
Using char type:
Grade: A
Symbol: @
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 10 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
BOOLEAN TYPE:
Data Memory What it
Meaning / Theory Example
Type (Typical)* Stores
Used to store logical values. It true or bool isPassed
bool 1 byte
represents only two possible values. false = true;
Note: *Memory size may vary depending on the system/compiler.
Explanation:
bool means Boolean.
• true (1)
• false (0)
Example:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Output:
Is Passed: 1
Is Fail: 0
Explanation
• bool stores only two values:
• true → printed as 1
• false → printed as 0
• Used for yes/no, true/false, condition checking
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 11 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
void TYPE:
Data What it One-Line
Meaning / Theory Memory
Type Represents Example
Used when a
No
Represents no value or no function does
void memory void display();
type. It means “nothing”. not return
for value
anything
• void means no value.
• It is mostly used:
1. When a function does not return anything
2. With pointers (void*)
3. To show a function takes no parameters
Example:
#include <iostream>
using namespace std;
int main() {
return 0;
}
Output:
Hello Students!
Explanation
• void means no return value.
• The function greet() prints a message.
• It does not return any value, so we write void before the function name.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 12 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example:
#include <iostream>
using namespace std;
int main() {
// 1️⃣ Array
int numbers[3] = {10, 20, 30};
cout << "Array elements: ";
cout << numbers[0] << " " << numbers[1] << " " << numbers[2] << endl;
// 2️⃣ Pointer
int value = 50;
int* ptr = &value; // pointer stores address of value
cout << "Pointer value: " << *ptr << endl;
// 3️⃣ Reference
int marks = 80;
int& ref = marks; // reference to marks
cout << "Reference value: " << ref << endl;
// 4️⃣ Function
display(100); // calling function
return 0;
}
Output:
Array elements: 10 20 30
Pointer value: 50
Reference value: 80
Function received value: 100
Explanation
• void means no return value.
• The function greet() prints a message.
• It does not return any value, so we write void before the function name.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 13 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
CONSTANTS
A constant is a fixed value that cannot be changed during the execution of a program.
Once you assign a value to a constant, you cannot modify it later.
Example Program:
#include <iostream>
using namespace std;
int main()
{
const float PI = 3.14; // Constant variable
float radius = 5;
return 0;
}
OUTPUT:
Radius = 5
Area of Circle = 78.5
Example Program:
#include <iostream>
using namespace std;
#define PI 3.14
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 14 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
int main()
{
float radius = 4;
float area = PI * radius * radius;
return 0;
}
OUTPUT:
Area = 50.24
Example Program:
#include <iostream>
using namespace std;
int main()
{
const int num = 10; // Integer constant
const float pi = 3.14; // Float constant
const char grade = 'A'; // Character constant
const string name = "Arun"; // String constant
const bool status = true; // Boolean constant
return 0;
}
OUTPUT:
Integer: 10
Float: 3.14
Character: A
String: Arun
Boolean: 1
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 15 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
ENUM
Enum (Enumeration) is a user-defined data type used to give names to a group of related
integer constants. It makes the program more readable and meaningful.
Instead of writing numbers like 1, 2, 3, we can write names like Monday, Tuesday, Wednesday.
Syntax of Enum:
enum EnumName
{
value1,
value2,
value3
};
Example:
enum Color
{
Red,
Green,
Blue
};
By default:
• Red = 0
• Green = 1
• Blue = 2
int main()
{
enum Color { Red, Green, Blue };
Color c = Green;
return 0;
}
Output:
Value of Green is: 1
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 16 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example: Enum with Custom Values
We can also assign values manually:
enum Day
{
Monday = 1,
Tuesday,
Wednesday
};
Here:
• Monday = 1
• Tuesday = 2
• Wednesday = 3
Example Program
#include <iostream>
using namespace std;
int main()
{
Day today = Wednesday;
cout << "Today number is: " << today << endl;
return 0;
}
Output:
Today number is: 3
Note:
• Enum values are integers
• Default value starts from 0
• Makes code easy to understand
• Used when we have fixed set of related values
OPERATORS
An operator is a symbol that tells the computer to perform some operation on values.
Example:
• + is used for addition
• - is used for subtraction
Example:
5 + 3 // + is an operator
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 17 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical calculations like addition, subtraction,
multiplication, etc.
Note: Since a and b are integers, a / b gives 3 (not 3.33) because it performs integer division.
int main()
{
int a = 10, b = 3;
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 18 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
RELATIONAL OPERATORS
Relational operators are used to compare two values. They always give the result as
True (1) or False (0).
Operator Meaning Example (a = 10, b = 3) Result
== Equal to a == b 0 (False)
!= Not equal to a != b 1 (True)
> Greater than a>b 1 (True)
< Less than a<b 0 (False)
>= Greater than or equal to a >= b 1 (True)
<= Less than or equal to a <= b 0 (False)
Assume:
int a = 10;
int b = 3;
Remember:
• True = 1
• False = 0
int main() {
int a = 10;
int b = 5;
return 0;
}
Output:
a == b : 0
a != b : 1
a>b :1
a<b :0
a >= b : 1
a <= b : 0
Important:
• Relational operators are mainly used in if conditions, loops, and decision-
making statements.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 19 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
LOGICAL OPERATORS
Logical operators are used to combine conditions. They are mainly used inside if statements.
Example:
int a = 10, b = 5;
int main()
{
int a = 10, b = 5;
cout << "a > 5 && b < 10 : " << (a > 5 && b < 10) << endl;
cout << "a > 5 || b > 10 : " << (a > 5 || b > 10) << endl;
cout << "!(a > 5) : " << !(a > 5) << endl;
return 0;
}
Output:
a > 5 && b < 10 : 1
a > 5 || b > 10 : 1
!(a > 5) : 0
Important Note:
• True = 1
• False = 0
ASSIGNMENT OPERATOR:
The assignment operator (=) is used to store a value inside a variable. It assigns (gives) a
value to a variable.
Example:
int x = 5;
x = 20; // Now x becomes 20
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 20 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program: Assignment Operator
#include <iostream>
using namespace std;
int main()
{
int num; // variable declaration
num = 25; // assignment
return 0;
}
Output:
Value of num = 25
Important Points:
• = means assign, not compare.
• == is used for comparison.
• Always declare the variable before assigning a value.
int main() {
int a = 10;
a += 5; // a = 10 + 5 = 15
cout << "After += : " << a << endl;
a -= 3; // a = 15 - 3 = 12
cout << "After -= : " << a << endl;
a *= 2; // a = 12 * 2 = 24
cout << "After *= : " << a << endl;
a /= 4; // a = 24 / 4 = 6
cout << "After /= : " << a << endl;
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 21 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
return 0;
}
Output:
After += : 15
After -= : 12
After *= : 24
After /= : 6
INCREMENT / DECREMENT
These operators are used to increase or decrease a variable value by 1.
Operator Meaning Example Result (a = 5)
++ Increase by 1 a++; a=6
-- Decrease by 1 a--; a=4
int main()
{
int a = 5;
a++; // Increment
cout << "After increment: " << a << endl;
a--; // Decrement
cout << "After decrement: " << a << endl;
return 0;
}
Output:
Initial value: 5
After increment: 6
After decrement: 5
Important Note
Expression Meaning
a++ Use first, then increase
++a Increase first, then use
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 22 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
TYPECASTING
Typecasting in C++ means converting one data type into another data type.
For example:
• Converting int → float
• Converting float → int
• Converting double → int
Example:
int a = 10;
float b = a; // int converted to float automatically
Here:
• a is int
• b becomes 10.0
int main()
{
int a = 10;
float b = a; // int -> float
return 0;
}
Output:
Value of a (int): 10
Value of b (float): 10.0
Here a becomes 10.0 when assigned to b.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 23 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program: Implicit Typecasting
#include <iostream>
using namespace std;
int main()
{
float x = 9.8;
int y = int(x); // C++ style casting
return 0;
}
Output:
Value of x (float): 9.8
Value of y (int): 9
The decimal part (.8) is removed.
This shows data loss, which is why explicit typecasting should be used carefully.
A simple function:
• Does not return a value (void type)
• May or may not take parameters
• Is called inside main() function
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 24 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example: Function to display a message
#include <iostream>
using namespace std;
void greet()
{
cout << "Hello Students!" << endl;
}
int main()
{
greet(); // function call
return 0;
}
Output:
Hello Students!
void display()
{
cout << "Number is 10";
}
int main()
{
display(); // calling the function
return 0;
}
Output:
Number is 10
Note:
• Function is a self-contained block of code
• void means no return value
• Function must be called inside main()
• Helps in making program clean and modular
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 25 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Syntax
return_type function_name(data_type &variable)
{
// function body
}
Example:
void change(int &x)
{
x = x + 10;
}
Example
• If we increase a number inside a function using call by reference, the original number will
also change.
int main()
{
int value = 10;
cout << "Before function call: " << value << endl;
cout << "After function call: " << value << endl;
return 0;
}
Output:
Before function call: 10
After function call: 15
RETURN BY REFERENCE:
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 26 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Syntax
data_type& function_name(parameters)
{
return variable;
}
int main()
{
int num = 20;
return 0;
}
Output:
Value of num: 50
SUMMARY:
Concept Meaning
Call by Reference Function modifies original variable
Return by Reference Function returns reference to original variable
Symbol Used &
INLINE FUNCTIONS
An inline function is a function where the compiler replaces the function call with the actual
function code.
It is used to:
• Reduce function call overhead
• Increase execution speed
• Use for small and simple functions
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 27 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Syntax
inline return_type function_name(parameters)
{
// function body
}
Example
• If we create a small function to add two numbers, using inline avoids the overhead of a
normal function call.
int main()
{
int result = add(5, 3);
return 0;
}
Output:
Sum is: 8
SUMMARY
• inline keyword is used.
• Best for small functions.
• Improves performance by reducing function call overhead.
• The compiler may ignore inline if the function is large.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 28 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
MARCO VS. INLINE FUNCTIONS
Macro
• Defined using #define.
• Processed by the preprocessor before compilation.
• No type checking.
• Simple text substitution.
Syntax: Macro
#define MACRO_NAME(parameters) expression
Example:
#define SQUARE(x) x*x
int main()
{
int num = 4;
int result = SQUARE(num);
return 0;
}
Output:
Square using Macro: 16
Inline Function
• Defined using inline keyword.
• Processed by the compiler.
• Performs proper type checking.
• Safer than macros.
Example:
inline int square(int x)
{
return x*x;
}
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 29 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program: Inline Function
#include <iostream>
using namespace std;
int main()
{
int num = 4;
int result = square(num);
return 0;
}
Output:
Square using Inline Function: 16
// Macro definition
#define SQUARE(x) x*x
int main()
{
int a = 5;
return 0;
}
Output:
Macro result: 25
Inline function result: 25
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 30 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
DIFFERENCE BETWEEN MACRO AND INLINE FUNCTION
Macro Inline Function
Defined using #define Defined using inline keyword
Processed by preprocessor Processed by compiler
No type checking Type checking is done
Performs text substitution Works like a normal function
Less safe Safer than macro
Difficult to debug Easier to debug
Overloading of functions
Function Overloading means:
• Defining multiple functions with the same name
• But with different parameters (different number or type of arguments)
It helps in:
• Improving readability
• Reusing the same function name for related tasks
The compiler decides which function to call based on the arguments passed.
Syntax:
return_type function_name(parameter_list1)
{
// code
}
return_type function_name(parameter_list2)
{
// code
}
The parameter list must be different.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 31 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program to add Function with two int parameters & Function with two float
parameters
#include <iostream>
using namespace std;
int main()
{
cout << "Sum of integers: " << add(5, 3) << endl;
cout << "Sum of floats: " << add(2.5f, 1.5f) << endl;
return 0;
} int result = square(num);
return 0;
}
Output:
Sum of integers: 8
Sum of floats: 4
Summary
• Same function name
• Different parameters
• Improves code readability
• Example: add(int, int) and add(float, float)
In C++:
• 2.5 → is treated as double by default
• 2.5f → is treated as float
The letter f tells the compiler that the number is a float literal, not a double.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 32 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example program for Function Overloading in C++
#include <iostream>
using namespace std;
int main()
{
cout << "Single value: " << display(5) << endl;
cout << "Sum of two values: " << display(5, 3) << endl;
return 0;
}
Output:
Single value: 5
Sum of two values: 8
Summary:
• Same function name display()
• Different number of parameters
• This is Function Overloading
default arguments
Default arguments are values assigned to function parameters.
• If the user does not pass a value while calling the function,
• The default value is automatically used.
• Default values are specified in the function declaration.
Syntax:
return_type function_name(data_type parameter = default_value)
{
// function body
}
Example:
int add(int a, int b = 5);
Here, if b is not provided, it will take value 5.
If we call: add(10);
It becomes: add(10, 5);
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 33 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program
#include <iostream>
using namespace std;
int main()
{
cout << "With one argument: " << add(5) << endl;
cout << "With two arguments: " << add(5, 3) << endl;
return 0;
}
Output:
With one argument: 15
With two arguments: 8
In this program:
Default value is used only when the second argument is not given.
friend functions
A friend function is a function that:
• Is not a member of a class
• But can access private and protected members of the class
It is declared inside the class using the keyword friend.
Used when we want an external function to access private data of a class.
A friend function accessing private data of a class.
Syntax:
class ClassName
{
private:
int data;
public:
friend void functionName(ClassName obj);
};
Explanation:
• friend keyword is used inside the class.
• The function is not a member of the class.
• But it can access private data of the class.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 34 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program
#include <iostream>
using namespace std;
class Sample
{
private:
int num;
public:
Sample(int n)
{
num = n;
}
int main()
{
Sample s(10);
show(s);
return 0;
}
Output:
Value of num: 10
In this program:
• num is a private variable.
• Normally, private data cannot be accessed outside the class.
• But show() is declared as a friend function.
• So, it can access [Link] directly.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 35 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Example Program
#include <iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int a)
{
x = a;
}
void display(Test t)
{
cout << "Value of x: " << t.x;
}
int main()
{
Test obj(5);
display(obj);
return 0;
}
Output:
Value of x: 5
In this program:
• x is a private variable.
• Normally, private data cannot be accessed outside the class.
• display() is declared as a friend function.
• So, it can access t.x directly.
• Therefore, it prints the value 5.
Summary
• Friend function is declared using friend keyword.
• It is not a class member.
• It can access private data of the class.
• Used when outside function needs access to class data.
virtual functions
A virtual function is a member function of a class that is declared using the keyword
virtual.
It is used to achieve runtime polymorphism.
✓ The function that is called depends on the object, not the pointer.
It is mainly used in inheritance.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 36 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Syntax:
return_type function_name(data_type parameter = default_value)
class Base
{
public:
virtual return_type function_name()
{
// code
}
};
Example Program
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
cout << "This is Base class" << endl;
}
};
int main()
{
Base* ptr;
Derived obj;
ptr = &obj;
ptr->show(); // Calls Derived class function
return 0;
}
Output:
This is Derived class
In this program:
• show() is declared as virtual in Base class.
• Derived class overrides show().
• Pointer ptr is of Base class.
• But it points to Derived object.
• So, Derived class function runs.
• This is called Runtime Polymorphism.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 37 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Summary
• Virtual function uses virtual keyword.
• Achieves runtime polymorphism.
• Function call depends on object, not pointer type.
Prepared by Dr. Arun Kumar G (Professor & HOD) and Mr. Rahul Kumar Gupta
(Assistant Professor), Dept. of ECE, JSS Academy of Technical Education, Noida.
Page 38 of 38