0% found this document useful (0 votes)
2 views37 pages

Programming Notes

The document compares Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP), highlighting their definitions, approaches, characteristics, and security features. It explains key OOP concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with examples and applications in various fields. Additionally, it covers data types, type compatibility, variable declaration, dynamic initialization, reference variables, and type casting in C++.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Programming Notes

The document compares Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP), highlighting their definitions, approaches, characteristics, and security features. It explains key OOP concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with examples and applications in various fields. Additionally, it covers data types, type compatibility, variable declaration, dynamic initialization, reference variables, and type casting in C++.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Campusify – Notes for MSBTE Diploma

Priority-Based | Topic-Wise | Exam-Oriented

1.1 Procedure-Oriented Programming (POP) vs. Object-


Oriented Programming (OOP)

Procedure-Oriented Programming (POP)

Definition

Procedure-Oriented Programming (POP) is a programming approach in which a


program is divided into functions. The primary focus is on procedures (functions) rather
than data.

Approach

Top-down approach is used in program design.

Theory

In the procedure-oriented approach, a problem is viewed as a sequence of tasks such as


reading, calculating, and printing. To perform these tasks, the program is divided into
several functions.

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:

•It becomes difficult to identify which function is using which data.


•Any modification to the data structure requires changes in all related functions.
•This increases the possibility of errors (bugs).

Therefore, data remains exposed, making POP less secure.

⚠️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.

Object-Oriented Programming (OOP)

Definition

Object-Oriented Programming (OOP) is a programming approach in which a program is


divided into objects. The main focus is on data (objects) rather than procedures.

Approach

Bottom-up approach is used in program design.

Theory

Object-Oriented Programming organizes programs around objects, where each object


contains both data and the functions that operate on that data.

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.

POP vs. OOP

Feature POP OOP

Approach Top-down approach Bottom-up approach

Focus Functions (procedures) Objects and data

⚠️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

Security Less secure More secure through data hiding

Feature POP OOP

Data is accessible to functions Data is hidden using access modifiers


Data Handling

Scalability Suitable for small programs Suitable for large applications

Communication Through parameter passing Through message passing

Code Reusability
Limited High (Inheritance, Polymorphism)

Functions are more important


Importance Data is more important

Overloading Not supported Supported (language-dependent)

Remember:
"POP tells the computer what to do, while OOP tells the object what it should
do."

1.2 Features of Object-Oriented Programming, Object-


Oriented Languages, and Applications of OOP
Features of Object-Oriented Programming

The six main features of OOP are:

•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

Definition: A class is a blueprint or template used to create objects.

Example: Class: Student

Data Members: Name, Roll No., Marks

2. Object

Definition: An object is a real instance of a class.

Example: Student s1;

Here, Student → Class, s1 → Object

Real-Life Example: Class: Mobile → Objects: Samsung, Vivo, iPhone

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

Example: ATM Machine


User only enters: Card, PIN, Amount. The internal processing remains hidden.

5. Inheritance

Meaning: Inheritance is the process by which one class acquires the properties and
methods of another class.

Example: Animal → Dog

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

For Circle → Draw Circle


For Rectangle → Draw Rectangle Same
function, different behavior.

Object-Oriented Languages

Examples of Object-Oriented Programming languages:

•C++
•Java
•Python
•C#
•Ruby

Applications of OOP

OOP is widely used in:


⚠️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

•Banking Systems
•Hospital Management Systems
•Student Management Systems
•Game Development
•GUI Applications
•Android Applications
•E-Commerce Websites

Applications of OOP (Exam-Oriented)

1. Banking Systems

•Used to manage customer accounts, transactions, loans, and ATM services.


•Each customer and account is represented as an object, making the system secure
and easy to maintain.

2. Game Development

•Used to develop video games.


•Game characters, vehicles, weapons, and enemies are represented as objects with their
own properties and behaviors.

3. GUI (Graphical User Interface) Applications

•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

•Used to manage products, customers, shopping carts, orders, and payments.


•Different objects interact with each other, making the application organized,
reusable, and scalable.

⚠️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.3 Data Types, Type Compatibility, Declaration of Variable,


