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

Tài liệu lập trình C tiếng Việt Lesson3 class and object

34 424 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 34
Dung lượng 254 KB

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

Nội dung

int x,y;public: void initint ox, int oy; void moveint dx, int dy; void display; }; // Define component function outside declaration void point::initint ox, int oy{...  Dữ liệu thành phầ

Trang 1

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

Bài 3: Đối tượng và lớp

Trang 3

Đối tượng

Object = Data + Method

Point object { // data

int x,y;

//method void init (int ox, int oy);

void move (int dx, int dy);

void display();

};

Trang 4

<The definitions of component functions that aren’t

defined inside class declaration >

#include <iostream>

#include <conio>

class point{

private:

Trang 5

int x,y;

public:

void init(int ox, int oy);

void move(int dx, int dy);

void display();

};

// Define component function outside declaration

void point::init(int ox, int oy){

Trang 7

 Tạo đối tượng

<class name> <object name>;

Data frame

Data of object 1 Data of object 2

Trang 8

 Dữ liệu thành phần

Cú pháp khai báo dữ liệu thành phần tương tự như khai báo biến

<type> <data component name>;

void display();

}; // inside defined functions are inline function

Trang 9

 Con trỏ this trong hàm thành phần

Con trỏ this là con trỏ trỏ tới chính đối tượng đang gọi hàm thành phần

void point::init(int x, int y){

Trang 11

point::init(int a=0, int b=0);

int point::coincide (point pt)

{ return (x==pt->x && y==pt->y); }

Trang 12

y

a

5 2

x y b

5 2

x

y

5 2

x y

Dynamic data

Trang 13

point(int ox, int oy){x=ox; y=oy;}

void move(int dx,int dy);

void display();

};

void point::move(int dx, int dy){x+=dx; y+=dy}

void point::display(){cout<<“coordinate: ”<<x<<“ ”<<y<<“\n”;}

Trang 14

Thiết lập và hủy bỏ

 Các yêu cầu đối với phương thức thiết lập

point(int ox, int oy) {x=ox; y=oy;}

void move(int, int);

void display();

};

Trang 15

Thiết lập và hủy bỏ

 Phương thức thiết lập mặc định

Trang 16

point(int ox, int oy){x=ox; y=oy;}

void move(int dx,int dy);

void display();

~point(){cout<<“releasing object./n”}

};

void point::move(int dx, int dy){x+=dx; y+=dy}

void point::display(){cout<<“coordinate: ”<<x<<“ ”<<y<<“\n”;}

Trang 17

Thiết lập và hủy bỏ

 Các yêu cầu đối với phương thức hủy bỏ:

 Nếu lớp không định nghĩa phương thức hủy bỏ, trình biên dịch sẽ tạo một phương thức hủy bỏ mặc định

Trang 18

Phương thức hủy bỏ cho lớp vector

delete [] v;

}

Trang 20

Khai báo thiết lập copy

point (point &); or point (const point &);

// do not declare point (point);

class point{

int x,y;

public:

point(int ox=1, int oy=0){ //constructor

cout<<“Create object: ”<<this<<endl;

cout<<“Two paramenters constructor.\n”;

x=ox; y=oy;

}

point (point &p){

cout<<“Create object: ”<<this<<endl;

cout<<“Copy constructor.\n”;

x=p.x; y=p.y;

}

Trang 21

Thiết lập copy

void move(int dx,int dy);

void display();

};

void point::move(int dx, int dy){x+=dx; y+=dy}

void point::display(){cout<<“Coordinate: ”<<x<<“ ”<<y<<“\n”;}

point fct(point a){

point a(5,2); a.dislay();

point b=fct(a); b.display();

}

Trang 23

b.n b.x

Trang 24

b.n b.x

Trang 25

Khởi tạo dữ liệu tĩnh

Trang 27

Dữ liệu tĩnh

++Created: there are 1 objects

++ Created: there are 2 objects

++ Created: there are 3 objects

Deleted: there are 2 objects

Deleted: there are 1 objects

++ Created: there are 2 objects

Deleted: there are 1 objects

Deleted: there are 0 objects

Dữ liệu tĩnh có thuộc tính truy cập là public hoặc private

Dữ liệu tĩnh độc lập với các thể hiện của lớp

Trang 28

Hàm thành phần tĩnh

 Khai báo với từ khóa static

 Không có con trỏ this

 Không sử dụng dữ liệu không tĩnh

 Được gọi không qua đối tượng nào

<classname>::<static function>(<parameters>)

Trang 29

}

Trang 31

int coincide(point p, point q){

return ((p.x==q.x) && (p.y==q.y));

}

Trang 34

Tất cả các hàm thành phần của lớp là bạn của lớp khác

Ngày đăng: 28/03/2016, 01:02

TỪ KHÓA LIÊN QUAN