0% found this document useful (0 votes)
8 views39 pages

Unit-4 Complete Notes

This document provides an overview of C++ basics, including its program structure, functions, and key concepts such as namespaces, identifiers, and variables. It emphasizes the importance of C++ in software development and outlines the main components of a C++ program, including include files, class declarations, and the main function. Additionally, it covers data types and examples of their usage in C++ programming.

Uploaded by

mh2dmustafajss
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)
8 views39 pages

Unit-4 Complete Notes

This document provides an overview of C++ basics, including its program structure, functions, and key concepts such as namespaces, identifiers, and variables. It emphasizes the importance of C++ in software development and outlines the main components of a C++ program, including include files, class declarations, and the main function. Additionally, it covers data types and examples of their usage in C++ programming.

Uploaded by

mh2dmustafajss
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

OBJECT

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

C++ BASICS: OVERVIEW


C++ is a powerful language that helps you write clear, fast, and structured programs.
C++ is a programming language used to create software like:
• Computer programs
• Games
• Mobile apps
• Operating systems
• Embedded systems
It was developed by Bjarne Stroustrup in 1979.

WHY LEARN C++?


• It is fast and powerful
• Used in real-world applications
• Good for learning programming concepts
• Used in companies for system software and game development

PROGRAM STRUCTURE

Include files
Class declaration
Member function definition
Main function program
Figure 1: Structure of a C++ Program

A C++ program has 4 main parts


1. Include Files
2. Class Declaration
3. Member Function Definition
4. Member Function Definition

1. Include Files
These are libraries that give ready-made functions.
Example:
#include <iostream>
It allows us to use:
• cout
• cin
• endl

(Think of it like: Borrowing tools before starting work.)

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);
};

3. Member Function Definition


Here we define what the function actually does.

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.

Execution always starts from:


int main()

(Think of it like: The main door of a house.)

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

Figure 2: 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;

class Student { // 2. Class declaration


public:
void display(); // only the name here
};

void Student::display() { // 3. Function definition


cout << "Welcome Student!\n";
}

int main() { // 4. Main function


Student s; // create object
[Link](); // call member function
return 0;
}

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;
}

• First and Second are namespaces.


• Both have number.
• No confusion because they are in different groups.

Example: Without Namespace (This Will Give Error)


#include <iostream>
using namespace std;

// 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

C++ gets confused: Which value should it use?

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.

Identifier = Name given to anything in a C++ program.

Rule 1: Must start with a letter or underscore (_)


Rule 2: Can contain letters, digits, and underscore
Rule 3: No special symbols allowed
Rule 4: Cannot use C++ keywords
Rule 5: Case Sensitive

Example:
#include <iostream>
using namespace std;

int main()
{
// Rule 1: Must start with a letter or underscore (_)

int age = 20; // ✔ Correct (starts with letter)


int _total = 50; // ✔ Correct (starts with underscore)

// int 1number = 10; // ❌ Wrong (cannot start with number)

// Rule 2: Can contain letters, digits, and underscore only

int student1 = 5; // ✔ Correct (contains digit)


int total_marks = 90; // ✔ Correct (contains underscore)

// int total marks = 80; // ❌ Wrong (space not allowed)

// Rule 3: No special symbols allowed

int marks100 = 100; // ✔ Correct

// int total@marks = 70; // ❌ Wrong (@ not allowed)


// int total-marks = 60; // ❌ Wrong (- not allowed)
// int total#marks = 50; // ❌ Wrong (# not allowed)

// Rule 4: Cannot use C++ keywords

// int int = 5; // ❌ Wrong (int is a keyword)


// int return = 10; // ❌ Wrong (return is a keyword)
// int class = 1; // ❌ Wrong (class is a keyword)

int number = 25; // ✔ Correct (not a keyword)

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

int value = 10; // ✔ Correct


int Value = 20; // ✔ Different variable (capital V)

return 0;
}

VARIABLES
In C++, a variable is a name used to store data (a value) in memory.

COMMON DATA TYPES IN C++


C++ data types are divided into three main categories:

1. Basic (Built-in / Primitive) Data Types

Integer Types
• int
• short
• long
• long long

Floating Point Types


• float
• double
• long double

Character Types
• char
• wchar_t

Boolean Type
• bool

Void Type
• void

2. Derived Data Types


• Array
• Pointer
• Reference
• Function

3. User-Defined Data Types


• struct
• class
• union
• enum
• typedef

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;
}

FLOATING POINT TYPES


