Sie sind auf Seite 1von 25

OBJECT ORIENTED PROGRAMMING

By
MD KAMRAN AZAM MD MAZHAAR NOOR MD GHURFAN KHAN MUNDRIKA PRASAD NEERAJ DUBEY NITESH KUMAR [100101128] [100101130] [100101132] [100101135] [100101140] [100101149]

INTRODUCTION
Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.

USE IN REAL WORLD -- PROBLEM DESCRIPTION


customers are allowed to have different types of bank accounts, deposit money, withdraw money and transfer money between accounts

PROCEDURAL APPROACH
bool MakeDeposit(int accountNum,float amount); float Withdraw(int accountNum,float amount); struct Account { char *name; int accountNum; float balance; char accountType; }; Focus is on procedures All data is shared: no protection More difficult to modify Hard to manage complexity

OBJECT ORIENTED APPROACH


class Account { public: float withdraw(); void deposit(float amount); private: float balance;

);
Protection Consistency Allows change

MAPPING THE WORLD TO SOFTWARE


Objects in the problem domain are mapped to objects in software

Procedural vs. Object-Oriented Programming


The unit in procedural programming is function, and unit in object-oriented programming is class Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them. Procedural programming separates the data of the program from the operations that manipulate the data, while objectoriented programming focus on both of them

Procedural

Object Oriented

OBJECTS AND CLASSES


OBJECTS:
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.

DECLARATION OF AN OBJECT
class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; main() { Rectangle r1; Rectangle r2; r1.set(5, 8); cout<<r1.area()<<end l; r2.set(8,10); cout<<r2.area()<<end l; }

CLASSES:
A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations. public class Student { }

DEFINE A CLASS TYPE


Header

class class_name { permission_label: member; Body permission_label: member; ... };

class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); };

TYPES OF CLASS
Information hiding
To prevent the internal representation from direct access from outside the class.

Access Specifiers

public may be accessible from anywhere within a program private may be accessed only by the member functions, and friends of this class protected acts as public for derived classes behaves as private for the rest of the program

ENCAPSULATION
[OR INFORMATION HIDING]
The encapsulation is the inclusion within a program object of all the resources need for the object to function - basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties.

ASSOCIATION
Association is a (*a*) relationship between two classes. It allows one object instance to cause another to perform an action on its behalf. Association is the more general term that define the relationship between two classes, where as the aggregation and composition are relatively special. public class StudentRegistrar { public StudentRegistrar (); { new RecordManager().Initialize(); } }

EXAMPLE OF AN ASSOCIATION
In this case we can say that there is an association between StudentRegistrar and RecordManager or there is a directional association from StudentRegistrar to RecordManager or StudentRegistrar use a (*Use*)RecordManager. Since a direction is explicitly specified, in this case the controller class is the StudentRegistrar.

INHERITANCE
Programs can be built in hierarchical structure from data abstractions that depend on other data abstractions (Components) The object style of data abstraction is the default, not the ADT style Object-oriented programming (inheritance) is based on the idea that data abstractions have much in common Example, sequences (stacks, lists, queues) Object oriented programming builds data abstractions incrementally, this is done by inheritance A data abstraction can be defined to inherit from another abstract datatype, have substantially the same functionality of the other abstract datatype

EXAMPLE OF INHERITANCE
Ability of a new class to be created, from an existing class by extending it, is called inheritance. public class Exception { }

public class IOException : Exception { }


According to the above example the new class (IOException), which is called the derived class or subclass, inherits the members of an existing class (Exception), which is called the base class or superclass. The classIOException can extend the functionality of the class Exception by adding new types and methods and by overriding existing ones.

POLYMORPHISMS
Polymorphisms is a generic term that means 'many shapes'. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things. In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding,

Method Overloading
The method overloading is the ability to define several methods all with the same name.
public class MyLogger { public void LogError(Exception e) { // Implementation goes here } public bool LogError(Exception e, string message) { // Implementation goes here } }

OPERATOR OVERLOADING
The operator overloading (less commonly known as ad-hoc polymorphisms) is a specific case of polymorphisms in which some or all of operators like +, - or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments.

METHOD OVERRIDING
Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its superclasses. A subclass can give its own definition of methods but need to have the same signature as the method in its super-class. This means that when overriding a method the subclass's method has to have the same name and parameter list as the super-class's overridden method.

EXCEPTIONS AND EXCEPTIONS CLASS


Java (like its cousin, C++) provides a neater, more structured alternative method for dealing with errors that can occur while a program is running. The method is referred to as exception handling. The word exception is meant to be more general than error. It includes any circumstance that arises as the program is executed which is meant to be treated as an exception to the normal ow of control of the program. An exception might be an error, or it might just be a special case that you would rather not have clutter up your elegant algorithm.

TYPES OF EXCEPTION HANDLER


The try Statement
Part of a code that can cause an exceptions.

The throw Statement

The catch Statement


Part of the code that can handle an exceptions.

IMPLEMENTATION OF AN EXCEPTION HANDLER


main() { int a,b; cout<<"Enter two vaues"; cin>>a>>b; try { int z = a/b; } cout<<"Vslue of z is"<<z; catch(Exception e){ cout<<"Divide by zero error"; } }

SUMMARY
Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships