+ All Categories
Home > Documents > What is Object Oriented Programming?

What is Object Oriented Programming?

Date post: 06-Jan-2016
Category:
Upload: urvi
View: 25 times
Download: 0 times
Share this document with a friend
Description:
An object is like a black box. The internal details are hidden. Identifying objects and assigning responsibilities to these objects. Objects communicate to other objects by sending messages . Messages are received by the methods of an object. What is Object Oriented Programming?. - PowerPoint PPT Presentation
Popular Tags:
29
1 What is Object Oriented Programming? An object is like a black box. The internal details are hidden. Identifying objects and assigning responsibilities to these objects. Objects communicate to other objects by sending messages. Messages are received by the methods of an object
Transcript
Page 1: What is Object Oriented Programming?

1

What is Object Oriented Programming?

An object is like a black box.

The internal details are hidden.

Identifying objects and assigning responsibilities to these objects.

Objects communicate to other objects by sending messages.

Messages are received by the methods of an object

Page 2: What is Object Oriented Programming?

2

What is an object?

Tangible Things as a car, printer, ...Roles as employee, boss, ...Incidents as flight, overflow, ...Interactions as contract, sale, ...Specifications as colour, shape, …

Page 3: What is Object Oriented Programming?

3

So, what are objects?

an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain.

OrAn "object" is anything to which a

concept applies. Etc.

Page 4: What is Object Oriented Programming?

4

Why do we care about objects?

Modularity - large software projects can be split up in smaller pieces.

Reuseability - Programs can be assembled from pre-written software components.

Extensibility - New software components can be written or developed from existing ones.

Page 5: What is Object Oriented Programming?

Example: The Person class#include<string>#include<iostream>class Person{ char name[20]; int yearOfBirth;public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl; } //...};

private data

public processes

Page 6: What is Object Oriented Programming?

6

The two parts of an object

Object = Data + Methods or to say the same differently:

An object has the responsibility to know and the responsibility to do.

= +

Page 7: What is Object Oriented Programming?

7

Basic Terminology

Abstraction is the representation of the essential features of an object. These are ‘encapsulated’ into an abstract data type.

Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.

Page 8: What is Object Oriented Programming?

8

Basic Terminology:Inheritance

Inheritance means that one class inherits the characteristics of another class.This is also called a “is a” relationship:

A car is a vehicle

A teacher is a person

A dog is an animal

Page 9: What is Object Oriented Programming?

9

Basic Terminology:Polymorphism

Polymorphism means “having many forms”. It allows different objects to respond to the same message in different ways, the response specific to the type of the object.E.g. the message displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).

Page 10: What is Object Oriented Programming?

10

Basic Terminology:Aggregation

Aggregation describes a “has a” relationship. One object is a part of another object.

We distinguish between composite aggregation (the composite “owns” the part) and shared aggregation (the part is shared by more then one composite).

A car has wheels.

Page 11: What is Object Oriented Programming?

11

Basic Terminology:Behaviour and Messages

The most important aspect of an object is its behaviour (the things it can do). A behaviour is initiated by sending a message to the object (usually by calling a method).

Page 12: What is Object Oriented Programming?

12

Abstract classes

ListClass is an abstract class, i.e., a class in which some methods are left undefined (such as init and append)

Abstract classes are not intended to be instantiated, but inherited from

Inheritance “fills in the blanks” by adding the missing methods

The result is a concrete class, i.e., a class that can be instantiated since all its methods are defined

NilClass and ConsClass are concrete classes This technique is an example of higher-order

programming (namely genericity: passing a procedure value, i.e., a method)

Page 13: What is Object Oriented Programming?

13

The two steps of Object Oriented Programming

Making Classes: Creating, extending or reusing abstract data types.

Making Objects interact: Creating objects from abstract data types and defining their relationships.

Page 14: What is Object Oriented Programming?

14

Classes (syntax simplified)

A class is also a value that can be in an expression position

class $attr AttrName1

: AttrNamenmeth Pattern Statement end

:meth Pattern Statement end

end

Page 15: What is Object Oriented Programming?

15

Controlling visibility

Visibility is the control given to the user to limit access to members of a class (attributes and methods)

Each member (attribute or method) is defined with a scope (part of program text that the member can be accessed by name)

