Programming Notes
Programming Notes
Definition
Approach
Theory
POP mainly focuses on developing functions, while less attention is given to the data
used by those functions.
In large programs, important data is often declared as global data, allowing multiple
functions to access and modify it. As a result:
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Real-Life Example
Writing a program in POP is like following a cooking recipe, where each step is performed
one after another.
Another example is a traditional library, where books are left on open tables. Anyone can
access, modify, or misplace them because there are no restrictions.
Characteristics
•Program is divided into functions.
•Focuses on functions.
•Data is shared globally.
•Uses a top-down approach.
•Less secure.
•Limited code reusability.
Definition
Approach
Theory
Since objects are independent, they can be reused in different programs without major
modifications.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
OOP closely binds data with its related functions, protecting it from unauthorized access or
accidental modification.
Real-Life Example
Object: Car
Properties (Data):
•Color
•Brand
•Speed
Behaviors (Functions):
•Start()
•Stop()
•Brake()
Characteristics
•Program is divided into objects.
•Focuses on data.
•Data is secure through data hiding.
•Uses a bottom-up approach.
•High code reusability.
•Easy to maintain.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Code Reusability
Limited High (Inheritance, Polymorphism)
Remember:
"POP tells the computer what to do, while OOP tells the object what it should
do."
•Class
•Object
•Encapsulation
•Data Abstraction
•Inheritance
•Polymorphism
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
1. Class
2. Object
3. Encapsulation
Meaning: Encapsulation is the process of wrapping data and functions together into a
single unit (class).
Example:
class Student
{
int roll;
void display();
};
Here, both data and functions are enclosed within the same class.
4. Data Abstraction
Meaning: Data abstraction means showing only the necessary information while hiding the
implementation details.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
5. Inheritance
Meaning: Inheritance is the process by which one class acquires the properties and
methods of another class.
Dog inherits: Eat(), Sleep(). Dog can also have its own function: Bark()
6. Polymorphism
Meaning: Polymorphism allows one function to perform different tasks depending on the
object.
Example: draw()
Object-Oriented Languages
•C++
•Java
•Python
•C#
•Ruby
Applications of OOP
•Banking Systems
•Hospital Management Systems
•Student Management Systems
•Game Development
•GUI Applications
•Android Applications
•E-Commerce Websites
1. Banking Systems
2. Game Development
•Used to develop desktop applications with buttons, menus, text boxes, and
windows.
•Each GUI component is treated as an object, making the interface easier to design
and manage.
4. E-Commerce Applications
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Real-Life Example
Think of a cupboard having different compartments.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Example Program :
#include<iostream>
using namespace std;
int main()
{
int age = 20;
float marks = 88.5;
char grade = 'A';
bool pass = true;
cout<<age<<endl;
cout<<marks<<endl;
cout<<grade<<endl;
cout<<pass;
return 0;
}
Type Compatibility
Definition
Type compatibility means assigning one data type to another compatible data type without causing errors.
If the data types are incompatible, explicit type conversion (type casting) is required.
Compatible Example
int a = 10;
float b = a;
Output
10.0
Incompatible Example
char ch = 'A';
int arr[10];
arr = ch;
This is invalid because different types cannot always be assigned to each other.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Important Point
✔ Compatible types can be assigned directly.
Declaration of Variable
Definition
A variable declaration tells the compiler the variable's name and data type before it is used.
Syntax
datatype variable_name;
Example :
int age;
float salary;
char grade;
bool result;
Initialization
Variables can also be initialized during declaration.
Real-Life Example
A variable is like a labeled storage box.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Example 1
int a = 10;
int b = 20;
int sum = a + b;
Here,
Example 2
int radius;
cin>>radius;
Real-Life Example
Imagine ordering food online.
Quantity
Discount
Delivery charge
Since these values change every time, the total amount is dynamically calculated.
Reference Variable
Definition
A reference variable is another name (alias) for an existing variable.
Syntax
datatype &reference_name = existing_variable;
Example
int num = 10;
cout<<num<<endl;
cout<<ref;
Output
10
10
ref = 50;
cout<<num;
Output
50
Changing ref also changes num because both refer to the same memory.
Real-Life Example
Rahul is also called Raju.
Rahul and Raju are different names, but they refer to the same person.
Similarly,
num
Memory
ref
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Type Casting
Definition
Type casting is the process of converting one data type into another.
Example
int a = 10;
float b = a;
Output
10.0
Syntax
(datatype)variable
Example
float marks = 89.75;
cout<<total;
Output
89
Real-Life Example
Suppose you have ₹99.75.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
It becomes ₹99.
Syntax
class_name::function_name()
OR
::variable_name
Why is it Needed?
Sometimes a local variable and a global variable have the same name. In such cases, the scope resolution
operator is used to access the global variable.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
int main()
{
int num = 50;
return 0;
}
Output
Local Variable : 50
Global Variable : 100
class Student
{
public:
void display();
};
void Student::display()
{
cout<<"Welcome to C++";
}
int main()
{
Student s;
[Link]();
return 0;
}
Real-Life Example
Imagine there are two students named Rahul in a classroom.
Similarly, the Scope Resolution Operator (::) specifies exactly which variable or function should be
accessed.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
new
delete
new Operator
Definition
The new operator allocates memory dynamically at runtime.
Syntax
pointer = new data_type;
Example
#include<iostream>
using namespace std;
int main()
{
int *ptr = new int;
*ptr = 25;
cout<<*ptr;
return 0;
}
Output
25
Advantages of new
Allocates memory during execution.
Memory size can be decided at runtime.
Returns the memory address.
delete Operator
Definition
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
The delete operator releases the memory allocated using the new operator.
Syntax
delete pointer;
Example
#include<iostream>
using namespace std;
int main()
{
int *ptr = new int;
*ptr = 50;
cout<<*ptr<<endl;
delete ptr;
return 0;
}
Real-Life Example
Imagine booking a hotel room.
3. Manipulators
Definition
Manipulators are special functions used to format the input and output in C++.
Common Manipulators
Manipulator Purpose
endl Moves cursor to the next line
setw() Sets the width of output
setprecision() Controls decimal places
fixed Displays fixed-point notation
left Left-aligns the output
right Right-aligns the output
1. endl
Example
cout<<"Hello"<<endl;
cout<<"World";
Output
Hello
World
2. setw()
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(10)<<"C++";
return 0;
}
Output
C++
3. setprecision()
Example
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float pi = 3.141592;
cout<<setprecision(4)<<pi;
return 0;
}
Output
3.142
4. fixed
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Example
cout<<fixed<<setprecision(2)<<25.5678;
Output
25.57
Real-Life Example
Manipulators are like formatting options in Microsoft Word.
Just as you change the font size, alignment, or spacing to make a document look neat, manipulators format
the program's output to make it easier to read.
Advantages of Manipulators
Improve output formatting.
Increase readability.
Display decimal values accurately.
Align output properly.
Exam Tips
Remember
✔ :: is used to access global variables and define functions outside a class.
Answer: new.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
// Executable Statements
return 0;
}
Example
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Why is it used?
2. Link Section
The Link Section includes the required header files.
Example
#include<iostream>
Here,
iostream provides input and output facilities like cin and cout.
3. Definition Section
This section defines constants or namespaces.
Example
Example
5. main() Function
The main() function is the entry point of every C++ program.
Example
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
int main()
{
return 0;
}
6. Declaration Section
Local variables are declared inside the main() function.
Example
int a, b;
7. Executable Section
This section contains the actual statements that solve the problem.
Example
sum = a + b;
Example
void display()
{
cout<<"Welcome";
}
Real-Life Example
Think of building a house.
Each part has a specific purpose, just like sections of a C++ program.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Syntax
cin >> variable;
Example
int age;
Syntax
cout << variable;
Example
cout << age;
Example Program
#include<iostream>
using namespace std;
int main()
{
int age;
cin>>age;
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
cout<<"Age = "<<age;
return 0;
}
Output
Enter Age : 20
Age = 20
int main()
{
cout<<"Welcome to C++ Programming";
return 0;
}
Output
Welcome to C++ Programming
int main()
{
int num1, num2, sum;
cin>>num1;
cin>>num2;
cout<<"Sum = "<<sum;
return 0;
}
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Output
Enter First Number : 10
Sum = 30
Documentation Section
Header Files
main()
Variable Declaration
Input
Processing
Output
return 0;
End
Important Points
✔ Every C++ program starts execution from the main() function.
✔ using namespace std; allows the use of cin and cout without std::.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Exam Tips
Remember
main() is the starting point of program execution.
cin takes input from the keyboard.
cout displays output on the screen.
>> receives data.
<< sends data to the output device.
Every C++ program should end with return 0; (for int main()).
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Class
Object
A class is a blueprint or template used to create objects, while an object is a real-world instance of a class.
Real-Life Example
Think of a house plan.
│
│ Creates
▼
Class
Definition
A class is a user-defined data type that groups data members (variables) and member functions
(functions) into a single unit.
A class does not occupy memory until its objects are created.
Syntax
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
class ClassName
{
access_specifier:
data members;
member functions;
};
Example
class Student
{
public:
int rollNo;
char name[20];
void display()
{
cout<<"Student Information";
}
};
Components of a Class
Component Description
Class Name Name of the class
Data Members Variables declared inside the class
Member Functions Functions declared inside the class
Access Specifier Controls accessibility of members
Object
Definition
An object is an instance of a class.
It is used to access the data members and member functions of the class.
Syntax
ClassName objectName;
Example
Student s1;
Here,
Student → Class
s1 → Object
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Real-Life Example
Class → Mobile
Objects
Samsung
Vivo
iPhone
Brand
Price
Storage
Specifying a Class
A class is specified using the class keyword followed by:
Class name
Access specifier
Data members
Member functions
Example
class Employee
{
private:
int id;
public:
void input()
{
}
void display()
{
}
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
};
Explanation
Employee → Class Name
id → Data Member
input() → Member Function
display() → Member Function
Access Specifiers
Definition
Access specifiers define the accessibility of data members and member functions of a class.
1. Public
2. Private
3. Protected
1. Public
Members declared as public can be accessed from anywhere in the program.
Example
class Student
{
public:
int rollNo;
};
2. Private
Members declared as private can be accessed only inside the class.
Example
class Student
{
private:
int marks;
};
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
3. Protected
Members declared as protected can be accessed inside the class and by derived (child) classes.
Example
#include<iostream>
using namespace std;
class Student
{
public:
void display()
{
cout<<"Welcome";
}
};
int main()
{
Student s;
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
[Link]();
return 0;
}
Advantages
Easy to write.
Suitable for small programs.
Function becomes inline by default.
Syntax
return_type ClassName::functionName()
{
// Function Body
}
Example
#include<iostream>
using namespace std;
class Student
{
public:
void display();
};
void Student::display()
{
cout<<"Welcome to C++";
}
int main()
{
Student s;
[Link]();
return 0;
}
Advantages
Keeps the class definition short.
Easy to manage large programs.
Improves readability.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Creating Objects
Definition
Creating an object means allocating memory for a class and allowing access to its members.
Syntax
ClassName objectName;
Example
Student s1;
Accessing Members
The dot (.) operator is used to access data members and member functions.
Example
Student s1;
[Link]();
Student s2;
Student s3;
Important Points
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
Example
class Student
{
public:
int rollNo;
float marks;
};
Student s1;
rollNo
marks
Student s2;
Memory Diagram
Class Student
rollNo
marks
display()
Create Object
s1
rollNo
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.
Campusify – Notes for MSBTE Diploma
Priority-Based | Topic-Wise | Exam-Oriented
marks
s2
rollNo
marks
Real-Life Example
Imagine a rubber stamp.
Each impression has its own ink on paper (its own data), but all are created from the same stamp (same class
definition).
Exam Tips
Remember
✔ A class is a blueprint; an object is an instance of the class.
✔ The Scope Resolution Operator (::) is used to define member functions outside the class.
⚠️Note: Unauthorized sharing, distribution, or reproduction of this PDF is illegal. This material is licensed
for individual use only and must not be shared with others.