0% found this document useful (0 votes)
1 views35 pages

Module 1 Notes

The document provides an overview of Object Oriented Programming (OOP) using C++, covering its introduction, characteristics, applications, and core concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It also includes details on basic C++ program structure, keywords, data types, operators, variables, constants, and decision control statements. The content is structured to facilitate understanding of OOP principles and their implementation in C++ programming.
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)
1 views35 pages

Module 1 Notes

The document provides an overview of Object Oriented Programming (OOP) using C++, covering its introduction, characteristics, applications, and core concepts such as classes, objects, encapsulation, inheritance, and polymorphism. It also includes details on basic C++ program structure, keywords, data types, operators, variables, constants, and decision control statements. The content is structured to facilitate understanding of OOP principles and their implementation in C++ programming.
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 USING
C++

Module1

(Presented by : Devi Hari S & Arya S Vishwam)


INDEX

[Link] to OOP
[Link] of OOP
[Link] of OOP
[Link] of OOP

• Object
• Class
• Data Abstraction
• Data Encapsulation
• Inheritance
• Polymorphism

[Link] C++ Program Structure


[Link]
[Link] Data Types
[Link] in C++
[Link]
[Link] and Constants
[Link] Control Statements
[Link] Statements
[Link] Statements
1. Introduction to Object Oriented
Programming (OOP)

Object Oriented Programming (OOP) is a programming


paradigm in which programs are designed using objects
and classes. In traditional programming methods such as
procedural programming, programs are written as a
sequence of instructions. However, when programs become
large and complex, managing them using only procedures
becomes difficult. Object oriented programming was
introduced to solve this problem.
In OOP, programs are divided into small units called
objects. These objects represent real-world entities such as
students, cars, bank accounts, employees, etc. Each object
contains two important components: data (attributes) and
functions (methods). The data represents the properties of
the object, while the functions represent the actions that can
be performed by the object.
For example, consider a Student object. The properties of
a student may include name, roll number, and marks. The
actions related to the student may include displaying details
or calculating marks. These properties and actions are
grouped together into a class.
Object oriented programming provides several advantages
such as code reusability, modularity, security, and easier
maintenance of programs. Because of these advantages,
OOP is widely used in modern software development.
Programming languages such as C++, Java, Python, and
C# support object oriented programming concepts.

2. Characteristics of OOP

Object Oriented Programming has several characteristics


that make it powerful for designing and developing
software systems. These characteristics allow programmers
to create programs that are more structured, reusable, and
easier to maintain.

Class
A class is a blueprint or template used to create objects. It
defines the structure and behavior of objects. A class
contains data members and member functions. Data
members store information about the object, while member
functions define the operations that can be performed on the
object.
Classes help organize programs by grouping related data
and functions together. Once a class is created, multiple
objects can be generated from it. This helps reduce
duplication of code.

Object
An object is an instance of a class. Objects represent real-
world entities and contain actual data values. When a class
is defined, it only defines the structure of objects. Memory
is allocated only when objects are created.
For example, if a class called Student is defined, objects
such as student1, student2, and student3 can be created
from that class.

Encapsulation
Encapsulation is the process of combining data and
functions into a single unit called a class. It protects data
from unauthorized access and ensures that data can only be
accessed through specific methods.
Encapsulation is implemented using access specifiers such
as public, private, and protected.

Abstraction
Abstraction means hiding the internal implementation
details and showing only the necessary features of an object
to the user.
For example, when using a car, the driver only needs to
know how to operate the steering wheel, accelerator, and
brake. The internal working of the engine is hidden from
the user.

Inheritance
Inheritance allows a class to acquire properties and
behaviors of another class. The existing class is called the
base class, and the new class is called the derived class.
Inheritance helps in code reuse and reduces the need to
write the same code multiple times.

Polymorphism
Polymorphism means the ability of a function or operator to
take multiple forms. It allows the same function name to
perform different operations depending on the parameters.
In C++, polymorphism is commonly implemented using
function overloading.

3. Applications of OOP

Object Oriented Programming is widely used in various


fields of software development. One of the main advantages
of OOP is that it helps manage complex systems by
dividing them into smaller objects.
OOP is commonly used in desktop applications, web
applications, and mobile applications. Many modern
software systems are built using object oriented
programming concepts.
Another important application of OOP is in game
development. In games, objects represent characters,
weapons, vehicles, and environments.
OOP is also used in graphical user interface (GUI)
systems where objects represent buttons, windows, icons,
and menus.
In addition, OOP is used in simulation systems, database
management systems, and artificial intelligence
applications.

4. Concepts of OOP
Object

An object is an instance of a class that represents a real-


world entity. Objects contain both data members and
member functions. The data members store values while the
member functions define operations that can be performed
on the data.
Objects help represent real-world entities in programs.
Multiple objects can be created from the same class, and
each object can have different values.

Syntax
ClassName objectName;
Example Program
#include<iostream>
using namespace std;
class Student
{
public:
int roll;
};
int main()
{
Student s1;
[Link] = 5;
cout<<"Roll Number: "<<[Link];
return 0;
}

Class

A class is a user-defined data type that acts as a blueprint


for creating objects. It defines the structure and behavior of
objects. Classes contain data members and member
functions.
Classes help organize programs and make them easier to
maintain. By using classes, programmers can create
multiple objects without rewriting the same code.
Syntax
class ClassName
{
access_specifier:
data_members;
member_functions();
};
Example Program
#include<iostream>
using namespace std;
class Student
{
public:
int roll;
string name;
void display()
{
cout<<"Roll: "<<roll<<endl;
cout<<"Name: "<<name<<endl;
}
};
int main()
{
Student s1;
[Link] = 1;
[Link] = "Arya";
[Link]();
return 0;
}
Data Abstraction

Data abstraction means hiding the internal implementation


details and showing only the necessary information to the
user. It helps reduce complexity and makes programs easier
to use.
For example, when using a mobile phone, users can make
calls or send messages without knowing the internal
working of the phone.

Syntax Example
class Example
{
public:
void show();
};
Example Program
#include<iostream>
using namespace std;
class Example
{
public:
void show()
{
cout<<"Example of abstraction";
}
};
int main()
{
Example obj;
[Link]();
return 0;
}

Data Encapsulation

Encapsulation is the process of combining data and


functions into a single unit called a class. It protects data
from unauthorized access by using access specifiers.
Encapsulation improves program security and ensures that
data is accessed in a controlled way.

Syntax
class ClassName
{
private:
data;
public:
functions();
};
Example Program
#include<iostream>
using namespace std;
class Bank
{
private:
int balance;
public:
void deposit()
{
balance = 5000;
}
void display()
{
cout<<"Balance: "<<balance;
}
};
int main()
{
Bank b;
[Link]();
[Link]();
return 0;
}

Inheritance

Inheritance allows one class to inherit properties and


functions from another class. The existing class is called the
base class, and the new class is called the derived class.
Inheritance helps reuse existing code and reduces
duplication.
Syntax
class DerivedClass : access_specifier
BaseClass
{
};
Example Program
#include<iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Animal eats"<<endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout<<"Dog barks"<<endl;
}
};
int main()
{
Dog d;
[Link]();
[Link]();
return 0;
}
Polymorphism

Polymorphism is one of the most important concepts in


