Вы находитесь на странице: 1из 21

Object Oriented Programming

Date: 27/01/11

Few Points
Why do we need OOPs ? What does it do that traditional languages like C, BASIC and PASCAL dont ? What are the principle behind the OOP? What does these two terms - OBJECT - CLASS mean ? What is the relationship between C++ and older C language?

Why do we need OOPs ?


To overcome the Limitations of Procedural programming Important Problem with Procedural programming : 1. Unrestricted Access of Functions to Global Data 2. Unrelated functions and data, the basis of procedural programming, provide a poor model of real world. 3. Difficulty to create new data type.

Unrestricted Access
Two kinds of Data in Procedural Programming 1. Local Data 2. Global Data

Local Data
The data that are defined inside the function and accessed by its function only. Function1 Local Data 1

Global Data
Global Data Global Data

Function1 Local Data 1

Function 2 Local Data 2

Function 3 Local Data 3

Problems with Large Program


. This leads to even large number of potential connection between functions and data. This large number of connections cause several problem : - Difficult to conceptualize program structure - Difficult to modify the program(A change made in global data items may result in rewriting all functions that access it. ) It is very difficult to identify what data is used by which function.

Main Program

Function- 1

Function- 2

Function- 3

Function- 4

Function- 5

Function- 6

Function- 7

Function- 8

Real World Modeling


The second and most important problem with the procedural programming is that the separate arrangements of data and function does a poor modeling of real world problem . In Physical world we deal with objects such as people and cars. Such objects arent like data and they arent like functions. Complex real world objects have both attributes and behavior.

Attributes
Also called Characteristic. Examples of attributes for People is eye-color, job-title and for Car horsepower and no of doors. Data is the certain specific value of the attributes such as blue ( for eye color ) and four ( for no of doors ).

Behavior
Behavior is something a real-world object does in response to some stimulus. For example : If you ask your boss for raise, he will generally say yes or no. If you apply breaks in car, it will generally stop. Saying something and stopping are examples of behavior. Thus behavior is like a function. So neither data nor functions, by themselves model a real world object effectively.

New Data Type


Being able to create your own Data Type is Called Extensibility. Procedural programming are not usually extensible. But if we want to work with complex numbers, two dimensional coordinates or dates, then built-in data types are not able to handle such complex data. Without unnatural convolution we cant bundle both x and y coordinates together into a single variable point and then add and subtract values of this type. This results that procedural program are more complex to write and maintain.

Data Type
1. Primitive Data type : Int , Float, Char int x ; float y ; char z ; 2. Array : for storing more than one data of same type. int x[10] ; 3. Structure : for storing more than one data of different type. struct node { int x ; int y ; }; struct node n ; n.x n.y

Contd..
Class node { int x; int y; display() { x; y; } }; node n ; n.x ; n.y ; n.display() ;

Class
class car { private: int carno; public: insert() { cin >> carno ; } display () { cout << carno; } }; main() { Data Member Member Function car c; c.carnoAccess specifiers = 5; (security c.insert(); of members ) c.display(); private } public Class car { int carno ; }
Default private

Encapsulation

Memory Allocation
class car { int carno ; int carmodel ; int no of wheel ; int no of gate ; int no of seat ; carno carmodel displaycarno() { cout << carno ; } displaynoofwheel() { cout << no of wheel ; } insert () { cin >> carno >> carmodel >> no of wheel >> no of gate >> no of seats ; } };

main() { car maruti ; maruti.insert();gate wheel car santro; santro.insert(); car tata ; tata.insert();. maruti.displaycarno(); . . }

seat

Initialization of Data in Class


Class car { private : int x, y ; public : car () { x = 10; y = 20; } car (int i ) { x=i; y = i + 50; } car (int i, int j ) { x=i; y=j; } }; 10 20

main () { car c ; car m(70); car CONSTRUCTOR k( 5, 10); . . Default Constructor . . } Parameterized Constructor

Class car { private : int x ; int y ; public : void display () ; void display (int x) ; }; Void car : : display ()

Scope Resolution & Overloaded Operator


main() { car C1, C2, C3 ; C1 = C2 + C3 ; . } Overloaded
Operator Overloaded Function
C1.x = C2.x + C3.x C1.y = C2.y + C3.y

{ cout << x << y ;

Polymorphism
Two type: 1. Static 2. Dynamic Static : a) Operator Overloading b) Function Overloading Dynamic : Virtual Function

Inheritance
POINT

LINE

POLYGONE

SQUARE

RECTANGLE

HEXAGONE

Thats all for today.. Thanks !

Вам также может понравиться