You are on page 1of 45

What is

Object
Oriented
Programming What is Object Oriented Programming
Dr. Munesh
Singh

Dr. Munesh Singh

Indian Institute of Information Technology


Design and Manufacturing,
Kancheepuram
Chennai-600127

December 20, 2018


What is OOP?

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Object Oriented Programming is a guideline for the
programmer to program in an efficient and more
manageable way
OOP principle leads to less error in the code, increase
collaboration among the developers and ease of
maintenance
OOPs is a programming approach which resolves around
the concept of ”Object”
Traditional C stlye Vs OOP style

What is
Object
Oriented
Programming

Dr. Munesh
Singh

running();
Traditional C stlye Vs OOP style

What is
Object
Oriented
Programming

Dr. Munesh
Singh

running();
ramesh.running();
What is an Object

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Any entity is an object.


How to represent real world things in a program?
Object is complete in terms of itself, as it contains
memory structure to hold information needed to describe
itself and set of operations that can affect or access its
information set
Concept of Classes and Objects

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Class is a blueprint of an Object.
Class is a description of Objects property set and set of
operations
Creating class is as good as defining a new data type
Class is a means to achieve encapsulation
Object is a run time entity
Object is an instance(example) of a class
CLass vs Object

What is
Object class box
Oriented
Programming
{
Dr. Munesh
Singh int l,b,h;
void setDimension(intx, int y, int z)
{.....}
void showDimension()
{....}
};
box b1;
box b2;
Software Development in C++

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Principle of OOPS

What is
Object
Oriented
Programming

Dr. Munesh
Singh Encapsulation(binding the properties with function)
Data Hiding (encapsulation also leads to data hiding)
Abstraction (hiding the details from the outer side of
world)
Inheritance (deriving the properties from your ancestors)
Polymorphism
Overriding (define the normal walking and redefine with
different walking)
Overloading (change the part to function e.g hand to walk)
Identifiers

What is
Object
Oriented
Programming

Dr. Munesh
Objective
Singh
Constants
Any information is constant
Data=information=constant
Types of constant primary and secondary
primary int, real, constant
Secondary constant Array, String, Pointer, Union,
Structure, Enumerator, and class
Secondary constant derived from the primary constant
Variables
Keywords
In Memory

What is
Object Variables
Oriented
Programming

Dr. Munesh
Singh

Variables are the names of the memory locations where we


store data
Variable name is any combination of alphabet (a to z or A
to Z), digit (0 to 9), and underscore
Valid variable name cannot start with number
Keywords

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Datatypes

What is
Object
Oriented
Programming

Dr. Munesh
Singh

int (e.g int a,b=5;)


char (e.g char ch=’a’;)
float (e.g float k=3.45;)
double (e.g double d1;)
void
Input Output in C++

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Objective
Output Instruction
Input Instruction
About iostream.h
Sample program
Output Instruction

What is
Object
Oriented
Programming

Dr. Munesh In C, standard output device is monitor and printf() is use


Singh
to send data/message to monitor.
printf() is a predefined function
In C++, we can use cout to send data/message to monitor
Output Instruction

What is
Object
Oriented
Programming

Dr. Munesh In C, standard output device is monitor and printf() is use


Singh
to send data/message to monitor.
printf() is a predefined function
In C++, we can use cout to send data/message to monitor
cout is a predefined object
The operator << is called the insertion or put to operator
Example: cout<<”Hello C++”;
cout<<”sum of”<<a<<”and”<<b<<”is”<<c;
cout<<a+b;
Input Instruction

What is
Object
Oriented
Programming

Dr. Munesh
Singh

The identifier cin is a predefined object in c++


The operator >> is known as extraction or get from
operator
Example: cin>>a;
cout>>a>>b
About iostream.h

What is
Object
Oriented
Programming

Dr. Munesh
Singh

We need to include header file iostream.h, it contains


declarations for the identifier cout and the operator <<.
And also for the identifier cin and operator >>
Header file contains declaration of identifiers
Identifier can be function names, variables, objects and
macros etc
endl

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Inserting endl into the output stream causes the screen


cursor to move to the beginning of the next line.
endl is a manipulator and it is declared in iostream.h
newline character also works as it work in c
Use of newline required double ”” but for endl no need of
””
Sample Program (test.cpp)

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Reference Varaibles in C++

What is
Object Types of Varaibles
Oriented
Programming

Dr. Munesh
Singh
Reference Varaibles in C++

