Tải bản đầy đủ (.pptx) (57 trang)

Tài liệu lập trình C Shap Lesson1 introduction

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (921.85 KB, 57 trang )

Lập trình hướng đối tượng

Giới thiệu


Nội dung khóa học


Phần 1: Giới thiệu lập trình hướng đối tượng









Các khái niệm và ngôn ngữ

Phần 2: Lập trình hướng đối tượng với C++



The advantage of C++ over C



Object & Class




Operator



Polymorphism



Template



I/O Stream - Exception

Phần 3: Lập trình hướng đối tượng với C#



Giới thiệu C#, .Net



Thừa kế và một số kỹ thuật



Ứng dụng với C#

Thài liệu tham khảo



Nguyễn Thanh Thuỷ: Lập trình hướng đối tượng với C++, NXB Khoa học và kỹ thuật

2


Phần 1



Giới thiệu



Các nguyên tắc hướng ĐT cơ bản



Types, Classes, Objects và Interfaces



Inheritance, Polymorphism và Delegation



Các ngôn ngữ HĐT

3



Computer programming

“The history of computer programming is a steady move
away from machine-oriented views of programming
towards concepts and metaphors that more closely reflect
the way in which we ourselves understand the world”

4


Programming progression ...

 Programming has progressed through:
machine code
assembly language
machine-independent programming languages
procedures & functions
objects

5


Machine language – Mark I

6


Machine language


0000 1001 1100 0110 1010 1111 0101 1000
1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111

7


Assembly language – PDP-11

8


Assembly language – Macro-11
GCD:

TST

B

BEQ
MOV

SIMPLE
A, R5

SXT

SIMPLE:


R4

DIV

B, R4

MOV

B, A

MOV

R5, B

CALL

GCD

RETURN

9


Machine-Independent Programming Languages – Fortran
! This example program solves for roots of the quadratic equation,
! ax^2 +bx +c =0,for given values of a, b and c.
!
PROGRAM bisection
IMPLICIT NONE

INTEGER :: iteration
DOUBLE PRECISION :: CC, Er, xl, x0, x0_old, xr

! Set convergence criterion and guess for xl, xr.
CC = 1.d-4
xl = 8.d-1
xr = 11.d-1

! Bisection method.
Er =CC +1
iteration = 0
DO WHILE (Er > CC)
iteration = iteration + 1

! Compute x0 and the error.
x0_old = x0
x0 = (xl + xr) / 2.d0

this is partial…

Er = DABS((x0 - x0_old)/x0)*100.d0
WRITE (*,10) iteration, x0_old, x0, Er
10 FORMAT (1X,I4,3(2X,E10.4))

10


Procedures & Functions – Pascal
program ValueArg(output);
{Shows how to arrange for a procedure to have arguments.}


procedure PrintInitials(First, Last : char);
{Within this procedure, the names First and Last represent the argument values. We’ll call write to print
them.}
begin
write(‘My initials are: ’);
write(First);
writeln(Last)
end; {PrintInitials}

begin
PrintInitials (‘D’, ‘C’); {Any two characters can be arguments.}
PrintInitials (‘Q’, ‘T’); {Like strings, characters are quoted.}
PrintInitials (‘&’, ‘#’)
end. {ValueArg}
11


Objects
class Time {

private:
int hour, minute;

public:
Time (int h, int m) {
hour = h;
minute = m;
}


void addMinutes (int m) {
int totalMinutes =
((60*hour) + minute + m) % (24*60);
if (totalMinutes<0)
totalMinutes = totalMinutes + (24*60);
hour = totalMinutes / 60;
minute = totalMinutes % 60;
}
}

this is partial…

12


Objects
class

Time

inTime

hour

Attributes:
hour = 8

minute

minute = 30

Methods:
void addMinutes(int m)

void addMinutes( int m )

outTime
Attributes:
hour = 17
minute = 35

objects

Methods:
void addMinutes(int m)

13


“Intrinsic Power” vs. “Effective Power”
 This progression is not a matter of “intrinsic power”


Anything you can do with a maximally capable computer language, you can theoretically do with
any other minimally capable computer language



But that is like saying a shovel is theoretically as capable as a tractor. In practice, using a shovel
might make things very hard…


14


Lập trình hướng đối tượng là gì?



Lập trình HĐT

 Phương pháp lập trình dựa trên các ĐT để xây dựng chương trình.



Đối tượng

 Vật thật hay trừu tượng với thuộc tính và hành vi
 Attribute: State, data
 Behavior: Action, method hay function

15


Grady Booch

“Object-oriented programming
is a method of implementation
in which programs are organized
as cooperative collections of
objects, each of which represents
an instance of some class...”

Grady Booch

16


Phát triển ứng dụng hướng đối tượng



Cách thiết kế và xây dựng ứng dụng

 Các đối tượng bó các dữ liệu và phương thức cùng nhau
 Các đối tượng cho phép phân tách định nghĩa và thể hiện



Không chỉ là cú pháp

 Lập trình hướng đối tượng còn bao hàm tư duy hướng đối tượng

17


Thiết kế một ứng dụng hướng đối tượng
Order
Take an Order

Customer

Create a Customer

Assign Salesperson

Check Credit

beOrder
Progress
Customer Table
DataSet

Customer
Table

Order Table

18


Phần 1



Giới thiệu



Các nguyên tắc hướng ĐT cơ bản



Types, Classes, Objects và Interfaces




Inheritance, Polymorphism và Delegation



Các ngôn ngữ HĐT

19


Các nguyên tắc hướng đối tượng cơ bản



Abstraction



Encapsulation



Hierarchies

20


Trừu tượng hóa

Góc nhìn bên ngoài của đối tượng



Trừu tượng hóa được sử dụng để quản lý sự phức tạp

 Tập trung vào các tính chất bản chất
 Loại bỏ các chi tiết
 Tìm các điểm chung của các đối tượng



Defines the public contract

 Mô tả bên ngoài cho người dùng đối tượng
 “Góc nhìn bên ngoài”
 Độc lập với thể hiện

21


Abstraction - Example

“What should an Order object

Order

do?”

CreateOrder

UpdateOrder
GetOrderTotal
Next

InternalOrder
Two types of
Orders

ExternalOrder

22


Đóng gói
Ẩn giấu các chi tiết thể hiện



Đóng gói ẩn giấu thể hiện

 Thúc đẩy thiết kế phần mềm theo module – dữ liệu và phương thức được bó cùng nhau
 Việc truy cập dữ liệu thường thông qua phương thức
 Thường được gọi là “ẩn giấu thông tin”



Cung cấp 2 dạng bảo vệ:

 Trạng thái không thể bị thay đổi trực tiếp từ bên ngoài
 Thể hiện có thể được thay đổi mà không ảnh hưởng tới người dùng đối tượng


23


Đóng gói – thí dụ
Implementation

Outside View

Order
orderNum AS INT
custNum AS INT

Public methods of Order

CalculatePrice( )

PUBLIC:
CreateOrder( )

CreateOrder

UpdateOrder( )

UpdateOrder

GetOrderTotal( )

GetOrderTotal


Next( )

Next

24


Đóng gói – thí dụ (tiếp)

Order
orderNum AS INT
custNum AS INT

Hmm...

I’d like to

change CalculatePrice to
CalculateTotalPrice

CalculatePrice( )

PUBLIC:
CreateOrder( )
UpdateOrder( )
GetOrderTotal( )

GetOrderTotal calls
CalculatePrice( )


Next( )

25


×