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 (10.7 MB, 49 trang )
86
Phạm vi lớp
class Money {
A
public:
void setDollars (int d);
…
Variables and
A
private:
int dollars;
functions declared in
};
the class declaration
(Money.h) are visible in
(int d)
the definitions of the
member functions.
void Money::setDollars
{
B
if (d >= 0)
Money::setDollars()
dollars = d;
else
dollars = 0;
}
B
87
Phạm vi file
To declare a variable
with file scope,
declare it outside any
block or class and
static int myVal;
void myFun(void);
int main (void){
myVal = 0;
precede it with the
keyword static.
myFun();
}
void myFun()
It is in scope from the
declaration to the end of
the file.
{
myVal++;
}
88
Thành viên dữ liệu tĩnh/không tĩnh
Thành viên dữ liệu không tĩnh
Mỗi một đối tượng của lớp có
một bản sao giá trị sở hữu của nó
Thành viên tĩnh
Như là một biến toàn cục
Một bản sao cho một lớp. Ví dụ
như bộ đếm
89
Thành viên dữ liệu tĩnh
Rectangle r1;
Rectangle r2;
Rectangle r3;
class Rectangle
{
private:
int width;
int length;
count
static int count;
public:
void set(int w, int l);
r1
r2
width
length
int area();
}
r3
width
length
width
length