<< Chapter < Page Chapter >> Page >

Các thành viên private chỉ có thể được truy cập bởi các hàm thành viên (và các hàm friend) của lớp đó. Các thành viên public của lớp có thể được truy cập bởi bất kỳ hàm nào trong chương trình.

Mục đích chính của các thành viên public là để biểu thị cho client của lớp một cái nhìn của các dịch vụ (services) mà lớp cung cấp. Tập hợp này của các dịch vụ hình thành giao diện public của lớp. Các client của lớp không cần quan tâm làm thế nào lớp hoàn thành các thao tác của nó. Các thành viên private của lớp cũng như các định nghĩa của các hàm thành viên public của nó thì không phải có thể truy cập tới client của một lớp. Các thành phần này hình thành sự thi hành của lớp.

Ví dụ 3.5: Chương trình sau cho thấy rằng các thành viên private chỉ có thể truy cập thông qua giao diện public sử dụng các hàm thành viên public.

#include<iostream.h>

class MyClass

{

private:

int X,Y;

public:

void Print();

};

void MyClass::Print()

{

cout<<X<<Y<<endl;

}

int main()

{

MyClass M;

M.X = 3;

M.Y = 4;

M.Print();

return 0;

}

Khi chúng ta biên dịch chương trình này, compiler phát sinh ra hai lỗi tại hai dòng 20 và 21 như sau:

Hình 3.5: Thông báo lỗi của ví dụ 3.5

Thuộc tính truy cập mặc định đối với các thành viên của lớp là private. Thuộc tính truy cập các thành viên của một lớp có thể được thiết lập rõ ràng là public, protected hoặc private. Thuộc tính truy cập mặc định đối với các thành viên của struct là public. Thuộc tính truy cập các thành viên của một struct cũng có thể được thiết lập rõ ràng là public, protected hoặc private.

Truy cập đến một dữ liệu private cần phải được điều khiển cẩn thận bởi việc sử dụng của các hàm thành viên, gọi là các hàm truy cập (access functions).

Các hàm truy cập và các hàm tiện ích

Không phải tất cả các hàm thành viên đều là public để phục vụ như bộ phận giao diện của một lớp. Một vài hàm còn lại là private và phục vụ như các hàm tiện ích (utility functions) cho các hàm khác của lớp.

Các hàm truy cập có thể đọc hay hiển thị dữ liệu. Sử dụng các hàm truy cập để kiểm tra tính đúng hoặc sai của các điều kiện – các hàm như thế thường được gọi là các hàm khẳng định (predicate functions). Một ví dụ của hàm khẳng định là một hàm IsEmpty() của lớp container - một lớp có khả năng giữ nhiều đối tượng - giống như một danh sách liên kết, một stack hay một hàng đợi. Một chương trình sẽ kiểm tra hàm IsEmpty() trước khi thử đọc mục khác từ đối tượng container.

Một hàm tiện ích không là một phần của một giao diện của lớp. Hơn nữa nó là một hàm thành viên private mà hỗ trợ các thao tác của các hàm thành viên public. Các hàm tiện ích không dự định được sử dụng bởi các client của lớp.

Ví dụ 3.6:   Minh họa cho các hàm tiện ích.

1: #include<iostream.h>

2: #include<iomanip.h>

3:

4: class SalesPerson

5: {

6: public:

7: SalesPerson();             //constructor

8: void SetSales(int, double);//Ng.dùng cung cấp các hình của

9: #9; #9; //những hàng bán của một tháng

10: void PrintAnnualSales();

11:

12: private:

13: double Sales[12]; //12 hình của những hàng bán hằng tháng

14: double TotalAnnualSales(); //Hàm tiện ích

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask