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 1Lậ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 5int 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 11point::init(int a=0, int b=0);
int point::coincide (point pt)
{ return (x==pt->x && y==pt->y); }
Trang 12y
a
5 2
x y b
5 2
x
y
5 2
x y
Dynamic data
Trang 13point(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 14Thiế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 15Thiết lập và hủy bỏ
Phương thức thiết lập mặc định
Trang 16point(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 17Thiế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 18Phương thức hủy bỏ cho lớp vector
delete [] v;
}
Trang 20Khai 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 21Thiế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 23b.n b.x
Trang 24b.n b.x
Trang 25Khởi tạo dữ liệu tĩnh
Trang 27Dữ 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 28Hà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 31int coincide(point p, point q){
return ((p.x==q.x) && (p.y==q.y));
}
Trang 34Tất cả các hàm thành phần của lớp là bạn của lớp khác