0% found this document useful (0 votes)
98 views36 pages

C Presentation

1. Procedure oriented programming focuses on procedures or functions, while object oriented programming focuses on data through classes and objects. 2. In OOP, programs are divided into classes which contain both data and methods, while POP divides programs into smaller functions that share global data. 3. OOP supports concepts like abstraction, encapsulation, inheritance and polymorphism which are not present in POP.

Uploaded by

AVI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views36 pages

C Presentation

1. Procedure oriented programming focuses on procedures or functions, while object oriented programming focuses on data through classes and objects. 2. In OOP, programs are divided into classes which contain both data and methods, while POP divides programs into smaller functions that share global data. 3. OOP supports concepts like abstraction, encapsulation, inheritance and polymorphism which are not present in POP.

Uploaded by

AVI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Difference between

Procedure Oriented Programming


And
Object Oriented Programming
POP OOP
• Emphasis on • Emphasis is on data
procedure I,e doing i.e. implementing
things things.
• Large programs are • Programs are divided
divided into smaller into what are known
parts called functions as class
• Most of the functions • Data is hidden and
share global data. cannot be accessed
• Data move openly by external functions.
around the system • Objects may
from function to communicate with
function. each other through
• Employs top-down functions.
approach. • Employs bottom-up
approach.
Object Based Programming
Abstraction and Encapsulation are

Eg. Ada
Object Oriented Programming
Inheritance & Polymorphism are included

eg. Simula,Smalltalk80
Class
Classes are called as the building blocks in the OOPs programs
Class are equivalent to the user defined functions in C.
A class contains variables which are called as Attributes and
Functions which are called as Methods
The default access of the class members is private.
Classes are standalone components and can be distributed
individually or as part of a library.
In short , a class is a blueprint for an object
Object
An entity that can store data and send and receive
message. An instance of a class.
A class is used to create an object
Each Object has its own attributes ( fields) and behavior
(Methods) are shared .
The data stored within an object represents the state of
the object. This data is called Attributes.
The Behavior of an object is what the object can do.This
is called Methods
A class must be defined before creating an object
Example
public class student
{
private: // Access Modifier
char name[10]; // attributes
int rollno;
public :
void getData(); // methods
void dispData();
};
void main()
{
student s1; // Declaring an object
s1.getData(); // Calling public method of a class
s1.dispData();
}
Example:
void student :: getData ()
{
printf(“Enter Student Name: “);
gets(name);
printf(“Enter Student RollNo:”);
scanf(“%d”, &rollno);
}
void student :: dispData ()
{
printf(“/nStudent Name : %s “, name);
printf(“/nStudent RollNo : %d”, rollno);
}
Abstraction:
The act of representing the essential features of something
without including much background details or explanations
Data Abstraction:
Classes use the concept of abstraction and are defined as a list
of abstract attributes such as size,weight and cost , and
functions to operate on these attributes. They encapsulate all
the essential properties of the objects that are to be created.
Since the classes use the concept of Data Abstraction,they are
known as Abstract Data Types(ADT)
Data Encapsulation:
The wrapping up of the data and functions into a single
unit called class is known as encapsulation.
Data cannot be accessed from outside directly without
function.
The functions provide interface between the object’s data
and program.
By default all class members are having private access
modifier. One can specify protected or public access
modifier to access the data .
In this way Data Hiding (Information Hiding) can be
achieved.
Polymorphism:
Polymorphism means the ability to take
more than one forms.
Eg:
12 + 15 = 27
Poly + morphism = Polymorphism.
Thus polymorphism plays an important role in
allowing objects having different internal
structures to share the same external interface
Types of Polymorphism
Polymorphism

Design time Polymorphism Run Time Polymorphism


OR Or
Static/Early Dynamic/Late
Binding Binding

Function Operator Virtual


overloading overloading Functions
DESIGN TIME POLYMORPHISM :
Also called Early Binding or Compile Time polymorphism
e.g: Operator Overloading and Function Overloading

RUNTIME POLYMORPHISM:
Also called Late Binding or Dynamic Binding
e.g: Virtual Functions
Overloading :
A language feature that allows a function or operator to be given
more than one definition

Function Overloading:
Same Name different argument list. It can be in the same class or in
the derived class.Return type can or cannot be same.
Which function will be called is decided at compile time by seeing
the types of arguments, as it is called as Compile time polymorphism
Eg: void display(int x) // display() is overloaded
int display(int x,int y)
Operator Overloading:
Operator overloading allows you to change the meaning of an
operator.
For e.g. most people, when they see a plus sign(+), assume it
represents addition of numeric data. However,there are times ,
when a plus sign represent something else. In the context of
strings, the plus sign does not mean addition of integers or floats,
but Concatenation of strings. It means C++ tries to make the user
defined data type behave in much of the same way as the built in
types. The mechanism of giving such special meanings to an
operator is called Operator overloading.
e.g char *s1=“hello”; char *s2=“ world”;
s1+s2
Operator Overloading:

Operators that cannot be overloaded are:

sizeof - Sizeof operator


. - Membership operator
:: - Scope resolution operator
?: - Conditional operator
.* - Pointer to member function
Runtime Polymorphisam:
The address of the functions are determined at run time rather than compile
time. Virtual functions are used to achieve late binding.
Virtual function : Is a member function that is declared within a base class and
redefined by a derived class. To create a virtual function.precede the function’s
declaration in the base class with virtual keyword.. When a class containing virtual
function is inherited, the derived class redefines the virtual function to fit its own
need..
In essence, virtual functions implement the “one interface,multiple methods”.
When accessed “normally” virtual functions behave just like any other type of class
member function. However when it is accessed via a base class pointer it supports
runtime polymorphism. When a base pointer points to a derived object that contains
a virtual function,C++ determines, which version of function call based upon the
type of object pointed to by the pointer.
class Base
{
public:

void display()
{
printf(“ Display Base”);
}

virtual void show()


{
printf(“ show Base”);

}
};

