hcphap@gmail.com, hcphap@dut.udn.vn
Session Objectives
Discuss the following:
The Object-Oriented approach Drawbacks of traditional programming Object-Oriented programming
Compare Classes with Structures Describe Private and Public sections of Classes
Sales
Functions/procedures/subroutines adopted to make programs more comprehensible. As programs grow larger and more complex, even use of functions can create problems.
Difficulty in implementation
Focus of traditional programming approach: On implementation details. Focus of a persons thinking: In terms of things or entities, their properties and actions. OOP techniques help to correspond realworld entities and actions to functions and data at the programming level.
Restrict access to the data so that only a few critical functions act upon it.
Introduction to OOP
OOP allows for analysis and design of an application in terms of entities or objects. Process replicates the human thought process as closely as possible. Code and data are merged into a single indivisible thing -- an object. Close match between objects in the programming sense and objects in the real world.
Objects
Represent an entity in the real world. A concept or thing with defined boundaries that is relevant to the problem we are dealing with. Objects serve two purposes:
They help to understand the real world They provide a practical basis for a computer application
Objects (Contd.)
Each object has its own properties or characteristics that describe what it is or does.
Vehicles in a traffic-monitoring application Menus The mouse and the keyboard A personnel file A table of marks relating to an examination Time Complex numbers
Different Objects
Name: Jack Age: 28 Weight: 65 kgs Actions: Walk Talk Sleep Model: Ferrari Colour: Red Year: 1995
Classes
Grouping of objects that have the same properties, common behaviour and common relationships. The term class is an abbreviation of class of objects.
Example, A class of persons, class of animals, class of processes.
Property/Attribute
A characteristic required of an object or entity when represented in a class is called a property. A class is a prototype and not an actual specimen of the entity. Each instance of the class or object has its own value for each of its properties but it shares the property names or operations with other instances of the class.
Abstract into
Method
An action required of an object or entity when represented in a class is called a method.
In a polygon class "draw", "erase" and "move" are examples of the methods that are part of the class.
Method (Contd.)
Object is a "black box" which receives and sends messages.
What is the What is the salary of salary of Jack? Jack?
Sales
The black box actually contains code (sequences of computer instructions) and data (information which the instructions operates on). Information passed to and retrieved from each department either by inter-departmental memos or verbal instructions are the messages between objects. These messages can be translated to function calls in a program.
Accounts
Abstraction
Process of examining certain aspects of a problem. An abstract specification tells us what an object does independent of how it works.
Data Abstraction
Data Abstraction
Process of examining all the available information about an entity to identify information that is relevant to the application.
Used to identify properties and methods of each object as relevant to the application at hand. By grouping objects into classes, we are doing data abstraction of a problem. Common definitions are stored once per class rather than once per instance of the class. Methods can be written once for a class, so that all the objects in a class benefit from code reuse.
Inheritance
It is the property that allows the reuse of an existing class to build a new class. The superclass is the class from which another class inherits its behaviour. The class that inherits the properties and methods of another class is called the subclass.
Inheritance (Contd.)
Each subclass shares common properties with the class from which it is derived.
For example, all vehicles in a class may share similar properties of having wheels and a motor
Encapsulation
Providing access to an object only through its messages, while keeping the details private is called information hiding. An equivalent buzzword is encapsulation.
Animals
Insects
Mammals
Reptiles
Amphibians
All communication to an object is done via messages. Messages define the interface to the object.
Encapsulation is the process that allows selective hiding of properties and methods in a class
Encapsulation (Contd.)
The advantage: a class can have many properties and methods but only some of these need to be exposed to the user. Ex. Company A have many methods and properties that are classified as private.
When company B deal with Company A, it does not interfere with the way in which Company A functions. Any information that Company B needs has to be strictly routed through proper channels.
Reusability
Object-oriented development allows:
Information to be shared within an application Reuse of designs and code on future projects
Programs are broken down into reusable objects. These objects can then be grouped together in different ways to form new programs. Inheritance also promotes reuse.
Polymorphism
Polymorphism means that the same functions may behave differently on different classes. The existing object stays the same, and any changes made are only additions to it.
Actions: Roar Eat flesh Hunt big game
Cats
Lions
Polymorphism
Subclasses Class: Shape Methods: Draw Move Initialise
Polymorphism promotes encapsulation. The Draw method is implemented for different cases but how it is to be implemented need not be visible to the user.
Private data is not available outside the class to any program Also, private data of other classes is hidden and not available within the functions of this class.
Data or functions
Public
Data or functions
Member functions
In C++, a function declared as a member of a class is called a member function.
Member functions are usually put in the public part of the class because they have to be called outside the class either in a program or in a function.
Declaration of a member function must define its return value as well as the list of its arguments.
For example, the member function void setdate(int, int, int) has no return value or a void return value, and has three integer arguments.
Defining Objects
exampleclass object1,object2; defines two objects, object1 and object2, of class exampleclass. The definition actually creates objects that can be used by the program. When we define an object, space is set aside for it in memory.
A member function is always called to act on a specific object, not on the class in general. Associated with a specific object with the dot operator ( the period).
object2
Passing Objects
Objects can be passed to a function and returned back just like normal variables. The compiler creates another object as a formal variable in the called function. It copies all the data members from the actual object.
int function1(exampleclass obj1) {return object_data;} Formal argument
When a member function is called, it is given access to the object of which the function is a member.
Returning Objects
A return statement in a function is considered to initialise a variable of the returned type.
exampleclass object3 = object1.function1(object2);
Object-Oriented Languages
C++ , Smalltalk , Eiffel, CLOS, Java Vary in their support of object-oriented concepts. No single language that matches all styles and meets all needs.
Passing and returning of objects is not very efficient since it involves passing and returning a copy of the data members.
An introduction to C++
Developed by Bjarne Stroustrup at AT&T Bell Laboratories. Derived from the C language. It is compatible with C (it is actually a superset). Most important elements added to C to create C++ are concerned with classes, objects and object-oriented programming.