Anda di halaman 1dari 47

Session 8

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 1 of 47

Session Objectives (1)


Discuss the following:
  

The Object-Oriented approach Drawbacks of traditional programming Object-Oriented programming

Discuss basic Object-Oriented concepts such as:


 

Objects Classes
 

Properties Methods

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 2 of 47

Session Objectives (2)


   

Abstraction Inheritance Encapsulation Polymorphism

Compare Classes with Structures Describe Private and Public sections of Classes Define Member functions

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 3 of 47

Session Objectives (3)


Use the Objects and Member functions of a Class
  

Define Objects Access Member Functions Pass and return Objects

Discuss briefly the features of C++ and another OO language (Smalltalk)

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 4 of 47

The Object-Oriented approach


Can classify living beings as objects just as we classify things we use as objects of different types. Any application can be defined in terms of entities or objects so that the process replicates human thought process as closely as possible.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 5 of 47

Departments as objects in an organisation

Personnel

Accounts

Sales

People in each department control and operate on that department's data.


Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 6 of 47

Traditional Programming Drawbacks (1)


Unmanageable programs


When programs become larger they become unmanageable.

Problems in modification of data




Adding new data items means modifying all the tasks or functions that access the data. Next to impossible to separate parts of a program from revisions made in another part.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 7 of 47

Traditional Programming Drawbacks (2)


Difficulty in Implementation


Traditional Programming focussed on implementation details. In Traditional Programming code and data were stored separately.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 8 of 47

Introduction to OOP
OOP allows for analysis and design of an application in terms of entities or objects. Code and data are merged into a single indivisible thing -- an object.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 9 of 47

Data and Functions of an Object


Data:
Number of employees Salary statements Bills Vouchers Receipts Petty cash records Banking data
Accounts

Functions:
Calculate salary Pay salary Pay bills Tally accounts Transact with banks

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 10 of 47

Objects
A concept or thing with defined boundaries that is relevant to the problem we are dealing with.

Objects serve two purposes:


 

Help to understand the real world Provide a practical basis for a computer application

Each object has its own properties or characteristics that describe what it is or does.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 11 of 47

Different Objects
Name: Jack Age: 28 Weight: 65 kgs Actions: Walk Talk Sleep Model: Ferrari Colour: Red Year: 1995

Actions: Start Stop Accelerate

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 12 of 47

Classes
Group 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.

Each object is said to be an instance of its class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 13 of 47

Objects and Classes


Polygon class

Polygon objects

Properties: Vertices Border color Abstract Fill color


into

Methods: Draw Erase Move

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 14 of 47

Property/Attribute
A characteristic required of an object or entity when represented in a class 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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 15 of 47

Method
Sales
What is the salary of Jack


The black box actually contains code The communication between objects is done using messages. These messages can be translated to function calls in a program.

Jacks salary is $2000

Accounts

An action required of an object or entity when represented in a class is known as a method


Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 16 of 47

Abstraction
Process of examining certain aspects of a problem.

An abstract specification tells us what an object does independent of how it works.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 17 of 47

Data Abstraction
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.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 18 of 47

Inheritance (1)
The property that allows the reuse of an existing class to build a new class. Superclass - The class from which another class inherits its behaviour. Subclass - The class that inherits the properties and methods of another class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 19 of 47

Inheritance (2)
Each subclass shares common properties with the class from which it is derived. Subclass may have its own particular characteristics.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 20 of 47

Class Animals and its subclasses


Animals

Insects

Mammals

Reptiles

Amphibians

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 21 of 47

Encapsulation (1)
Providing access to an object only through its messages, while keeping the details private is called information hiding. An equivalent buzzword is encapsulation.


A class has many properties and methods. It is not necessary for a user to have access to all of them. All communication to an object is done via messages. Messages define the interface to the object.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 22 of 47

Encapsulation (2)
Encapsulation:


Builds an impenetrable wall to protect the code from accidental corruption due to the silly little errors that we are all prone to make.

Isolates errors to small sections of code to make them easier to find and fix.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 23 of 47

Reusability
Object-oriented development allows:
 

Information to be shared within an application Reuse of designs and code for future projects

Programs are broken down into reusable objects. Inheritance also promotes reuse.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 24 of 47

Felines and Subclasses


