Object Oriented Programming

Procedural Concept

The main program coordinates calls to procedures and hands over appropriate data as parameters.

Object-Oriented Concept

Objects of the program interact by sending messages to each other

Object: any object in real world or an instance of a class in a program Object oriented: languages and programming techniques based on objects (classes) instead of procedures or functions .

Modularity : each object has a clear semantic (Employer or Drawing Device). Reutilisability: .CMR INSTITUTE OF TECHNOLOGY Object Oriented Programming Goals:       Reduce conceptual load (minimize amount of detail programmer must think at one time) Provide fault containment Provide independence between program components(modify internal implementation without changing external code or vice versa. Less bugs : the data are accessed through the methods and you can use them only the way to object¶s creator wants you to.

Object Oriented Programming Key Features ‡ Data Abstraction ‡ Polymorphism ‡ Inheritance ‡ Encapsulation ‡ Modularity ‡ Hierarchy .


Procedural Approach cont d Focus is on procedures All data is shared: no protection More difficult to modify Hard to manage complexity .


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

account . transfer Customer.Procedural vs. deposit. Object-Oriented Procedural Object Oriented Withdraw. money.

}.float amount).Procedural Approach bool MakeDeposit(int accountNum. float Withdraw(int accountNum. int accountNum. struct Account { char *name. . float balance. char accountType.float amount).

A View of the Two paradigms .

Mapping the world to software Objects in the problem domain are mapped to objects in software .

Object Oriented Data and operations are grouped together Account Withdraw Deposit Transfer Interface: Set of available operations .

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

object class girl Jodie Daria Jane Brittany .Objects and Classes Classes reflect concepts. objects reflect instances that embody those concepts.

Objects and Classes cont d Class BankAccount Balance InterestYTD Owner Account_number Operations MakeDesposit Transfer WithDraw GetBalance Balance 500 InterestYTD Owner Account_number Balance 10.000 InterestYTD Owner Account_number .

balance=500.Instantiation An Object is instantiated from a Class BankAccount myAccount. myAccount. .

Objects and Classes Class  Object    Visible in source code The code is not duplicated   Own copy of data Active in running program Occupies memory Has the set of operations given in the class .

Classification Animal Mammal Rodent Primate Cats Reptile Mouse Squirel Rabbit .

Classification Account Checking Account Savings Account Value First Select Access First Interest .

Structure of a C++ Program Include files Class Declaration Member Functions Definition Main function program .

int l) { width = w.5). public: Rectangle(int w. length = l. } } main() { Rectangle rect(3. } int area() { return width*length. length. cout<<rect. } .area()<<endl.Example Code class Rectangle { private: int width.

}. class Triangle { private: int a. // The area of a circle int area(). // The area of a triangle int area().Encapsulation class Circle { private: int radius public: Circle(int radius). public: Triangle (int a. int c). . c. int b. }. b.

end .

Oil change? Price? back . length of warranty.Abstraction Extract the relevant object properties while ignoring inessentials  Defines a view of the object Example .car  Car dealer views a car from selling features standpoint  Price. type of spark plugs. color.  Mechanic views a car from systems maintenance standpoint  Size of the oil filter.

Polymorphism One interface Multiple implementations Inheritance Method overloading back .

The sub-class inherits the base class data members and member functions back .Inheritance A class which is a subtype of a more general class is said to be inherited from it.

Encapsulation Steps  Decompose an object into parts  Hide and protect essential information  Supply interface that allows information to be modified in a controlled and useful manner Internal representation can be changed without affecting other system parts Example .car radio  Interface consists of controls and power and antenna connectors  The details of how it works is hidden back  To install and use a radio  Do not need to know anything about the radio s electronics .

Automobile can be decomposed into subsystems  Cooling system  Radiator Ignition system  Battery Thermostat Water pump  Starter Spark plugs back .Modularity Dividing an object into smaller pieces or modules so that the object is easier to understand and manipulate Most complex systems are modular Example .

Hierarchy Hierarchy  Ranking or ordering of objects based on some relationship between them Help us understand complex systems  Example . a useful way of ordering similar abstractions is a taxonomy from least general to most general back .a company hierarchy helps employees understand the company and their positions within it For complex systems.