Programming languages use words like public, private, and protected to define visibility

Unfortunately, different languages use these keywords to define different scopes Source of enormous confusion! Be careful!

Page 16: What is Object Oriented Programming?

16

Public and private scopes

In Smalltalk and Oz, a private member is one which is only visible in the object instance The object instance can see all the private

members in its class and its super classesIn C++ and Java, a private member is

visible among all instances of a given class, but not to subclasses

A public member is visible anywhere in the program

By default, attributes are private and methods are public

Page 17: What is Object Oriented Programming?

17

Function and data member

Page 18: What is Object Oriented Programming?

18

continue

Page 19: What is Object Oriented Programming?

19

Function overloading

When a function is refined with different set of arguments,then it is known as overloaded function.This process is known as function overloading.

Example: int add(); int add(int,int); int add(float,float);

Page 20: What is Object Oriented Programming?

20

Friend Function

Page 21: What is Object Oriented Programming?

21

Const and volatile function

C++ allows you to restrict the use of a particular member function so, as the programmer, you can insure that the user can only use it in the appropriate context. This is accomplished with the keywords const and volatile.

const objects const tells the compiler that a variable will not change throughout its

lifetime. This applies to variables of built-in types, as you've seen. When the compiler sees a const like this, it stores the value in its symbol table and inserts it directly, after performing type-checking (remember that this is an improvement in C++, which acts differently than C). In addition, it prevents you from changing the value of a const. The reason to declare an object of a built-in type as const is so the compiler will insure that it isn't changed.

You can also tell the compiler that an object of a user-defined type is a const. Although it is conceptually possible that the compiler could store such an object in its symbol table and generate compile-time calls to member functions (so all the values associated with a const object would be available at compile-time), in practice it isn't feasible. However, the other aspect of a const — that it cannot be changed during its lifetime — is still valid and can be enforced.

Page 22: What is Object Oriented Programming?

22

const member functions

The compiler can tell when you're trying to change a simple variable, and can generate an error. The concept of constness can also be applied to an object of a user-defined type, and it meanst the same thing: the internal state of a const object cannot be changes. This can only be enforced if there is some way to insure that all operations performed on a const object won't it. C++ provides a special syntax to tell the compiler that a member function doesn't change an object.

The keyword const placed after the argument list of a member function tells the compiler that this member function can only read data members, but it cannot write them. Creating a const member function is actually a contract which the compiler enforces. If you declare a member function like this:

class X { int i; public: int f() const; };then f( ) can be called for any const object, and the compiler knows that it's a safe thing to do because you've said the function is const

Page 23: What is Object Oriented Programming?

23

volatile objects and member function

A volatile object is one which may be changed by forces outside the program's control. For example, in a data communication program or alarm system, some piece of hardware may cause a change to a variable, while the program itself may never change the variable. The reason it's important to be able to declare a variable volatile is to prevent the compiler from making assumptions about code associated with that variable. The primary concern here is optimizations. If you read a variable, and (without changing it) read it again sometime later, the optimizer may assume that the variable hasn't changed and delete the second read. If the variable was declared volatile, however, the optimizer won't touch any code associated with the variable.

The syntax for const and volatile member functions is identical. Only volatile member functions may be called for volatile objects. In addition, objects and member functions can be both const and volatile, as shown here:

Page 24: What is Object Oriented Programming?

24

Static Members

Page 25: What is Object Oriented Programming?

25

Continue

Page 26: What is Object Oriented Programming?

26

Nested classes

A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.

Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class

Page 27: What is Object Oriented Programming?

27

Local classes

A local class is declared within a function definition. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.

Page 28: What is Object Oriented Programming?

28

C++ and C

C is a subset of C++.Advantages: Existing C libraries can be used, efficient code can be generated.But: C++ has the same caveats and problems as C (e.g. pointer arithmetic,…).

C++ can be used both as a low level and as a high level language.

We focus on the

high level

aspects.

Page 29: What is Object Oriented Programming?

29

C++ and Java

Java is a full object oriented language, all code has to go into classes.

C++ - in contrast - is a hybrid language, capable both of functional and object oriented programming.

So, C++ is more powerful but also more difficult to handle than Java.


Recommended