Felines Actions: Make sounds Eat/drink Hunt prey

Cats

Actions: Mew Drink milk Hunt mice

Lions

Actions: Roar Eat flesh Hunt big game

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 25 of 47

Polymorphism (1)
Polymorphism enables the same function to behave differently on different classes. The existing object stays the same, and any changes made are only additions to it.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 26 of 47

Polymorphism (2)
Subclasses Class: Shape Methods: Draw Move Initialise

Polymorphism promotes encapsulation.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 27 of 47

Class Construct
The general syntax of the class construct is:
class user_defined name{ private: data_type members; implementation operations; public: data_type members; implementation operations; };

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 28 of 47

Classes and Structures


A structure contains one or more data items (called members) which are grouped together as a single unit. Class consists of not only data elements but also functions, which operate on the data elements. All the variables and the functions declared in a class, whether in the public or private section, are the members of the class. In a structure all elements are public by default.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 29 of 47

Public and Private (1)


Class members can be declared in the public or the private sections of a class. To prevent data from unrestricted access, the data members of a class are normally declared in the private section. The public part constitutes the interface to objects of the class. The data members and functions declared in the public section, can be accessed by any function in the outside world (outside the class).
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 30 of 47

Public and Private (2)


Class Private Not accessible from outside class ccessible from outside class Data or functions Public Data or functions

Private data is not available outside the class

Private data of other classes is hidden and not available within the functions of this class.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 31 of 47

Member functions (1)


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 has a void return value, and has three integer arguments.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 32 of 47

Member functions (2)


Member functions can be more complex as they can have local variable, parameters and other elements. Should not have the same name as a data member.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 33 of 47

Using the class (1)


class exampleclass{ private: object_data is an integer; member_function1(parameter1, parameter2) {assign value to object_data} member_function2(){display data} };

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 34 of 47

Using the class (2)


main program{ exampleclass object1,object2; object1.member_function1(200);

object1.member_function2(); object2.member_function1(350); object2.member_function2(); }


Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 35 of 47

Defining Objects
exampleclass object1,object2; defines two objects, object1 and object2, of class exampleclass. When we define an object, space is set aside for it in memory.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 36 of 47

Calling member functions (1)


We communicate with objects by calling their member functions.
object1.member_function1(200); object1.member_function2();

A member function is always called to act on a specific object, not on the class. Associated with a specific object with the dot operator ( the period).

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 37 of 47

Calling member functions (2)


The general syntax for accessing a member function of a class is class_object.function_member() The dot operator is called the class member operator.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 38 of 47

Two objects with different values


object_data object_data 200 Specifications for exampleclass objects Objects of the class exampleclass object1

object_data 350

object2

exampleclass class specifier


Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 39 of 47

Passing Objects (1)


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.
int function1(exampleclass obj1) { return object_data;} Formal argument
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 40 of 47

Passing Objects (2)


The above function can be invoked using a function call such as,
int variable = object1.function1(object2);

actual argument When a member function is called, it is given access to the object of which the function is a member.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 41 of 47

Returning Objects
A return statement in a function is considered to initialise a variable of the returned type.
exampleclass object3 = object1.function1(object2);

Passing and returning of objects is not very efficient since it involves passing and returning a copy of the data members.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 42 of 47

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.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 43 of 47

An introduction to C++ (1)


Developed by Bjarne Stroustrup at AT&T Bell Laboratories. Derived from the C language. Is compatible with C.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 44 of 47

An introduction to C++ (2)


C++ programs are fast and efficient but it sacrifices some flexibility in order to remain efficient. C++ uses compile-time binding. Provides high run-time efficiency and small code size, but it trades off some of the power to reuse classes.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 45 of 47

Smalltalk (1)
Pure object-oriented language. While C++ makes some practical compromises to ensure fast execution and small code size, Smalltalk makes none. Uses run-time binding, which means that nothing about the type of an object need be known before a Smalltalk program is run. Significantly faster to develop than C++ programs.

Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 46 of 47

Smalltalk (2)
Has a rich class library that can be easily reused via inheritance Has a dynamic development environment. It is not explicitly compiled, like C++. Smalltalk generally takes longer to master than C++. It is syntactically very simple.
Linux, C, C++ / Object Oriented Programming with C++ / Session 8 / 47 of 47

Anda mungkin juga menyukai