Organization of data and functions in object oriented programming

Object Oriented programming
Object Oriented programming
• Programming Language?
• Object Oriented Paradigm.
a. Procedural-Oriented Programming.
b. Object Oriented Programming.
• Characteristics of Object-Oriented Language.
Programming Language
4th Generation Language
Pascle
C
High-Level Language
Assembly Language
Machine Language
Hardware
Layers of Computer Software
Programming Paradigms
Different approaches to building solutions to
specific type of problems.
Two most important Paradigms are
i. Procedural-Oriented Programming.
ii. Object Oriented Programming.
Procedural-Oriented Programming
• List or Set of instruction telling a computer
What to do step by step
• It focus on the Process rather than Data.
• Example C,COBAL…….etc
Procedural-Oriented Programming
Typical structure of procedure oriented programs
Procedural-Oriented Programming
Relationship of data and functions in procedural programming
Procedural-Oriented Programming
• DRAWBACKS:• In a large program it is very difficult to find or identify what data is
being used by which function.
• It does not model real world problem very well.
• FEATURES OF POP:• Emphasis on doing things(algorithm) means the main focus is on
writing algorithms.
• Most of the functions share global data.
• TOP DOWN APPROACH in program design.
• Functions transform data from one form to another.
• Data move openly around the system from one function to another.
• Large program is divided into smaller programs known as functions.
Object Oriented Programming
• The major motivating factor in the invention
of object oriented is to remove some of
the flaws encountered in the procedural
oriented approach.
• This approach to problem solving where all
computations are carried out using Object.
• Object is component of a program that know
how to perform certain action and how to
interact with other element of the program.
Object Oriented Programming
Organization of data and functions in object oriented programming
Object Oriented Programming
Some characteristics of Object Oriented Programming are :1) Emphasis is on data rather than procedures or algorithms.
2) Programs are divided into what are known as objects.
3) Data structures are designed such that characterize the objects.
4) Functions that operate on the data are tied together in the data
structure.
5) Data is hidden and cannot be accessed by external functions.
6) Objects may communicate with each other through functions.
7) New data and functions can be easily added whenever necessary.
8) Follows bottom-up approach in program design.
Characteristics of Object-Oriented
Language
• Here are few major elements of Object-Oriented
languages.
- Objects
- Classes
- Data abstraction
- Data encapsulation
- Inheritance
- Polymorphism
- Dynamic binding
- Message passing
Characteristics of Object-Oriented
Language
• Classes:Classes are data types based on which
object are created.
Class is collection of function and variables.
Each function is used for particular purpose.
Example:-Car , Plan .
Characteristics of Object-Oriented
Language
• Object:Object is the basic unit of OOP.
Object can identified by its unique name.
There is more than one instance of an object.
Each instance of an object can hold its own
relevant data.
Ex:-Car Key
Characteristics of Object-Oriented
Language
• Data abstraction:Showing Essential features
and Hiding non-essential features to the user.
Ex:- Tv remote
• Encapsulation:is a process of binding or
wrapping the data and the codes that operate
on the data into a single entity.
Example:-Email a/c.
Characteristics of Object-Oriented
Language
• Inheritance:• Polymorphism:• Dynamic Binding :Dynamic binding also called
dynamic dispatch is the process of linking
procedure call to a specific sequence of code
(method) at run-time. It means that the code to
be executed for a specific procedure call is not
known until run-time. Dynamic binding is also
known as late binding or run-time binding.
Characteristics of Object-Oriented
Language
• Message Passing is nothing but sending and
receiving of information by the objects same as
people exchange information. So this helps in
building systems that simulate real life. Following
are the basic steps in message passing.
• Creating classes that define objects and its
behavior.
• Creating objects from class definitions
• Establishing communication among objects
Hello World
Hello World
• Main() Function: Similar to C,C++ Program must contain a function
Called main().
 Execution of program start.
 Format:void main()
{
// Program Body
}
Hello World
/*hello.c: Printing Hello word Message */
#includde<stdio.h>
Void main()
{
printf(“Hello Word”);
}
Hello World
1: //hello.cpp:printing hello word message --- Comment
2: #include<iostream.h>
Preproassor directive
3: void main()
Function Declaration
4: {
Function begin
5:
cout<<“Hello World”; //body of function main
6: }
End Function
Stream Based I/O
• Output Stream :-Output stream allow to
perform write operations on output device
such as screen, disk etc..
Object
Hello
Cout
<<
insertion operator
Cout<<“Age =”<<age; Cascaded ouput
Hello
Stream Based I/O
• Input Stream :-Input Stream allow to perform
read operations with input device such as
keyboard,disk etc..
Object
variable
Keyboard
Cin>>variable;
Cin
>>
extraction operator
name
Use of Class
#include<iostream.h>
directive
Using namespace std;
Class Person
{
Char name[30];
int age;
Public:
Void getdata(void)
{
Cout<<“Enter name”;
Cin>>name;
Cout<<“Enter age”;
Cin>>age;
}
Preproassor
Class Declaration
Member fun. Def.
Void display(void)
Member fun.
Def.
{
Cout<<“Name :”<<name;
Cout<<“Age :”<<age;
}
};
Int main()
{
Person p;
p.getdata();
p.display();
return 0;
}
Main Function Progm.
Structure of C++
Include File
Class Declaration
Member Function Declaration
Main Function Program
• User define data type:- the C++ language
allow us to create and use data type other
than the fundamental data type.
1.Structure:- In c++ a structure is a collection of
variables that are referenced by a single name.
Syntax:Struct name
{
data-type member1;
}
Storage organization when structure variable is
define
Struct Student
{
int roll_no;
char name[25];
Char branch[15];
int mark;
}
2 bytes
25 bytes
15 bytes
2 bytes
Structure
#include <iostream>
Using namespace std;
Struct Person
{ int age;
float salary;
};
int main()
{
Person P1;
cout<<“Enter age” ;
Cin>>p1.age;
Cout<<“Enter salary”;
Cin>>p1.salary;
Cout<<“age”<<p1.age
<<Endl;
Cout<<“Salary”<<p1.salar
y <<Endl;
return 0;
}
• Unions: unions are conceptually similar to structure as
they allow us to group together dissimilar type
element inside a single unit.
Difference :- Size of a structure type is equal
to the sum of the sizes of individual member.
Size of a union is equal to the size of its largest
member element.
Storage organization when structure variable is
define
Union student
{
int roll_no;
char name[25];
Char branch[15];
int mark;
}
25 by
#include <iostream>
Struct
{ char name[25];
int idno;
float salary;
}emp;
Union
{ char name[25];
int idno;
float salary;
}desc;
Void main()
{ cout<<“The size of the
structure
is”<<sizeof(emp)<<endl;
cout<<“The size of the
Union”<<sizeof(desc)<<endl
;
}
Output:The size of the structure is 31
The size of the Union is 25
Enumerated:- An
Enumeration is userdefined data type that
consists of integral
constants to define an
enumeration keyword
enum
#include<iostream>
Using namespace std;
Enum
week{Sunday,Monday,Tue
sday,Wenesday,Friday,Sat
urday}
int main()
{ week today;
Today=Wenseday;
Cout<<“Day”<<today+1;
return 0;
}
#include<iostream>
Using namespace std;
Enum seasons{spring=34,summer=4,winter=32};
Int main()
Seasons s;
S=summer;
Cout<<“Summer=”<<s<<Endl;
Return 0;
}
• Reference Variable:C++ support new type of variable called
reference variable.
Reference variable behaves similar to value
variable and pointer variable.
Reference variable act as alias(alternative
name) for previous define variables.
Data type & Reference Variable= Value variable
Char
Ch1
= Abc
// Ch1 is an Alias of Char Abc
#include<iostream.h>
Void main()
{
int a=1,b=2,c=3;
Int &z=a;
Cout<<“a=”<<a<<“b=”<<b<<“c
=”<<c<<“z=”<<z<<endl;
Z=b;
Cout<<“a=”<<a<<“b=”<<b<<“c
=”<<c<<“z=”<<z<<endl;
Z=c;
Cout<<“a=”<<a<<“b=”<<b<<“c
=”<<c<<“z=”<<z<<endl;
}
Cout<<“&a=”<<&a<<“&b=”<
<&b<<“&c=”<<“&z=”<<&
z<<endl;
}
Output:a=1 b=2 c=3 z=1
a=2 b=2 c=3 z=2
a=3 b=2 c=3 z=3
a=0xfff4 b=0xfff2 c=0xfff0
z=0xfff4
(Memory address of
variables)