Oops
Oops
Procedural-Oriented programming
Conventional programming , using high level languages such as COBOL ,FORTRAN & C, is known as procedure-oriented programming (POP). A problem is viewed as a sequence of things to be done such as reading , calculating & printing. Using procedural paradigm, a problem is decomposed into several subproblems and subsubproblems. In POP number of functions are written to accomplish the tasks such as reading, calculating and printing.
3
Fig. Typical structure of procedure-oriented program Structured top-down design: - First consideration: functions. - Secondary consideration: data structure.
Object-Oriented Paradigm
The real world is made of interacting, classifiable and identifiable objects. To model the real world more closely Object-Oriented (OO) Paradigm.
Object-Oriented Paradigm
OOP treats data as a critical element in the program development. It ties data more closely to the functions that operate on it. It allows decomposition of a program into a number of entities called objects and then builds data & functions around these objects.
9
11
13
14
1)
Object :
i) Object are the basic run-time entities in an object-oriented system. ii) They may represent a person, a place a bank account, a table of data or any item that the program has to handle. iii) Programming problem is analyzed in terms of objects and the nature of communication between them. iv) Objects take up space in the memory & have an associated address like structure . v) When a program executes, the object interacts by sending messages to one another. Ex. If there are two objects customer and account then the customer object may send a message to account object requesting for the bank balance.
15
16
Objects
17
18
Classes
19
20
22
Abstraction/ Encapsulation
Implementation details of the class are hidden. The access to the object is only through public methods. Methods: operations(functions) of a class.
23
Example: class Student { // class definition private: char id, name[20]; double balance, payrate; public: double TA_Salary() {/**/} void payFee(); }; //define method payFee of class Student void Student::payFee() {/* */} //create two Student objects Student student1, student2; salary1 = student1.TA_Salary(); //a message is sent to student1 student2.payFee(); //a message is sent to student2 Message: the name of a method.
24
Inheritance
One class can inherit data and methods from other more generic class. e.g. an apple is a kind of fruit; a graduate student is a kind of student. Goal:
To allow a class to be extended. To allow similar objects to share their common data and operations. To facilitates polymorphism and dynamic binding in C++.
A class inherits from more than one other classes multiple inheritance
25
26
Inheritance
27
Polymorphism
Original meaning: Having many forms. Polymorphism is the ability to invoke different methods for the same message.
28
Polymorphism
Fig. Polymorphism
29
Dynamic Binding
30
Message Passing
Oops consists of a set of objects that communicate with each other. Message passing involves the following steps : 1. Creating classes that define objects & their behaviour, 2. Creating objects from class definitions, & 3. Establishing communication among objects. A message for an object is a request for execution of a procedure , & 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) & the information to be sent. Example :
31
Benefits of OO Design
Increased productivity Enhanced reliability Improved maintenance Enhanced extensibility
32
Comparing Approaches
33
34
Introduction to C++
Developed by Bjarne Stroustrup in 1983 at the AT&T Bell lab. Derived primarily from two other languages: C and Simula67. It is a superset of C. C++ is a hybrid language since it supports both procedural and OO paradigm. C++ programs : Built from pieces called classes and functions C++ standard library : Rich collections of existing classes and functions
35
File type
Interface descriptions (header or include files) Implementation files of C Implementation files of C++ Interface description (template)
36
Compilation steps.
.cc
Compiler
.h, .tpl
.o Linker
libraries
a.out
37
39