Object Oriented Programming. The word polymorphism
comes from two Greek words: poly meaning many and
morph meaning forms. Therefore polymorphism means
“one function having many forms.”
In programming, polymorphism allows a function or
operator to behave differently depending on the situation. It
enables the same function name to perform multiple tasks
based on the number or type of arguments passed to it. This
helps programmers write flexible and reusable code.
Polymorphism improves code readability and reduces the
need to create multiple function names for similar
operations. Instead of writing separate functions for
different tasks, the same function name can be used with
different parameters.
In C++, polymorphism is mainly implemented using
function overloading and operator overloading. Function
overloading means defining multiple functions with the
same name but different parameters. The compiler decides
which function to execute depending on the arguments
provided.
Syntax (Function Overloading)
return_type
functionName(parameter_list);
Example Program
#include<iostream>
using namespace std;
int add(int a,int b)
{
return a+b;
}
int add(int a,int b,int c)
{
return a+b+c;
}
int main()
{
cout<<"Sum of two numbers:
"<<add(2,3)<<endl;
cout<<"Sum of three numbers:
"<<add(2,3,4);
return 0;
}

5. Basic C++ Program Structure

A C++ program follows a specific structure so that the


compiler can understand and execute it correctly. Every C+
+ program consists of several parts such as header files,
namespace declaration, main function, and program
statements.
The program usually begins with header files that include
predefined libraries. These libraries contain functions that
can be used in the program. After that, the namespace
declaration is written to avoid writing long standard library
names repeatedly.
The main() function is the most important part of a C++
program because the execution of the program starts from
this function. Inside the main function, statements are
written to perform different operations such as input,
output, calculations, and logical operations.
Finally, the program ends with the return statement, which
indicates that the program has finished execution.

Syntax
#include<iostream>
using namespace std;
int main()
{
statements;
return 0;
}
Example Program
#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to C++ Programming";
return 0;
}

6. Keywords

Keywords are reserved words in C++ that have special


meanings and are predefined by the programming language.
These words cannot be used as variable names, function
names, or identifiers because they are already used by the
compiler to perform specific tasks.
Keywords help define the structure and behavior of a
program. They are used to declare data types, control
program flow, define classes, and perform many other
operations.
For example, keywords such as int, float, class, public,
private, if, else, return, and while are commonly used in
C++ programs.
Understanding keywords is important for programmers
because they form the basic building blocks of
programming languages.

Example
int number;
In this example, int is a keyword used to declare an integer
variable.
7. Basic Data Types

Data types specify the type of data that a variable can store.
They tell the compiler how much memory should be
allocated for the variable and what type of values can be
stored in it.
Using appropriate data types helps improve program
efficiency and ensures that data is stored correctly. Different
types of data require different amounts of memory, so
selecting the correct data type is important in programming.
Some commonly used basic data types in C++ include:
• int – used to store integer values such as 10, 20, or -5
• float – used to store decimal numbers such as 3.5 or
7.25
• double – used to store large decimal numbers with
higher precision
• char – used to store single characters such as 'A' or 'b'
• bool – used to store logical values such as true or false

Example
int age = 20;
float marks = 85.5;
char grade = 'A';
bool result = true;
In this example, different variables are declared using
different data types.

8. Streams in C++

Streams are used for input and output operations in C++


programs. They allow data to flow between the program
and input/output devices such as the keyboard and monitor.
In C++, the iostream header file provides two commonly
used stream objects:
• cin – used to take input from the user through the
keyboard
• cout – used to display output on the screen
The symbol >> is called the extraction operator and is used
with cin to receive input. The symbol << is called the
insertion operator and is used with cout to display output.
Streams make it easier for programs to interact with users
by allowing them to enter data and see results.

Example Program
#include<iostream>
using namespace std;
int main()
{
int number;
cout<<"Enter a number: ";
cin>>number;
cout<<"You entered: "<<number;
return 0;
}

9. Operators

Operators are symbols used to perform operations on


variables and values. They help perform calculations,
comparisons, and logical operations in programs.
Operators make programming easier because they allow
complex operations to be written in a simple form.
Different types of operators are used depending on the
operation being performed.
Some common types of operators in C++ include:
• Arithmetic operators (+, -, *, /, %) used for
mathematical calculations
• Relational operators (>, <, >=, <=, ==, !=) used for
comparisons
• Logical operators (&&, ||, !) used for logical
conditions
• Assignment operators (=, +=, -=) used to assign
values to variables
Example Program
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
cout<<"Addition: "<<a+b<<endl;
cout<<"Subtraction: "<<a-b<<endl;
cout<<"Multiplication: "<<a*b;
return 0;
}

