31
Object-Oriented Programming Objects can be used effectively to represent real-world entities For instance, an object might represent a particular employee in a company Each employee object handles the processing and data management related to that employee 04/27/15 MCA-114 2

implementing oop_concept

Embed Size (px)

Citation preview

Page 1: implementing oop_concept

Object-Oriented Programming

• Objects can be used effectively to represent real-world entities

• For instance, an object might represent a particular employee in a company

• Each employee object handles the processing and data management related to that employee

04/27/15 MCA-114 2

Page 2: implementing oop_concept

Objects• An object has:

– state - descriptive characteristics

– behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its account number and its current balance

• The behaviors associated with a bank account include the ability to make deposits and withdrawals

• Note that the behavior of an object might change its state

04/27/15 MCA-114 3

Page 3: implementing oop_concept

Classes• An object is defined by a class

• A class is the blueprint of an object

• Multiple objects can be created from the same class

04/27/15 MCA-114 4

Page 4: implementing oop_concept

Objects and Classes

04/27/15 MCA-114 5

Bank Account

A class(the concept)

John’s Bank AccountBalance: $5,257

An object(the realization)

Bill’s Bank AccountBalance: $1,245,069

Mary’s Bank AccountBalance: $16,833

Multiple objectsfrom the same class

Page 5: implementing oop_concept

AttributesContain current state of an object.

• Attributes can be classified as simple or complex. • Simple attribute can be a primitive type such as

integer, string, etc., which takes on literal values.• Complex attribute can contain collections and/or

references. • Reference attribute represents relationship. • complex object: contains one or more complex

attributes

04/27/15 MCA-114 6

Page 6: implementing oop_concept

Methods and messages

Method: Defines behavior of an object, as a set of encapsulated functions.

Message: Request from one object to another asking second object to execute one of its methods.

04/27/15 MCA-114 7

(a)

(b)

(a) Object showing attributes and methods(b) Example of a method

Page 7: implementing oop_concept

Classes

Class: Blueprint for defining a set of similar objects.

Objects in a class are called instances.

04/27/15 MCA-114 8

Page 8: implementing oop_concept

Data Encapsulation and Abstraction

• The wrapping up of data and functions into a single unit is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program. This insulation of the data form direct access by the program is called data hiding or information hiding.

• Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member function. Since the classes use the concept of data abstraction, they are knows as Abstract Data Types (ADT).

04/27/15 MCA-114 9

Page 9: implementing oop_concept

Inheritance• One class can be used to derive another via

inheritance

• Classes can be organized into hierarchies

04/27/15 MCA-114 10

Bank Account

Account

Charge Account

Savings Account

Checking Account

Page 10: implementing oop_concept

Inheritance (contd)Inheritance allows one class of objects to be defined as a special case

of a more general class.

Special cases are subclasses and more general cases are superclasses.

04/27/15 MCA-114 11

Generalization: process of forming a superclass Specialization: forming a subclass

•Subclass inherits all properties of its superclass and can define its own unique properties. •Subclass can redefine inherited methods. •All instances of subclass are instances of superclass. •Principle of substitutability: instance of subclass can be used whenever method/construct expects instance of superclass.•A KIND OF (AKO): Name for relationship between subclass and superclass

Page 11: implementing oop_concept

Types of inheritance

04/27/15 MCA-114 12

(a)(b)

(c)

(a) Single(b) Multiple(c) Repeated

(b)

Page 12: implementing oop_concept

Inheritance (contd)• Define humanBeing to be a class– A humanBeing has attributes, such as age, height,

gender– Assign values to attributes when describing object

• Define Parent to be a subclass of HumanBeing – A Parent has all attributes of a HumanBeing, plus

attributes of his/her own (name of oldest child, number of children)

– A Parent inherits all attributes of humanBeing• The property of inheritance is an essential feature of

object-oriented languages such as Smalltalk, C++, Ada 95, Java (but not C, FORTRAN)

04/27/15 MCA-114 13

Page 13: implementing oop_concept

Inheritance (contd)

• UML notation– Inheritance is represented by a large open triangle

04/27/15 MCA-114 14

Page 14: implementing oop_concept

Overriding and OverloadingOverriding: Process of redefining a property within a subclass.

Overloading: Allows name of a method to be reused with a class or across classes.

04/27/15 MCA-114 15

Overriding Example:Might define method in Staff class to increment salary based on commission

method void giveCommission(float branchProfit) {

salary = salary + 0.02 * branchProfit; }

May wish to perform different calculation for commission in Manager subclass:

method void giveCommission(float branchProfit) {

salary = salary + 0.05 * branchProfit; }

Page 15: implementing oop_concept

Aggregation

• UML Notation

04/27/15 MCA-114 16

Page 16: implementing oop_concept

Association

• UML Notation

04/27/15 MCA-114 17

Page 17: implementing oop_concept

Polymorphism and Dynamic Binding

• Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in difference instances. The behaviors depends upon the types of data used in the operation. For example, consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings , the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

04/27/15 MCA-114 18

Page 18: implementing oop_concept

