Download presentation
Presentation is loading. Please wait.
Published byraju K Modified over 5 years ago
1
Principles of object – oriented programming UNIT-1 Chapter-1
2
A look at Procedure Oriented Programming In POP the problem is viewed as the sequence of things to be done, such as: reading, calculating and printing Primary focus is on functions Problem is decomposed into Tasks
3
Typical structure of procedural oriented programs
4
A look at POP In POP problem/task is represented by instructions/actions. Instructions/actions are grouped into functions Flow charts are used to organize these actions Flow charts represent the flow of control from one action to another. Little attention is given to data used by functions
5
A look at POP In a multi-function program, important data items are placed as global may be accessed by all the functions. Each function may have its own local data Global data is vulnerable to an inadvertent change by a function If data structure is revised, we need to revise all functions that access the data. Also, procedural approach does not model real world problems very well Emphasis is on action, not data
6
Relationship of data and functions in POP
7
Characteristics of POP Emphasis is on doing things (algorithms). Large programs are divided into smaller programs known as functions. Most of the functions share global data. Data move openly around the system from function to function. Functions transform data from one form to another. Employs top-down approach in program design. divide the problem into tasks and then divide tasks into smaller sub-tasks and so on.
8
Object Oriented Paradigm OOP treats data as a critical element in the program development. Does not allow data to flow freely around the system. Ties data more closely to the function that operate on it, and protects it from accidental modification from outside function. Problem decomposed into a number of entities called objects and then builds data and function around these objects.
9
Organization of data and function in OOP The data of an object can be accessed only by the function associated with that object. However, function of one object can access the function of other objects
10
Features of OOP Emphasis is on data rather than procedure. Programs are divided into objects. Data structures characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Data is hidden and cannot be accessed by external function. Objects may communicate with each other through function. New data and functions can be easily added whenever necessary. Follows bottom up approach in program design. bottom level modules developed first
11
Definition of OOP An approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand
12
Basic Concepts of OOP Objects Classes Data abstraction and encapsulation Inheritance Polymorphism Dynamic binding Message passing
13
Objects Objects are basic run time entities in OO system An Object may represent a person, a place, a bank account, a table of data May also represent user-defined data such as vectors, time, and lists. Program objects match closely with the real-world objects.
14
Objects During pgm execution objects interact by sending messages to one another Customer object requesting for the bank balance with account object For interaction an object need to know the type of message accepted, and the type of response returned Code and data details are not needed Object contains data, and code to manipulate data.
15
Representing an object
16
Classes The entire set of data and code of an object can be made a user-defined data type using the class. objects are variables of the type class We can create any number of objects belonging to that class. A class is thus a collection of objects of similar types Eg. Mango, Apple and orange are members of class fruit
17
Data Abstraction and Encapsulation wrapping up of data and function into a single unit (called class) is known as encapsulation The data is not accessible to the outside world only those functions which are wrapped in the class can access it This feature is called data hiding or information hiding. Functions provide the interface between the object’s data and the program.
18
Data Abstraction and Encapsulation Abstraction refers to the act of representing essential features without including the background details or explanation. A class is defined as a list of abstract attributes such as size, wait, and cost, and function operate on these attributes. A class encapsulates all the essential properties of the object that are to be created. attributes aka data members as they hold information functions aka methods or member function
19
Inheritance It is the process by which objects of one class acquire the properties of objects of another classes. Supports hierarchical classification Each derived class shares common characteristics with the class from which it is derived Inheritance provides reusability (reuse a class) add additional features to an existing class without modifying it
20
Inheritance - Example
21
Polymorphism - operator overloading Polymorphism means the ability to take more than on form Based on the type of data used, an operation may exhibit different behavior in different instances Example: operation of addition. For two numbers, the operation will generate a sum. For two strings, the operation will produce a third string by concatenation. Process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
22
Polymorphism - function overloading Using a single function name to perform different type of task is known as function overloading A single function name can be used to handle different number and different types of argument. Allows objects having different internal structures to share the same external interface
23
Function overloading -example
24
Dynamic Binding Binding means linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time It is associated with polymorphism and inheritance. Example- procedure “draw” in previous fig. by inheritance, every object will have this procedure but its algorithm is unique to each object At run-time, the code matching the object under current reference will be called.
25
Message Passing In OOP, set of objects communicate with each other Objects communicate with one another by sending and receiving information A message for an object is a request for execution of a procedure Message invokes a function (procedure) in the receiving object
26
Message Passing Message passing involves specifying the name of object, the name of the function (message) and the information to be sent
27
Benefits of OOP Through inheritance, we can eliminate redundant code and extend the use of existing Classes. We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity. The principle of data hiding helps the programmer to build secure program that can not be invaded by code in other parts of a programs. It is possible to have multiple instances of an object to co-exist without any interference.
28
Benefits of OOP It is possible to map object in the problem domain to those in the program. It is easy to partition the work in a project based on objects. The data-centered design approach enables us to capture more details of a model in implementable form. Object-oriented system can be easily upgraded from small to large system. Message passing techniques for communication between objects makes to interface descriptions with external systems much simpler. Software complexity can be easily managed.
29
Object Oriented Languages Depending upon the features supported, the programs that support OOP concepts can be classified into the two categories: 1.Object-based programming languages support programming with objects do not support inheritance and dynamic binding Example : Ada programming language 2.Object-oriented programming languages. Object-based features + inheritance + dynamic binding Example : C++, Java
30
Applications of OOP Real-time system Simulation and modeling Object-oriented data bases Hypertext, Hypermedia, and expertext AI and expert systems Neural networks and parallel programming Decision support and office automation systems CAM/CAD systems
31
Beginning with C++ C++ is a compiled, object-oriented language C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell Labs. C++ is a superset of C
32
Application of C++ We can build special object-oriented libraries which can be used later by many programmers. C++ maps the real-world problems, C part of C++ gives the language the ability to get closed to the machine-level details. C++ programs are easily maintainable and expandable. new feature can be easily added using the existing structure of an object.
33
Simple C++ Program #include using namespace std; int main() { cout<<” c++ is better than c \n”; return 0; }
34
Simple C++ Program Like C, the C++ program is a collection of function. The above example contain only one function main() Program execution begins at main()
35
C++ comments Comments are explanatory notes; they are ignored by the compiler. There are two ways to include comments in a program : single line comment. // This is an example of // C++ program to illustrate // some of its features multiline comment /* This is an example of C++ program to illustrate some of its features */
36
Output operator cout<<”C++ is better than C.”; Above statement is an output statement cout represents the standard output stream (represents the screen) << is called the insertion or put to operator. cout is equivalent to printf()
37
C++ compiler directives The #include directive tells the compiler to include some already existing C++ code in your program. The included file is then linked with the program. There are two forms of #include statements: #include //for pre-defined files #include "my_lib.h" //for user-defined files Old versions of C++ use iostream.h iostream should be included at the beginning of all programs that use input/output statements.
38
Example for pre-defined files #include //used for sqrt() function #include //for output formatting utilities #include //for memory allocation etc #include //for string processing functions #include //for file input/output
39
Namespace Namespace defines a scope for the identifiers that are used in a program For using the identifier defined in the namespace scope we must include the following directive: using namespace std; std is the namespace where ANSI C++ standard class libraries are defined Above directive brings all the identifiers defined in the std namespace to the current global scope
40
Return Type of main() main() returns an integer value to the operating system. Every main() should end with a return(0) statement The default return type for all function in C++ is int Even if int is explicitly not specified
41
Average of two numbers #include // include header file using namespace std; int main() { float number1, number2,sum, average; cin >> number1; // Read Numbers cin >> number2; // from keyboard sum = number1 + number2; average = sum/2; cout << ”Sum = “ << sum << “\n”; cout << “Average = “ << average << “\n”; return 0; } //end of example The output would be: Enter two numbers: 6.5 7.5 Sum = 14 Average = 7
42
Variables float number1, number2,sum, average; Input Operator cin >> number1; The operator >> is known as extraction or get from operator cin is equivalent to scanf()
43
Cascading of I/O Operators Cout << “Sum = “ << sum << “\n”; Cout << “Sum = “ << sum << “,” << “Average = “ << average << “\n”; The output will be: Sum = 14, average = 7 Cin >> number1 >> number2;
44
Structure of C++ Program
45
Program is normally organized into three separate files. Class declarations are placed in a header file definitions of member functions go into another file. main program is placed in a third file It should include the previous two files
46
Structure of C++ Program The class definition including the member functions constitute the server Main program is known as client server that provides services to the main program The client uses the server through the public interface of the class.
47
Compile C++ Program In Linux, use the “cc” (uppercase) command to compile the program. CC example.C The compiler produces the executable file a.out
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.