Dynamic Initialization of Variable, Reference Variable, Type
Casting
Data Types
Definition
A data type specifies the type of value a variable can store. It also determines the amount of memory
allocated and the operations that can be performed on the data.

Real-Life Example
Think of a cupboard having different compartments.

 One compartment stores clothes.


 Another stores books.
 Another stores shoes.

Similarly, different data types store different kinds of values.

Types of Data Types in C++


C++ mainly provides the following data types:

Category Data Types

Basic (Fundamental) int, float, double, char, bool, void


Derived Array, Pointer, Function, Reference
User-defined class, structure, union, enum

Basic Data Types

Data Type Description Example

int Stores integer values int age = 20;

float Stores decimal values float price = 12.5;

double Stores large decimal values double pi = 3.141592;

char Stores a single character char grade = 'A';

bool Stores true or false bool status = true;

⚠️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

Data Type Description Example

void Represents no value void display();

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

Since int can be converted into float, this is compatible.

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.

✔ Incompatible types require type casting.

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;

Real life Example :


Redmi mobile;
Samsung mobile;
Apple mobile;
(mobile is a variable name and brands (apple,samsung) are data types)

Example :
int age;

float salary;

char grade;

bool result;

Initialization
Variables can also be initialized during declaration.

int age = 20;

float marks = 85.5;

char grade = 'A';

Real-Life Example
A variable is like a labeled storage box.

 Label = Variable Name


 Box Size = Data Type

⚠️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

 Item inside = Value

Dynamic Initialization of Variable


Definition
Dynamic initialization means assigning a value to a variable during program execution instead of giving it
a fixed value.

Example 1
int a = 10;
int b = 20;

int sum = a + b;

Here,

The value of sum is calculated during execution.

Example 2
int radius;

cin>>radius;

float area = 3.14 * radius * radius;

The value of area depends on user input.

Real-Life Example
Imagine ordering food online.

The final bill depends on:

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

Both variables refer to the same memory location.


⚠️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
datatype &reference_name = existing_variable;

Example
int num = 10;

int &ref = num;

cout<<num<<endl;

cout<<ref;

Output

10

10

Modifying Through Reference


int num = 10;

int &ref = num;

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.

Types of Type Casting


1. Implicit Type Casting
2. Explicit Type Casting

1. Implicit Type Casting


Performed automatically by the compiler.

Example
int a = 10;

float b = a;

Output

10.0

2. Explicit Type Casting


Performed manually by the programmer.

Syntax
(datatype)variable

Example
float marks = 89.75;

int total = (int)marks;

cout<<total;

Output

89

The decimal part is removed.

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

If someone asks only for the whole number,

It becomes ₹99.

This is similar to converting float → int.

1.4 Special Operators in C++: Scope Resolution Operator,


Memory Management Operators, Manipulators
Special Operators in C++
Definition
Special operators in C++ are operators that perform specific tasks beyond basic arithmetic and logical
operations. They help in accessing variables, managing memory, and formatting input/output.

The three important special operators are:

1. Scope Resolution Operator (::)


2. Memory Management Operators (new and delete)
3. Manipulators

1. Scope Resolution Operator (::)


Definition
The Scope Resolution Operator (::) is used to access a global variable, define member functions outside
a class, and access class members.

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.

Example 1: Accessing a Global Variable


#include<iostream>
using namespace std;

int num = 100;

⚠️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;

cout<<"Local Variable : "<<num<<endl;

cout<<"Global Variable : "<<::num;

return 0;
}
Output
Local Variable : 50
Global Variable : 100

Example 2: Defining a Function Outside the Class


#include<iostream>
using namespace std;

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.

One belongs to Computer Department and another to Mechanical Department.

To identify the correct Rahul, we specify the department.

Similarly, the Scope Resolution Operator (::) specifies exactly which variable or function should be
accessed.

Uses of Scope Resolution Operator


 Access global variables.
 Define member functions outside the class.
 Access class members.

⚠️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

2. Memory Management Operators


Definition
Memory Management Operators are used to allocate and deallocate memory dynamically during program
execution.

C++ provides two operators:

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

