1. Trang chủ
  2. » Công Nghệ Thông Tin

bài giảng chú giải trình hướng đối tượng bài 1

42 286 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 474 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 sc

Trang 1

What is Object Oriented

• Objects communicate to other

objects by sending messages.

• Messages are received by the

methods of an object

Trang 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, …

Trang 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

Or

• An "object" is anything to which a concept

applies

Trang 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

Trang 5

Example: The Person class

public processes

Trang 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.

Trang 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

Trang 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

Trang 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)

Trang 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)

Trang 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

Trang 14

Classes (syntax simplified)

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

class $

attr

〈 AttrName1 〉

: 〈 AttrNamen 〉

meth 〈 Pattern 〉 〈 Statement 〉 end

:

meth 〈 Pattern 〉 〈 Statement 〉 end

end

Trang 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)

private , and protected to define visibility

• Unfortunately, different languages use these

keywords to define different scopes

– Source of enormous confusion! Be careful!

Trang 16

Public and private scopes

only visible in the object instance

– The object instance can see all the private members in its class and its super classes

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

Trang 17

Function and data member

Trang 18

continue

Trang 19

Default Arguments

• A default argument is a value given in the

function declaration that the compiler

automatically inserts if the caller does not provide a value for that argument in the

function call

• Syntax:

Trang 20

Default Arguments

(Examples)

• The default value of the 2nd argument is 2

• This means that if the programmer calls

pow(x), the compiler will replace that call with pow(x,2), returning x2

double pow( double x, int n=2)

// computes and returns xn

Trang 21

Default Arguments

(Rules)

• Once an argument has a default value, all

the arguments after it must have default

values

• Once an argument is defaulted in a function

call, all the remaining arguments must be defaulted

int f( int x, int y=0, int n) int f( int x, int y=0, int n=1)

Trang 22

Static Members

Trang 23

Continue

Trang 24

• Add (int, int, int)

• Function overloading is useful when similar function is required to be called with either variable number of arguments or arguments of different type or both.

Trang 26

What is a Friend Function?

• A friend function is used for accessing the non-public members of a class A class can allow non-member functions and other classes to access its own private data, by making them friends Thus, a friend

function is an ordinary function or a member of another class

How to define and use Friend Function in C++:

• The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend The friend function must have the class to which it is declared as friend passed to it in argument

Some important points

• The keyword friend is placed only in the function declaration of the friend function and not in the

function definition.

It is possible to declare a function as friend in any number of classes.

• When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend.

• It is possible to declare the friend function as either private or public.

Trang 27

const member functions

• A function, which guarantees not to modify the invoking object

• If the body of the const function contains a

statement that modifies the invoking object, the program does not compile

• One exception here is the mutable member A mutable data member can be modified by

const function

Trang 28

If rollno definition is changed to

mutable int rollno;

Trang 30

volatile member functions Declare a member function with the volatile specifier to

ensure that it can be called safely for a volatile object:

volatile B b; // b is a volatile object

b.f(); // call a volatile member function safely

}

The object b is declared volatile Calling a non-volatile member function from this

Trang 31

Pointers and objects

Trang 34

Allocating memory using new

Point *p = new Point(5, 5);

• new allocates space to hold the object.

• new calls the object’s constructor.

• new returns a pointer to that object.

Trang 35

Deallocating memory using delete

Trang 36

Using new with arrays

Trang 37

• A pointer can point to an object created by a class.

• Object pointers are useful in creating objects at run time.

Trang 38

constant objects

const student s1(x,y); // object s1 is constant

• Any attempt to modify the values of x and y will generate compile time error.

• A constant object can call only constant member functions.

Trang 41

C++ and C

• C is a subset of C++

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.

Trang 42

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

Ngày đăng: 24/10/2014, 16:24

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w