Polymorphism and Dynamic Binding (contd)

04/27/15 MCA-114 19

Shape

Draw()

Triangle object

Draw()

Box

Draw()

CircleDraw()

Page 19: implementing oop_concept

Dynamic Binding• Binding refers to the 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 now knows until the time of the call at run-time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of that reference.

• Consider the procedure “draw”, every object will have this procedure. Its algorithm is, however, unique to each object and so the draw procedure will be redefined in each class that defines the object. At run-time, the code matching the object under current reference will be called.

04/27/15 MCA-114 20

Page 20: implementing oop_concept

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.

• Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model their real-world counterparts.

• Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent. Example:-

• employee.salary(name)• Object message information

04/27/15 MCA-114 21

Page 21: implementing oop_concept

Default Parameter Value• C++ allows us to call a function without specifying all its arguments. In such

cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. Default values are specified when the function is declared. The compiler looks at the prototype to see how many arguments a function uses and alerts the program for possible default values.

• A default argument is checking for type at the time of declaration and evaluated at the time of call. Once important point to note is that only the trailing arguments can have default values and therefore we must add defaults form right to left. We cannot provide a default value to a particular argument in the middle of an argument list.

• Default arguments are useful in situations where some arguments always have the same value.

04/27/15 MCA-114 22

Page 22: implementing oop_concept

int main()

{

float amount;

float value(float p, int n, float r=0.15);

amount=value(5000.00,5);

cout<<amount;

return 0;

}

float value(float p, int n, float r)

{

}

04/27/15 MCA-114 23

Default Parameter ValueDefault Parameter Value

Page 23: implementing oop_concept

Using Reference variables with Functions

• Provision of the reference variables in C++ permits us to pass parameters to the functions by reference. When we pass arguments by reference, the “formal” arguments in the called function become aliases to the “actual” arguments in the calling function. This means that when the function is working with its own arguments, it is actually working on the original data.

• In C , this is accomplished using pointers .

void swap(int &a, int &b)

{

int t=a;

a=b;

b=t;

}

swap(m,n);

04/27/15 MCA-114 24

Page 24: implementing oop_concept

04/27/15 MCA-114 25

Debugging Exercises

1. What will happen when you execute the following code?

#include<iostream> int main() { int i=0; i=400*400/400; cout<<i;return 0;}2. Identify the error in the following program

#incude<iostream> int main(){ int num[]={1,2,3,4,5,6}; num[1]==[1]num ?cout<<“Success”:cout<<“Error”;return 0;}

Page 25: implementing oop_concept

04/27/15 MCA-114 26

Debugging Exercises

3. Identify the errors in the following program.#include<iostream> int main(){ int i=5; while(1){ Switch(i ) { default: case 4: case 5: break; case 1: continue; case 2: case 3: break; } i--;} }

Page 26: implementing oop_concept

04/27/15 MCA-114 27

Debugging Exercises

4. Identify the error in the following program.

#include<iostream>#define pi 3.14int squareArea(int &);int circleArea(int &);

int main(){ int a=10; cout<<squareArea(a)<<“ “; cout<<CircleArea(a)<<“ “; cout<<a<<endl;} int squareArea(int &a) { return a*==a;}

int circleArea(int &r){return r=pi*r*r;}

Page 27: implementing oop_concept

04/27/15 MCA-114 28

Debugging Exercises

5. Identify the error in the following program.#include<iostream>#include<cmalloc>

char* allocateMemory();

int main(){ char *str; str=allocateMemory(); cout<<str; delete str; str= “ “; cout<<str;}

Char* allocateMemory(){Str=“memory allocation test, “; retrun str;}

Page 28: implementing oop_concept

04/27/15 MCA-114 29

Debugging Exercises

6. Identify the error in the following program#include<iostream> void display(const int const1=5) { const int const2=5; int arrary1[const1]; int arrary2[const2]; for(int i=0;i<5;i++) { arrary1[i]=I; array2[i]=i*10; cout<<array1[i]<<‘ ‘ <<array2[i]<<‘ ‘; }

} int main(){ display(5);}

Page 29: implementing oop_concept

04/27/15 MCA-114 30

Debugging Exercises

7. Identify the error in the following program.

#include<iostream> int gValue=10; void extra(){ cout<<gValue<<‘ ‘;}

int main(){ extra(); { int gValue=20; cout<<gValue<<‘ ‘; cout<<:gValue<<‘ ‘; }

}

Page 30: implementing oop_concept

04/27/15 MCA-114 31

Find out Output

8. #include<iostream> class user{ private: int i; float f; char c;public: void displaydata() { cout<<endl<<i<<‘\n’<<f<<“\n”<<c; } };

int main(){ cout<<sizeof(user); user u1;cout<<endl<<sizeof(u1);u1.displaydata(); }

Page 31: implementing oop_concept

04/27/15 MCA-114 32

Find out Output

9. #include<iostream> class date{ private: int dd,mm,yy; public: date() { cout<<endl<<“Reached here”; }};

int main(){ date today; date *p=&today; cout<<endl<<p;}