10. Variables and Constants

A variable is a named memory location used to store data


in a program. The value stored in a variable can change
during program execution. Variables are used to store
information such as numbers, characters, and other data that
the program needs to process.
Variables must be declared with a specific data type before
they are used. The data type determines what type of value
can be stored in the variable.
A constant, on the other hand, is a value that cannot be
changed once it is defined. Constants are used when a fixed
value is required throughout the program.
Using constants helps prevent accidental changes to
important values and improves program reliability.

Example
int age = 20;
const float PI = 3.14;
Here, age is a variable whose value can change, while PI is
a constant whose value cannot be modified.

11. Decision Control Statements

Decision control statements allow a program to make


decisions based on conditions. These statements help the
program choose different actions depending on whether a
condition is true or false.
In many programs, different actions must be performed
based on user input or certain conditions. Decision control
statements help control the flow of the program.
The most commonly used decision control statements in C+
+ are:
• if statement
• if–else statement
• switch statement
These statements evaluate conditions and execute specific
blocks of code based on the result.

If Statement

The if statement is used to execute a block of code only


when a specified condition is true. If the condition is false,
the statements inside the if block will not be executed.
The condition inside the if statement usually involves
relational or logical operators. If the condition evaluates to
true, the program executes the statements inside the curly
braces.

Syntax
if(condition)
{
statements;
}
Example
#include<iostream>
using namespace std;
int main()
{
int number = 10;
if(number > 0)
{
cout<<"Number is positive";
}
return 0;
}

If–Else Statement

The if–else statement is used when the program needs to


choose between two alternatives. If the condition is true, the
statements inside the if block are executed. Otherwise, the
statements inside the else block are executed.
This statement is useful when a program must perform one
action when a condition is true and another action when it is
false.

Syntax
if(condition)
{
statements;
}
else
{
statements;
}
Example
#include<iostream>
using namespace std;
int main()
{
int number;
cout<<"Enter a number: ";
cin>>number;
if(number%2==0)
cout<<"Even number";
else
cout<<"Odd number";
return 0;
}

Switch Statement

The switch statement is used when a program needs to


check multiple conditions. Instead of writing many if–else
statements, the switch statement allows the program to
choose from several possible options.
Each possible option is represented by a case label. When
the value of the expression matches a case, the
corresponding block of code is executed.

