<< Chapter < Page Chapter >> Page >

1: #include<iostream.h>

2:

3: class Test

4: {

5: public:

6: Test(int = 0); // Constructor mặc định

7: void Print() const;

8: private:

9: int X;

10: };

11:

12: Test::Test(int A)

13: {

14: X = A;

15: }

16:

17: void Test::Print() const

18: {

19: cout<<" X = "<<X<<endl

20:      <<" this->X = "<<this->X<<endl

21:      <<"(*this).X = "<<(*this).X<<endl;

22: }

23:

24: int main()

25: {

26: Test A(12);

27:

28: A.Print();

29: return 0;

30: }

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

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

Một cách khác sử dụng con trỏ this là cho phép móc vào nhau các lời gọi hàm thành viên.

Ví dụ 3.18: Chương trình sau minh họa trả về một tham chiếu tới một đối tượng Time để cho phép các lời gọi hàm thành viên của lớp Time được móc nối vào nhau.

1: #include<iostream.h>

2:

3: class Time

4: {

5: public:

6: Time(int = 0, int = 0, int = 0); // Constructor mặc định

7: // Các hàm set

8: Time&SetTime(int, int, int); // Thiết lập Hour, Minute va Second

9: Time&SetHour(int); // Thiết lập Hour

10: Time&SetMinute(int); // Thiết lập Minute

11: Time&SetSecond(int); // Thiết lập Second

12: // Các hàm get

13: int GetHour() const; // Trả về Hour

14: int GetMinute() const; // Trả về Minute

15: int GetSecond() const; // Trả về Second

16: // Các hàm in

17: void PrintMilitary() const; // In t.gian theo dạng giờ quân đội

18: void PrintStandard() const; // In thời gian theo dạng giờ chuẩn

19: private:

20: int Hour; // 0 - 23

21: int Minute; // 0 - 59

22: int Second; // 0 - 59

23: };

24:

25: // Constructor khởi động dữ liệu private

26: // Gọi hàm thành viên SetTime() để thiết lập các biến

27: // Các giá trị mặc định là 0

28: Time::Time(int Hr, int Min, int Sec)

29: {

30: SetTime(Hr, Min, Sec);

31: }

32:

33: // Thiết lập các giá trị của Hour, Minute, và Second

34: Time&Time::SetTime(int H, int M, int S)

35: {

36: Hour = (H>= 0&&H<24) ? H : 0;

37: Minute = (M>= 0&&M<60) ? M : 0;

38: Second = (S>= 0&&S<60) ? S : 0;

39: return *this; // Cho phép móc nối

40: }

41:

42: // Thiết lập giá trị của Hour

43: Time&Time::SetHour(int H)

44: {

45: Hour = (H>= 0&&H<24) ? H : 0;

46: return *this; // Cho phép móc nối

47: }

48:

49: // Thiết lập giá trị của Minute

50: Time&Time::SetMinute(int M)

51: {

52: Minute = (M>= 0&&M<60) ? M : 0;

53: return *this; // Cho phép móc nối

54: }

55:

56: // Thiết lập giá trị của Second

57: Time&Time::SetSecond(int S)

58: {

59: Second = (S>= 0&&S<60) ? S : 0;

60: return *this; // Cho phép móc nối

61: }

62:

63: // Lấy giá trị của Hour

64: int Time::GetHour() const

65: {

66: return Hour;

67: }

68:

69: // Lấy giá trị của Minute

70: int Time::GetMinute() const

71: {

72: return Minute;

73: }

74:

75: // Lấy giá trị của Second

76: int Time::GetSecond() const

77: {

78: return Second;

79: }

80:

81: // Hiển thị thời gian dạng giờ quân đội: HH:MM:SS

82: void Time::PrintMilitary() const

83: {

84: cout<<(Hour<10 ? "0" : "")<<Hour<<":"

85:      <<(Minute<10 ? "0" : "")<<Minute<<":"

86:      <<(Second<10 ? "0" : "")<<Second;

87: }

88:

89: // Hiển thị thời gian dạng chuẩn: HH:MM:SS AM (hay PM)

90: void Time::PrintStandard() const

91: {

92: cout<<((Hour == 0 || Hour == 12) ? 12 : Hour % 12)<<":"

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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