Data Memory Range
Meaning / Theory Example
Type (Typical)* (Typical)*
Used to store decimal
numbers (numbers with Approx. ±3.4 ×
float 4 bytes float temp = 36.5f;
fraction). Less precision 10³⁸
compared to double.
Used to store decimal
Approx. ±1.7 × double salary =
double numbers with higher 8 bytes
10³⁰⁸ 12345.6789;
precision than float.
Used to store very large or
10, 12, or 16 Approx. ±1.1 ×
long very precise decimal long double value =
bytes (system 10⁴⁹³² (varies
double numbers. More precision 123456.789123;
dependent) by system)
than 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 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

cout << "Float value: " << num1 << endl;


cout << "Double value: " << num2 << endl;
cout << "Long Double value: " << num3 << endl;

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).

Memory size may vary depending on the system/compiler.

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 = '@';

// Wide character (Unicode)


wchar_t currency = L'₹';
wchar_t letter = L'Ω';

// Printing normal characters


cout << "Using char type:" << endl;
cout << "Grade: " << grade << endl;
cout << "Symbol: " << symbol << endl;

cout << endl;

// Printing wide characters


wcout << L"Using wchar_t type:" << endl;
wcout << L"Currency: " << currency << endl;
wcout << L"Letter: " << letter << endl;

return 0;
}

Output:
Using char type:
Grade: A
Symbol: @

Using wchar_t type:


Currency: ₹
Letter: Ω
Explanation:
• char → Stores normal single characters (A, B, @, 5)
• wchar_t → Stores special/international characters (₹, Ω, தமிழ் )
• char uses cout
• wchar_t wcout
• Wide characters use L before the character → L'₹'

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.

It can store only two values:

• true (1)
• false (0)

Mostly used in conditions and decision making.

Example:
#include <iostream>
using namespace std;