Syntax
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Example
#include<iostream>
using namespace std;
int main()
{
int day = 2;
switch(day)
{
case 1:
cout<<"Monday";
break;
case 2:
cout<<"Tuesday";
break;
default:
cout<<"Invalid day";
}
return 0;

12. Looping Statements

In programming, there are situations where a block of code


needs to be executed repeatedly. Writing the same
statements multiple times makes the program longer and
harder to manage. To solve this problem, programming
languages provide looping statements.
Loops allow a set of instructions to be executed repeatedly
until a specific condition becomes false. They help reduce
code repetition and make programs more efficient and
easier to read.
In C++, there are three main types of loops:
1. while loop
2. do-while loop
3. for loop
Each loop works in a slightly different way, but all of them
are used to repeat a block of statements.

While Loop

The while loop is used when the number of iterations is not


known in advance. In this loop, the condition is checked
before executing the statements inside the loop.
If the condition is true, the statements inside the loop are
executed. After executing the statements, the condition is
checked again. This process continues until the condition
becomes false.
If the condition is false at the beginning, the loop will not
execute at all.

Syntax
while(condition)
{
statements;
}
Example Program
#include<iostream>
using namespace std;
int main()
{
int i = 1;
while(i <= 5)
{
cout<<i<<endl;
i++;
}
return 0;
}
Explanation
In this program, the variable i is initialized with the value 1.
The loop checks whether i is less than or equal to 5. If the
condition is true, the value of i is printed and then
incremented by 1. The loop continues until i becomes 6, at
which point the condition becomes false and the loop stops.
Do-While Loop

The do-while loop is similar to the while loop, but it has


one important difference. In a do-while loop, the statements
inside the loop are executed at least once, even if the
condition is false.
This happens because the condition is checked after
executing the statements inside the loop.
The do-while loop is useful when the program needs to
execute the statements at least once before checking the
condition.

Syntax
do
{
statements;
}
while(condition);
Example Program
#include<iostream>
using namespace std;
int main()
{
int i = 1;
do
{
cout<<i<<endl;
i++;
}
while(i <= 5);
return 0;
}
Explanation
In this example, the value of i is initially set to 1. The
program prints the value of i and then increases it by 1.
After executing the statements, the condition i <= 5 is
checked. If the condition is true, the loop continues. When i
becomes 6, the condition becomes false and the loop stops.

For Loop

The for loop is commonly used when the number of


iterations is known in advance. It combines initialization,
condition checking, and increment/decrement in a single
line.
Because of its compact structure, the for loop is widely
used in many programming situations such as counting
numbers, processing arrays, and performing repeated
calculations.

Syntax
for(initialization; condition;
increment)
{
statements;
}
Example Program
#include<iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 5; i++)
{
cout<<i<<endl;
}
return 0;
}
Explanation
In this program, the variable i is initialized with the value 1.
The condition i <= 5 is checked before each iteration. If the
condition is true, the number is printed. After printing, i is
increased by 1. The loop continues until i becomes greater
than 5.

13. Jump Statements

Jump statements are used to transfer control from one part


of a program to another. These statements help change the
normal flow of execution in a program.
Jump statements are often used inside loops and conditional
statements to control how the program executes.
In C++, the commonly used jump statements are:
1. break statement
2. continue statement
3. goto statement
Each of these statements serves a specific purpose in
controlling program execution.

Break Statement

The break statement is used to immediately terminate a


loop or a switch statement. When the break statement is
encountered, the program exits the loop and continues
executing the next statement after the loop.
The break statement is useful when a certain condition is
met and there is no need to continue the loop.

Syntax
break;
Example Program
#include<iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 10; i++)
{
if(i == 5)
break;
cout<<i<<endl;
}
return 0;
}
Explanation
In this program, the loop prints numbers from 1 to 10.
However, when i becomes 5, the break statement is
executed. This immediately stops the loop, and the program
exits the loop. As a result, only numbers 1 to 4 are printed.

Continue Statement

The continue statement is used to skip the current iteration


of a loop and move to the next iteration. When the continue
statement is executed, the remaining statements inside the
loop for that iteration are skipped.
This statement is useful when certain values should not be
processed but the loop should continue running.

Syntax
continue;
Example Program
#include<iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 5; i++)
{
if(i == 3)
continue;
cout<<i<<endl;
}
return 0;
}
Explanation
In this example, the loop prints numbers from 1 to 5. When
the value of i becomes 3, the continue statement is
executed. This skips printing the number 3 and moves to
the next iteration. As a result, the output will be 1, 2, 4, 5.

Goto Statement

The goto statement is used to transfer program control to


another part of the program using a label. It allows the
program to jump directly to a specific location in the code.
Although the goto statement can be useful in certain
situations, it is generally not recommended for regular use
because it can make programs difficult to read and
understand.

Syntax
goto label;
label:
statements;
Example Program
#include<iostream>
using namespace std;
int main()
{
int i = 1;
start:
cout<<i<<endl;
i++;
if(i <= 5)
goto start;
return 0;
}
Explanation
In this program, a label named start is created. The
program prints the value of i, increases it by 1, and then
checks the condition i <= 5. If the condition is true, the
program jumps back to the start label using the goto
statement. This continues until the condition becomes false.

You might also like