Why is delete Important?


If allocated memory is not released, it causes a memory leak, which wastes memory.

Real-Life Example
Imagine booking a hotel room.

 new → Booking a room.


 delete → Checking out and freeing the room.

If you never check out, the room remains occupied unnecessarily.

Difference Between new and delete


Feature new delete
Purpose Allocates memory Releases memory
Memory Allocated at runtime Freed after use
Return Value Returns memory address No return value
Usage new int delete ptr

3. Manipulators
Definition
Manipulators are special functions used to format the input and output in C++.

They improve the appearance and readability of program output.


⚠️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

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.

✔ new allocates memory.

✔ delete releases memory.

✔ Always use delete after new to avoid memory leaks.

✔ endl moves to the next line.

✔ setw() sets output width.

✔ setprecision() controls decimal places.

✔ fixed displays numbers in fixed-point format.

Frequently Asked Viva Questions


Q1. Why is the Scope Resolution Operator used?
Answer: It is used to access global variables and define member functions outside a class.

Q2. Which operator allocates memory dynamically?


⚠️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

Answer: new.

Q3. Which operator releases dynamically allocated memory?


Answer: delete.

Q4. Why is delete necessary?


Answer: It frees allocated memory and prevents memory leaks.

Q5. Which header file is required for setw() and setprecision()?


Answer: #include <iomanip>.

⚠️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.5 Structure of C++ Program, Basic Input/Output


Operators and Functions in C++, Simple C++ Program
Structure of a C++ Program
Definition
A C++ program consists of different sections, where each section has a specific purpose. Together, these
sections form the complete structure of a C++ program.

General Structure of a C++ Program


// Documentation Section

#include<iostream> // Link Section

using namespace std; // Definition Section

// Global Declaration Section

int main() // Main Function


{
// Declaration Section

// Executable Statements

return 0;
}

// User Defined Function Section

Components of a C++ Program


Section Purpose
Documentation Section Contains comments describing the program.
Link Section Includes header files required by the program.
Definition Section Defines constants or namespaces.
Global Declaration Section Declares global variables and function prototypes.
main() Function Starting point of program execution.
Declaration Section Declares local variables.
Executable Section Contains statements to perform the required task.
User-defined Function Section Contains programmer-defined functions.

Explanation of Each Section


1. Documentation Section
This section contains comments that explain the purpose of the program.

Example

// Program to calculate the sum of two numbers

⚠️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?

 Makes the program easier to understand.


 Improves readability.
 Ignored by the compiler.

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

using namespace std;

It allows us to use cout and cin without writing std::.

4. Global Declaration Section


Variables or function prototypes declared outside all functions are called global declarations.

Example

int marks = 100;

A global variable can be accessed by all functions in the program.

5. main() Function
The main() function is the entry point of every C++ program.

Program execution always starts from this function.

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;

These variables can be used only within the main() function.

7. Executable Section
This section contains the actual statements that solve the problem.

Example

sum = a + b;

8. User-defined Function Section


Functions created by the programmer are called user-defined functions.

Example

void display()
{
cout<<"Welcome";
}

Real-Life Example
Think of building a house.

 Documentation → House Plan


 Link Section → Construction Materials
 Definition Section → Rules
 Global Section → Water Tank
 main() → Main Entrance
 Declaration Section → Rooms
 Executable Section → Daily Activities
 User-defined Functions → Extra Facilities like Garden or Garage

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

Basic Input/Output Operators and Functions in C++


Definition
Input and output operations allow a program to communicate with the user.

 Input → Accepts data from the user.


 Output → Displays information on the screen.

Basic Input/Output Objects


Object Purpose
cin Accepts input from the keyboard.
cout Displays output on the screen.
cerr Displays error messages.
clog Displays log or diagnostic messages.

Input Operator (>>)


Definition
The extraction operator (>>) is used with cin to receive input from the user.

Syntax
cin >> variable;
Example
int age;

cin >> age;

Output Operator (<<)


Definition
The insertion operator (<<) is used with cout to display output.

Syntax
cout << variable;
Example
cout << age;

Example Program
#include<iostream>
using namespace std;