What is
Object
Oriented
Programming

Dr. Munesh
Singh Types of Varaibles
Reference means address
Reference Variable is an internal pointer
Declaration of Reference Variable is preceded with ’&’
symbol (but do not read it as ’address of’)
Reference variables must be initialized during declaration.
It can be initialized with already declared variables only.
Reference variables can not be updated
Function in C++

What is
Object
Oriented
Programming

Dr. Munesh
Singh
What is function?
Function is block of code performing a unit task.
Function has a name, return type and arguments.
Function is a way to achieve modularization
Functions are predefined and user-defined
Predefined functions are declaration in header file ans
defined in library files.
Definition, Declaration and Call

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Declaration

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Function Declaration is also known as Function prototype


Functions need to be declared before use (just like
Variables)
Function can be declared locally or globally
ReturnType functionName(argument list)
Function definition is a block of code
Ways to define a function?

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Take Nothing, Return Nothing


Takes Something, Return Nothing
Takes Nothing, Return Something
Takes Something, Return Something
Formal and Actual Arguments

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Types of Formal Arguments

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Formal arguments can be three types


Ordinary variables of any type
Pointer variables
Reference variables
Call by value

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Call by Address

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Call by Reference

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Benefit of Function

What is
Object
Oriented
Programming

Dr. Munesh
Singh

Easy to read
Easy to Modify
Avoids rewriting of same code
Easy to debug
Better memory utilization
Function saves memory

What is
Object Function Memory
Oriented
Programming Function in a program is to save memory space which
Dr. Munesh becomes appreciable when a function is likely to be called
Singh
many times.

Function is time consuming


However every time a function is called, it takes lot of
extra time in executing a series of instructions for tasks
such as jumping to the functions, saving registers, pushing
arguments into the stack and returning to the calling
function.

So...
So when function is small it is worthless to spend so much
extra time in such tasks in cost of saving comparatively
small space.
Inline Function

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Benefit of Inline
To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function
An inline function is a function that is expanded in line
when it is invoked
Compiler replaces the function call with the corresponding
function code.
What is
Object
Oriented
Programming

Dr. Munesh
Singh
Inline is request
inline is a request not a command.
The benefit of speed of inline functions reduces as the
function grows in size.
So the compiler may ignore the request in some situations.
Few of them:
Function containing loops, switch, goto.
Functions with recursion
Containing static variable.
Example

What is
Object
Oriented
Programming

Dr. Munesh
Singh
Default Argument in C++

What is
Object Function argument
Oriented
Programming

Dr. Munesh
Singh
Function Overloading

What is
Object
Oriented
Programming

Dr. Munesh
Singh
How Function Overloading is Resolved?

What is
Object
Oriented
Programming

Dr. Munesh
Singh First, C++ tries to find an exact match. This is the case
where the actual argument exactly matches the parameter
type of one of the overloaded functions.
If no exact match is found, C++ tries to find a match
through promotion
Char, unsigned char, and short is promoted to an int.
float is promoted to double
If no promotion is found, C++ tries to find a match
through standard conversion
Strucure in C++

What is
Object Three important points
Oriented
Programming Structure is collection of dissimilar elements
Dr. Munesh
Singh Structure is a way to group variables
Structure is used to create data type
struct book
{
int bookid;
char title[20];
float price;
};
void main()
{
book b1;
Strucure in C++

What is
Object Function define
Oriented
Programming void main()
Dr. Munesh
Singh {
book b1;
b1=input();
};
book input()
{
book b;
cout<<”Enter bookid, title and price”;
cin>>b.bookid>>b.title>>b.price;
return b;
}
Strucure in C++

What is
Object
Oriented Function define
Programming
struct book
Dr. Munesh
Singh {
int bookid;
char title[20];
float price;
void input()
{
cout<<”Enter bookid, title and price”;
cin>>bookid>>title>>price;
}
};
Main() function

What is
Object
Oriented
Programming

Dr. Munesh
Singh

void main()
{
book b1;
b1.input();
}
Strucure in C++

What is
Object Function define
Oriented
Programming struct book
Dr. Munesh
Singh
{
private:
int bookid;
char title[20];
float price;
public:
void input()
{
cout<<”Enter bookid, title and price”;
cin>>bookid>>title>>price;
}
Main() function

What is
Object
Oriented
Programming

Dr. Munesh
Singh

void main()
{
book b1;
b1.bookid–x(Private)
b1.input();–(Public)
}

You might also like