<< Chapter < Page Chapter >> Page >

Ví dụ 3.14: C.trình sau sử dụng một bộ khởi tạo thành viên để khởi tạo một hằng của kiểu dữ liệu có sẵn.

2: #include<iostream.h>

3: class IncrementClass

4: {

5: public:

6: IncrementClass (int C = 0, int I = 1);

7: void AddIncrement()

8: {

9: Count += Increment;

10: }

11: void Print() const;

12: private:

13: int Count;

14: const int Increment; //Thành viên dữ liệu const

15: };

16:

17: //Constructor của lớp IncrementClass

18: //Bộ khởi tạo với thành viên const

19: IncrementClass::IncrementClass (int C, int I) : Increment(I)

20: {

21: Count = C;

22: }

23:

24: //In dữ liệu

25: void IncrementClass::Print() const

26: {

27: cout<<"Count = "<<Count

28: #   #  <<", Increment = "<<Increment<<endl;

30: }

31:

32: int main()

33: {

34: IncrementClass Value(10, 5);

35:

36: cout<<"Before incrementing: ";

37: Value.Print();

38: for (int J = 1; J<= 3; J++)

40: {

41: Value.AddIncrement();

42: cout<<"After increment "<<J<<": ";

43: Value.Print();

44: }

45: return 0;

46: }

Chương trình này sử dụng cú pháp bộ khởi tạo thành viên để khởi tạo thành viên dữ liệu const Increment của lớp IncrementClass ở dòng 19.

Chúng ta chạy ví dụ 3.14 , kết quả ở hình 3.14

Hình 3.14: Kết quả của ví dụ 3.14

Ký hiệu : Increment(I) (ở dòng 19 của ví dụ 3.14) sinh ra Increment được khởi động với giá trị là I. Nếu nhiều bộ khởi tạo thành viên được cần, đơn giản bao gồm chúng trong danh sách phân cách dấu phẩy sau dấu hai chấm. Tất cả các thành viên dữ liệu có thể được khởi tạo sử dụng cú pháp bộ khởi tạo thành viên.

Nếu trong ví dụ 3.14 chúng ta cố gắng khởi tạo Increment với một lệnh gán hơn là với một bộ khởi tạo thành viên như sau:

IncrementClass::IncrementClass (int C, int I)

{Count = C;

Increment = I;

}

Khi đó trình biên dịch (Borland C++) sẽ có thông báo lỗi như sau:

Hình 3.15: Thông báo lỗi khi cố gắng khởi tạo một thành viên dữ liệu const bằng phép gán

Lớp như là các thành viên của các lớp khác

Một lớp có thể có các đối tượng của các lớp khác như các thành viên. Khi một đối tượng đi vào phạm vi, constructor của nó được gọi một cách tự động, vì thế chúng ta cần mô tả các tham số được truyền như thế nào tới các constructor của đối tượng thành viên. Các đối tượng thành viên được xây dựng theo thứ tự mà trong đó chúng được khai báo (không theo thứ tự mà chúng được liệt kê trong danh sách bộ khởi tạo thành viên của constructor) và trước các đối tượng của lớp chứa đựng chúng được xây dựng.

Ví dụ 3.15: Chương trình sau minh họa các đối tượng như các thành viên của các đối tượng khác.

1: #include<iostream.h>

2: #include<string.h>

3:

4: class Date

5: {

6: public:

7: Date(int = 1, int = 1, int = 1900); //Constructor mặc định

8: void Print() const; //In ngày theo dạng Month/Day/Year

9: private:

10: int Month; //1-12

11: int Day; //1-31

12: int Year; //Năm bất kỳ

13://Hàm tiện ích để kiểm tra Day tương thích đối với Month và Year

14: int CheckDay(int);

15: };

16:

17: class Employee

18: {

19: public:

20: Employee(char *, char *, int, int, int, int, int, int);

21: void Print() const;

22: private:

23: char LastName[25];

24: char FirstName[25];

25: Date BirthDate;

26: Date HireDate;

27: };

Questions & Answers

the diagram of the digestive system
Assiatu Reply
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
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