Vous êtes sur la page 1sur 10

Difference between Procedure Oriented Programming

& Object Oriented Programming

POP 1. Emphasis on procedure i.e. doing things 2. Large programs are divided into smaller parts called functions 3. Most of the functions share global data. 4. Data move openly around the system from function to function. 5. Employs top-down approach. - Procedure oriented programming basically consists of writing a list of instructions for the computer to follow and organizing this instructions into groups known as functions. Here we concentrate more on the development of functions, very little attention is given to the data that are being used by various functions OOP 1. Emphasis is on data i.e. implementing things. 2. Programs are divided into what are known as class 3. Data is hidden and cannot be accessed by external functions. 4. Objects may communicate with each other through functions. 5. Employs bottom-up approach. : OOP allows us to decompose a problem into a number of entities called objects and then builds data and functions around these entities. Object Based Programming Abstraction and Encapsulation are e.g. Ada Object Oriented Programming Inheritance & Polymorphism are included e.g. Simula,Smalltalk80 Basic Principles of OOP

Class Object Data Abstraction Data Encapsulation Polymorphism Inheritance Message Passing 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). 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 / state int rollno; public : void getData(); // methods / behavior void dispData(); }; void main() { student s1; s1.getData(); s1.dispData(); } void student :: getData () {

// Declaring an object // Calling public method of a class

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 objects 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 :
One thing having more than one forms. e.g. 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 1. Design/Compile time Polymorphism / Static Binding / Early Binding

Two type of Compile Time polymorphism a) Function Overloading b) Operator Overloading 2. Runtime Polymorphism / e.g. Virtual Functions Dynamic binding / Late Binding

1. Design/Compile time Polymorphism / Static Binding / Early Binding:


It refers to events that occur at compile time. Early binding occurs when all information needed to call a function is known at compile time means that an object and function call are bound during compilation.

Overloading :
A language feature that allows a function or operator to be given more than one definition

a) Function Overloading:
is the process of using same name for two or more functions name having different types of parameter or a different number of parameter. It is only through these differences that the compiler knows what function to call in any situation.

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 e.g: void display(int x) // display() is overloaded int display(int x,int y)

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

2)

Runtime Polymorphisam:

It refers to function calls which 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 functions 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 base pointer.
e.g.
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. e.g. 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 ; s->draw(); }

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.

TYPES OF INHERITANCE 1. Single inheritance : e.g. class B is derived from class A 2. Multiple Inheritance : e.g. class C is derived from class A & B 3. Hierarchical Inheritance : e.g. class B, C,D is derived from class A 4. Multilevel Inheritance: e.g. class B is derived from class A and class C derived from B 5. Hybrid Inheritance e.g. class B & C is derived from class A and class D is derived from B & C

Member variables and their Access Modifiers Same class Derived class Outside class private Is visible Not visible Not visible Access of Inheritance private
Private Protected Public Not inherited Not inherited Not inherited

protected Is visible Is visible Not visible

public Is visible Is visible Is visible

protected
private protected Protected

public
private protected public

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. e.g Television , car etc.

Association:
In association both the whole object as well as the parts can be represented. e.g Computer, stereo system etc

Composition and Inheritance


Composition always represents Has-a Relationship and Inheritance always represents Is-a Relationship. e.g: car has a steering------Composition e.g.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 dont provide any constructor than a default constructor is provided by the complier.But if we provide a parameterized constructor then 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. Virtual Base Class: A base class that has been qualified as virtual in the inheritance definition. In multiple inheritance, a derived class can inherit the members of a base class via two or more inheritance paths. If the base class is not virtual , the derived class will inherit more than one copy of the members of the base class. For a virtual base class,however only one copy of its members will be inherited regardless of the numbers of inheritance paths between the base class and the derived class.

Vous aimerez peut-être aussi