int main()
{

bool isPassed = true; // true value


bool isFail = false; // false value

cout << "Is Passed: " << isPassed << endl;


cout << "Is Fail: " << isFail << endl;

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;

// void function (does not return anything)


void greet()
{
cout << "Hello Students!" << endl;
}

int main() {

greet(); // calling the void function

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.

DERIVED DATA TYPES


Data Meaning / Theory (Simple One-Line
What It Does
Type Explanation) Example
Stores multiple values of the same data Holds many int arr[3] = {10,
Array
type in one variable name. values together. 20, 30};
A variable that stores the address of Points to memory
Pointer int* ptr = &num;
another variable. location.
Another name (alias) for an existing Refers to original
Reference int& ref = num;
variable. variable.
A block of code that performs a specific Executes when
Function void greet();
task. called.

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;

// Function (Derived Type)


void display(int num)
{
cout << "Function received value: " << num << endl;
}

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.

USER-DEFINED DATA TYPES


Data Meaning / Theory (Simple What it is One-Line
Type Explanation) used for Example
Groups different types of data To store related struct Student { int
struct
under one name. data together. roll; char grade; };
Similar to struct, but also Used in Object-
class Car { public:
class supports functions and data Oriented
int speed; };
protection (OOP). Programming.

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

Allows storing different data


union Data { int x;
union types in the same memory Saves memory.
float y; };
location (only one at a time).
Used to give names to a set of Improves enum Day {Mon,
enum
constant values. readability. Tue, Wed};
Creates a new name (alias) for Makes code simple
typedef typedef int marks;
an existing data type. and readable.

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.

SYNTAX: Using const Keyword


const data_type variable_name = value;
Example:
const int age = 25;

Example Program:
#include <iostream>
using namespace std;

int main()
{
const float PI = 3.14; // Constant variable
float radius = 5;

float area = PI * radius * radius;

cout << "Radius = " << radius << endl;


cout << "Area of Circle = " << area << endl;

// PI = 3.1415; ❌ ERROR (Cannot change constant)

return 0;
}
OUTPUT:
Radius = 5
Area of Circle = 78.5

SYNTAX: Using #define (Preprocessor Constant)


#define NAME value
Example:
#define PI 3.14

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;

cout << "Area = " << area << endl;

return 0;
}
OUTPUT:
Area = 50.24

TYPES OF CONSTANTS IN C++


Type Example Meaning
Integer Constant 10, -5 Whole numbers
Floating Constant 3.14, 5.6 Decimal numbers
Character Constant 'A', '@' Single character
String Constant "Hello" Text
Boolean Constant true, false Logical value

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

cout << "Integer: " << num << endl;


cout << "Float: " << pi << endl;
cout << "Character: " << grade << endl;
cout << "String: " << name << endl;
cout << "Boolean: " << status << endl;

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.

Example: Without enum:


int day = 1; // What is 1? Monday? Tuesday?

Example: With enum:


Day today = Monday; // Easy to understand

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

Example: Example (Basic Understanding)


#include <iostream>
using namespace std;

int main()
{
enum Color { Red, Green, Blue };

Color c = Green;

cout << "Value of Green is: " << c << endl;

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;

enum Day { Monday = 1, Tuesday, Wednesday, Thursday, Friday };

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

TYPES OF OPERATORS IN C++


1. Arithmetic Operators
2. Relational (Comparison) Operators
3. Logical Operators
4. Assignment Operators
5. Compound Assignment Operators
6. Increment / Decrement Operators

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.

Operator Meaning Example (a = 10, b = 3) Result


+ Addition c = a + b; c = 13
- Subtraction c = a - b; c=7
* Multiplication c = a * b; c = 30
/ Division (Integer) c = a / b; c=3
% Modulus (Remainder) c = a % b; c=1
Assume:
int a = 10;
int b = 3;
int c;

Note: Since a and b are integers, a / b gives 3 (not 3.33) because it performs integer division.

Example Program: Arithmetic Operators


#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 3;

cout << "Addition: " << a + b << endl;


cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;

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

Example Program: Relational Operators


#include <iostream>
using namespace std;

int main() {
int a = 10;
int b = 5;

cout << "a == b : " << (a == b) << endl;


cout << "a != b : " << (a != b) << endl;
cout << "a > b : " << (a > b) << endl;
cout << "a < b : " << (a < b) << endl;
cout << "a >= b : " << (a >= b) << endl;
cout << "a <= b : " << (a <= b) << endl;

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.

Operator Meaning Example (a = 10, b = 5) Result


&& AND (a > 5 && b < 10) 1 (True)
! NOT !(a > 5) 0 (False)

Example:
int a = 10, b = 5;

(a > 5 && b < 10) // true (1)


(a > 5 || b > 10) // true (1)
!(a > 5) // false (0)

Example Program: Logical Operators


#include <iostream>
using namespace std;

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.

Operator Meaning Example Result


= Assign value a = 10; a becomes 10

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

cout << "Value of num = " << num << endl;

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.

COMPOUND ASSIGNMENT OPERATORS:


Compound assignment operators are a short way of writing calculations and assignment
together.
Instead of writing: a = a + 5;
We write: a += 5;

Both mean the same thing.

Operator Meaning Normal Form Example (a = 10) Result


+= Add and assign a = a + value a += 5; 15
-= Subtract and assign a = a - value a -= 3; 7
*= Multiply and assign a = a * value a *= 2; 20
/= Divide and assign a = a / value a /= 2; 5

Example Program: Assignment Operator


#include <iostream>
using namespace std;

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

Types of Increment / Decrement


There are two types:
1. Post-Increment (a++): Use the value first, then increase.
2. Pre-Increment (++a): Increase first, then use the value.

Example Program: Increment / Decrement


#include <iostream>
using namespace std;

int main()
{
int a = 5;

cout << "Initial value: " << a << endl;

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

Example: Post-Increment and Pre-Increment


int x = 5;
cout << x++; // prints 5
cout << ++x; // prints 7
Note: Mostly used in loops like for and while

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

It is mainly used when:


• We want accurate calculations
• We want to avoid data loss
• We want to convert data for specific operations

Types of Typecasting in C++

There are two types:


1. Implicit Typecasting (Automatic)
• Done automatically by the compiler.
• Happens when converting smaller data type → larger data type.

Example:
int a = 10;
float b = a; // int converted to float automatically
Here:
• a is int
• b becomes 10.0

Example Program: Implicit Typecasting


#include <iostream>
using namespace std;

int main()
{
int a = 10;
float b = a; // int -> float

cout << "Value of a (int): " << a << endl;


cout << "Value of b (float): " << b << endl;

return 0;
}

Output:
Value of a (int): 10
Value of b (float): 10.0
Here a becomes 10.0 when assigned to b.

2. Explicit Typecasting (Manual)


• Done manually by the programmer.
• Used when converting larger type → smaller type.
• May cause data loss.

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

cout << "Value of x (float): " << x << endl;


cout << "Value of y (int): " << y << endl;

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.

CONTROL STRUCTURES C++ FUNCTIONS: SIMPLE FUNCTIONS


A function in C++ is a block of code that performs a specific task.
• It helps in code reusability
• Makes the program organized
• Reduces repetition of code

A simple function:
• Does not return a value (void type)
• May or may not take parameters
• Is called inside main() function

Syntax of a Simple Function


return_type function_name()
{
// function body
}

Syntax of a Simple Function if the function does not return anything:


void function_name()
{
// statements
}

Syntax of a Simple Function Calling the function inside main()::


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 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!

Example: Function to print a number


#include <iostream>
using namespace std;

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

Call and Return by reference

Call by Reference means:


• Instead of passing a copy of a variable,
• We pass the original variable’s reference (address) to the function.
• Any changes made inside the function will affect the original variable.
• In C++, reference variables are declared using &.

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.

Example Program: Call by Reference


#include <iostream>
using namespace std;

void increase(int &num) // reference parameter


{
num = num + 5;
}

int main()
{
int value = 10;

cout << "Before function call: " << value << endl;

increase(value); // call by reference

cout << "After function call: " << value << endl;

return 0;
}
Output:
Before function call: 10
After function call: 15

RETURN BY REFERENCE:

Return by Reference means:


• A function returns a reference to a variable.
• The returned value can modify the original variable.
• The function return type is written as data_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 26 of 38
OBJECT ORIENTED PROGRAMMING (BOE-064) UNIT-1
Syntax
data_type& function_name(parameters)
{
return variable;
}

Example: If a function returns a reference of variable x, and we write:


getValue(x) = 50;
• Then the original value of x becomes 50.
• This happens because the function returns the reference (original memory location),
not a copy of the value.

Example Program: Call by Reference


#include <iostream>
using namespace std;

int& getValue(int &x)


{
return x; // returning reference
}

int main()
{
int num = 20;

getValue(num) = 50; // modifying original value

cout << "Value of num: " << num << endl;

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

The keyword used is inline.

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.

Example Program: Call by Reference


#include <iostream>
using namespace std;

inline int add(int a, int b)


{
return a + b;
}

int main()
{
int result = add(5, 3);

cout << "Sum is: " << result;

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.

DIFFERENCE BETWEEN NORMAL FUNCTION AND INLINE FUNCTION IN C++


Normal Function Inline Function
Function call is replaced by actual function
Function call is executed normally.
code.
Requires function call overhead (stack, jump,
No function call overhead.
return).
Execution may be slightly slower. Execution is faster for small functions.
Suitable for large and complex functions. Suitable for small and simple functions.
Declared without inline keyword. Declared using inline keyword.
May increase memory usage (code
Memory usage is less.
expansion).
NOTE:
• Normal Function: A function that is called normally during program execution.
• Inline Function: A function where the compiler replaces the function call with the actual
function code to improve speed.

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

Example Program: Macro


#include <iostream>
using namespace std;

#define SQUARE(x) x*x // Macro definition

int main()
{
int num = 4;
int result = SQUARE(num);

cout << "Square using Macro: " << result;

return 0;
}
Output:
Square using Macro: 16

Inline Function
• Defined using inline keyword.
• Processed by the compiler.
• Performs proper type checking.
• Safer than macros.

Syntax: Inline Function


inline return_type function_name(parameters)
{
return expression;
}

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;

inline int square(int x) // Inline function definition


{
return x * x;
}

int main()
{
int num = 4;
int result = square(num);

cout << "Square using Inline Function: " << result;

return 0;
}
Output:
Square using Inline Function: 16

Example Program: Both Macro & Inline Function


#include <iostream>
using namespace std;

// Macro definition
#define SQUARE(x) x*x

// Inline function definition


inline int square(int x)
{
return x * x;
}

int main()
{
int a = 5;

cout << "Macro result: " << SQUARE(a) << endl;


cout << "Inline function result: " << square(a) << endl;

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.

We can create multiple add() functions:


• One for two integers
• One for two floats

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;

// Function with two int parameters


int add(int a, int b)
{
return a + b;
}

// Function with two float parameters


float add(float a, float b)
{
return a + b;
}

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);

cout << "Square using Inline Function: " << result;

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;

// Function with one parameter


int display(int a)
{
return a;
}

// Function with two parameters


int display(int a, int b)
{
return a + b;
}

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.

Helps reduce multiple function definitions.


Makes function calls simpler.

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 add(int a, int b = 10) // default argument


{
return a + b;
}

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:

int add(int a, int b = 10)


• b has a default value of 10.
• If we call add(5), then:
✓ a=5
✓ b = 10 (default value is used)
• If we call add(5, 3), then:
✓ a=5
✓ b = 3 (default value is replaced)

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;
}

friend void show(Sample obj); // friend function declaration


};

// Friend function definition


void show(Sample obj)
{
cout << "Value of num: " << [Link];
}

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;
}

friend void display(Test t); // friend function


};

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;
}
};

class Derived : public Base


{
public:
void show()
{
cout << "This is Derived 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

You might also like