int main()
{
int age;

cout<<"Enter 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

Difference Between cin and cout


Feature cin cout
Purpose Accepts input Displays output
Operator >> <<
Data Flow Keyboard → Program Program → Screen
Object Type Input Stream Output Stream

Simple C++ Program


Program 1: Display a Message
#include<iostream>
using namespace std;

int main()
{
cout<<"Welcome to C++ Programming";

return 0;
}
Output
Welcome to C++ Programming

Program 2: Addition of Two Numbers


#include<iostream>
using namespace std;

int main()
{
int num1, num2, sum;

cout<<"Enter First Number : ";

cin>>num1;

cout<<"Enter Second Number : ";

cin>>num2;

sum = num1 + 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

Enter Second Number : 20

Sum = 30

Flow of a C++ Program


Start

Documentation Section

Header Files

using namespace std;

main()

Variable Declaration

Input

Processing

Output

return 0;

End

Important Points
✔ Every C++ program starts execution from the main() function.

✔ #include<iostream> provides input/output functionality.

✔ 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

✔ cin is used for input.

✔ cout is used for output.

✔ >> is called the Extraction Operator.

✔ << is called the Insertion Operator.

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

1.6 Class & Object: Introduction, Specifying a Class, Access


Specifiers, Defining Member Functions, Creating Objects,
Memory Allocation for Objects
Introduction to Class and Object
Definition
Object-Oriented Programming (OOP) is based on two fundamental concepts:

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

 The house plan is a Class.


 The actual houses built using that plan are Objects.

One blueprint can be used to build many houses.

Similarly, one class can be used to create many objects.

Class and Object Relationship


Class
(Blueprint)


│ Creates

Object 1 Object 2 Object 3

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.

Unlike a class, an object occupies memory.

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

Creating Multiple Objects


Student s1, s2, s3;

Three objects are created from the same class.

Real-Life Example
Class → Mobile

Objects

 Samsung
 Vivo
 iPhone

All mobiles have common properties like:

 Brand
 Price
 Storage

But each object stores different values.

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.

There are three access specifiers in C++:

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.

This is the default access specifier.

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.

It is mainly used in inheritance.

Difference Between Access Specifiers


Access Specifier Inside Class Outside Class Derived Class
Public ✔ Yes ✔ Yes ✔ Yes
Private ✔ Yes ✘ No ✘ No
Protected ✔ Yes ✘ No ✔ Yes

Easy Trick to Remember


 Public → Everyone can access.
 Private → Only the class can access.
 Protected → Class and child class can access.

Defining Member Functions


Member functions can be defined in two ways:

1. Inside the class


2. Outside the class

1. Member Function Inside the Class


When a function is defined within the class definition, it is called an inside class member function.

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.

2. Member Function Outside the Class


A function can also be defined outside the class using the Scope Resolution Operator (::).

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

Difference Between Inside and Outside Class Functions


Inside Class Outside Class
Defined inside the class Defined outside the class
Inline by default Uses :: operator
Suitable for small programs Suitable for large programs
Easier to write Better program organization

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

Creating Multiple Objects


Student s1;

Student s2;

Student s3;

Each object has its own copy of data members.

Memory Allocation for Objects


Definition
Memory is allocated only when an object is created, not when the class is defined.

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

 Class → No memory allocated for data members.


 Object → Memory allocated for data members.
 Member functions are shared by all objects.

Example
class Student
{
public:

int rollNo;

float marks;

};

When the class is declared:

Memory = 0 bytes allocated for objects

When an object is created:

Student s1;

Memory is allocated for:

 rollNo
 marks

If another object is created:

Student s2;

A separate copy of rollNo and marks is allocated for 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

Create Another Object

s2

rollNo

marks

display() ← Shared by all objects

Real-Life Example
Imagine a rubber stamp.

 The stamp is the Class.


 Every impression made using the stamp is an Object.

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.

✔ A class does not occupy memory until an object is created.

✔ Objects are created using the class name.

✔ Data members occupy separate memory for each object.

✔ Member functions are shared by all objects.

✔ The dot (.) operator is used to access members.

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

You might also like