class Derived : public base


{
public:
void display()
{
printf(“ Display Base”);
}

void show()
{
printf(“ show Base”);

}
};
void main()
{
Base B;
Derived D
Base *bptr;

printf(“ bptr points to base”);


bptr=&B;
bptr -> display(); // calls base version
bptr -> show(); // calls base version

printf(“ bptr points to derived”);


bptr=&D;
bptr -> display(); // calls base version
bptr -> show(); // calls derive version
}
Pure Virtual Functions:
is a virtual function that has no definition in the base class
The function which looks as follows:
virtual returntype functionname () = 0
The classes which contain such functions are called as
Abstract Classes.The abstract classes cannot be instantiated
i.e. its object cannot be created,you can create pointers and
references to select the proper virtual function
It becomes mandatory for all the derived classes to give
implementation to this function. Hence a contract relation is
established in this inheritance.
class Shape{
virtual void draw() { }=0;; // pure virtual function declaration I.e. behavior
}
class Circle : public Shape
{
void draw() { }
}
class Rectangle : public Shape
{
virtual void draw() { }
}
void main()
{
Shape *s;
Circle cobj1;
Rectangle robj1;
s=&cobj1;
s->draw();
s=&robj1 ;
Inheritance:
A relationship between classes such that some of the
members of the one class(Base/Super class) can be inherited
by the another class(Derived/Subclass class). By default
class inherited privately
This supports the idea of classification(extensibility) and
reusability. This means that we can add additional features to
an existing class without modifying it and even use the
existing features.
shape

circle triangle
Eg :
square
TYPES OF INHERITANCE
SINGLE INHERITANCE
Bderived from A
MULTIPLE INHERITANCE
Cderived from A & B
HIERARCHICAL INHERITANCE
B, C,D derived from A
MULTILEVEL INHERITANCE
B  derived from A and C derived from B
HYBRID INHERITANCE
B & C derived from A & D derived from
B&C
Members variables and their acess:

Private Protected Public

Same Is Is visible Is
class visible visible
Derived Not Is visible Is
Class visible visible
Outside Not Not visible Is
Class visible visible
Access of members

Way Private Protected Public


Of
Private not Privately Privately
Inheritance accessib
le
Protected Not Protected Protected
accessib
le
Public Not Protected Public
accessib
le
Composition :
When an object is build of one or more different objects the
such a relationship is called Composition.
Composition is of two types:

a. Aggregation
b. Association
Aggregation:
Aggregation means that a complex object is composed of other objects
where you normally see the object as a whole and are unable to see the
internal other objects.
Eg Television , car etc.

Association:
In association both the whole object as well as the parts can be
represented.
Eg Computer,stereo system etc.
Composition and Inheritance
Composition always represents Has-a Relationship and Inheritance
always represents Is-a Relationship.
Eg: car has a steering------Composition
Eg:student is a sportsman.----Inheritance
Message Passing:
The phenomenon of one object communicating with other is
called Message Passing.
e.g. public class Payroll{
char name[10];
public:
void getData();
};
void main()
{ Payroll p;
p.getData();
}
void Payroll : getData ()
{ Person p;
p.setname(“Joe”); // Payroll object is sending message to Person object
}
Mutators :
Functions which changes or set the attributes are called Mutators

Insepectors :
Functions which determine the attributes of an objects are called
Inspectors

Facilitators :
Functions that direct an Object to perform some functions or options
are called facilitators.
Constructors and Destructors
Constructors : are special functions written in any class.A
constructor does not have any return type as well as its name is same
as that of the class name. It is implicitly called always when an object
of a class is created. We can initialize all the attributes of the class in
the constructor.
Constructors may or may not accept parameters.
If we don’t provide any constructor than a default constructor is
provided by the complier.But if we provide a parameterized
constructor than one default constructor should also be provided.
In case of inheritance the base class constructor is called first then the
derived class constructor is called.
Destructor:
Destructors is the special function similar to a constructor .
Destructor is also having the signature same as constructor the
only difference is it is preceded by a tild (~).
A destructor is implicitly called when the object moves out of
scope.
When memory is allocated dynamically then we have to call
destructor explicitly.
Incase of Inheritance the destructor of derived class is called
first and then the destructor of the base class.
public class employee{
private: // Access Modifier
char ename[10]; // attributes
int empno;
public :
employee(); // Constructor
~employee() ; // Destructor
void setData(); // Mutators
void getData(); // Inspectors
void dispData(); // Facilitators
};
void main()
{ employee e1;
e1.setData();
e1.getData();
}
employee : employee() // Constructor
{
printf(“Constructor is called”);
}
employee :: ~employee() // Destructor
{
printf(“\nDesstructor is called”);
}
void employee :: setData() // Mutators
{ static num=1;
empno = num + 1;
}
void employee :: getData() // Inspectors
{
printf(“Enter your name:”);
gets(ename );
}
void employee :: dispData() // Facilitators
{
printf(“\n Employee name: %s”, ename);
printf(“\n Employee name: %d”, empno);
}
Friend Functions:
The functions which can access the private data members of a
class are called as friend functions of that class. A friend function
can be declared using a keyword friend.
Friend functions do not get inherited, i.e. a friend of a base class
need not be the friend of the derived class until we explicitly
declare it as a friend of the derived class.

You might also like