Object Oriented Programming
Object Oriented Programming
Object Oriented Programming
3/8/2013
Dept of C.S.E
3/8/2013
Dept of C.S.E
3/8/2013
Dept of C.S.E
Inheritance
Polymorphism Dynamic binding Message passing
3/8/2013
Dept of C.S.E
Objects :
Objects are the basic run-time entities in an object oriented system. They may represent a place, a bank account, a table of data or any item that the program has to handle. When a program is executed, the objects interact by sending messages to one another. For example, if customer and account are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance. Each object contains data, and code to manipulate the data.
3/8/2013
Dept of C.S.E
Classes :
The entire set of data and code of an object can be made a user defined data type with the help of a class. Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. Classes are user defined data types and behave like the built-in types of a programming language. Internal data of a class member data Internal functions of a class member functions
3/8/2013
Dept of C.S.E
Data Encapsulation :
The wrapping of data and functions into a single unit (called class) is known as encapsulation. The data is not accessible to the outside world, and only those functions which wrapped in the class can access it. These functions provide the interface between the objects data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding.
3/8/2013
Dept of C.S.E
Data Abstraction :
This refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction. They encapsulate all the essential properties of the objects that are to be created.
3/8/2013
Dept of C.S.E
Inheritance :
Inheritance is the process by which objects of one class acquire the properties of another class. It supports the concept of hierarchical classification. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. (Base class and derived class)
The new class will have the combined features of both the classes.
3/8/2013
Dept of C.S.E
Polymorphism :
Polymorphism means the ability to take more than one form. The behavior depends upon the types of data used in the operation. This is something similar to a particular word having several different meanings depending on the context.
Shape Draw( )
Circle: object
Triangle:object Draw(triangle)
Draw(circle)
3/8/2013
Dept of C.S.E
10
Dynamic binding:
This means that the code associated with a given procedure call is not known until the time of the call at run time.
Message passing :
An object oriented program consists of a set of objects that communicate with each other.
The process of programming in an object oriented language, therefore, involves the following basic steps: 1.Creating classes that define objects and their behavior, 2.Creating objects from class definitions, and 3.Establishing communication among objects
3/8/2013 Dept of C.S.E 11
Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. A message for an object is a request for execution of a procedure, and therefore will invoke a function (procedure) in the receiving object that generates the desired result.
Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent.
Ex:- employee.salary(name);
3/8/2013
Dept of C.S.E
12
3/8/2013
Dept of C.S.E
13
int number;
float cost; public: void getdata(int a, float b); void putdata(void); };
3/8/2013
Dept of C.S.E
14
Creating Objects
item x,y,z; // x,y and z are objects of type item. The class specification provides only a template & does not create any memory space for the objects. The declaration of an object is similar to that of a variable of any basic type. The necessary memory space is allocated to an object at this stage.
3/8/2013
Dept of C.S.E
15
Ex:-
x.getdata(100,75.5);
This statements assigns 100 to number and 75.5 to cost of the object x. A member function can be invoked only by using an object. getdata(100,75.5); //illegal number (declared private) can be accessed only through a member function. x. number=100; // illegal
3/8/2013
Dept of C.S.E
16
The general form return-type class-name:: function-name (argument declaration) { Function body } The membership label class name :: tells the compiler that the function function-name belongs to the class class-name. The symbol :: is called the scope resolution operator. Member functions can also be defined inside the class definition.
3/8/2013
Dept of C.S.E
17
Program continued..
void main( ) { student s1; char name1[20]; int roll1; cout<<Enter name<<endl; cin>>name1; cout<<Enter roll no<<endl; cin>>roll1; s1.getdata(name1,roll1); s1.putdata( ); }
3/8/2013
Dept of C.S.E
19
void set::input( ) { cout<<Enter values for m and n<< endl; cin >> m >> n; } void set::display ( ) { cout<<largest value=<<largest( ); } void main( ) { set A; A.input( ); A.display( ); }
3/8/2013
Dept of C.S.E
21
void array::sort(void) { for(i=0;i<size-1;i++) for(j=i+1;j<size ;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]temp; } } void array :: display( ) { for(i=0; i<size; i++) cout<<a[i]<< ; cout<<endl; }
Dept of C.S.E 22
void main( )
{ array obj1; objl. setval( ); cout<<original array<<endl; objl.display( ); objl.sort( ); cout<<sorted array<< endl; objl.display( ); getch( ); }
3/8/2013
Dept of C.S.E
23
3/8/2013
Dept of C.S.E
24
Object oriented systems can be easily upgraded from small to large systems.
Software complexity can be easily managed.
3/8/2013
Dept of C.S.E
25