<< Chapter < Page Chapter >> Page >

28:

29: //Constructor: xác nhận giá trị tương thích của Month

30: //Gọi hàm CheckDay() để xác nhận giá trị tương thích của Day

31: Date::Date(int Mn, int Dy, int Yr)

32: {

33: if (Mn>0&&Mn<= 12)

34: Month = Mn;

35: else

36: {

37: Month = 1;

38: cout<<"Month "<<Mn<<" invalid. Set to Month 1."

39:           <<endl;

40: }

41: Year = Yr;

42: Day = CheckDay(Dy);

43: cout<<"Date object constructor for date ";

44: Print();

45: cout<<endl;

46: }

47:

48: //Hàm xác nhận giá trị Day tương thích đưa vào Month và Year

49: int Date::CheckDay(int TestDay)

50: {

51: static int DaysPerMonth[13] = {0, 31, 28, 31, 30, 31,

52: 9; 9; 9; 9; 9; 9; #   #   #   #   30, 31, 31, 30,31, 30, 31};

53:

54: if (TestDay>0&&TestDay<= DaysPerMonth[Month])

55: return TestDay;

56: if (Month == 2&&TestDay == 29&&

57: ; ; (Year % 400 == 0 || (Year % 4 == 0&&Year % 100 != 0)))

58: return TestDay;

59: cout<<"Day "<<TestDay<<"invalid. Set to Day 1."<<endl;

60: return 1;

61: }

62:

63: //In đối tượng Date dạng Month/Day/Year

64: void Date::Print() const

65: {

66: cout<<Month<<'/'<<Day<<'/'<<Year;

67: }

68:

69: Employee::Employee(char *FName, char *LName,

70:             int BMonth, int BDay, int BYear,

71:             int HMonth, int HDay, int HYear)

72: :BirthDate(BMonth, BDay, BYear), HireDate(HMonth, HDay, HYear)

73: {

74://Sao chép FName vào FirstName và phải chắc chắn rằng nó phù hợp

75: int Length = strlen(FName);

76:

77: Length = Length<25 ? Length : 24;

78: strncpy(FirstName, FName, Length);

79: FirstName[Length] = '\0';

80: //Sao chép LName vào LastName và phải chắc chắn rằng nó phù hợp

81: Length = strlen(LName);

82: Length = Length<25 ? Length : 24;

83: strncpy(LastName, LName, 24);

84: LastName[Length] = '\0';

85: cout<<"Employee object constructor: "

86:      <<FirstName<<' '<<LastName<<endl;

87: }

88:

89: void Employee::Print() const

90: {

91: cout<<LastName<<", "<<FirstName<<endl<<"Hired: ";

92: HireDate.Print();

93: cout<<" Birthday: ";

94: BirthDate.Print();

95: cout<<endl;

96: }

97:

98: int main()

99: {

100: Employee E("Bob", "Jones", 7, 24, 49, 3, 12, 88);

101:

102 cout<<endl;

103: E.Print();

104: cout<<endl<<"Test Date constructor with invalid values:"

105:      <<endl;

106: Date D(14, 35, 94); //Các giá trị Date không hợp lệ

107: return 0;

108: }

Chương trình gồm lớp Employee chứa các thành viên dữ liệu private LastName, FirstName, BirthDate và HireDate. Các thành viên BirthDate và HireDate là các đối tượng của lớp Date mà chứa các thành viên dữ liệu private Month, Day và Year. Chương trình khởi tạo một đối tượng Employee, và các khởi tạo và các hiển thị các thành viên dữ liệu của nó. Chú ý về cú pháp của phần đầu trong định nghĩa constructor của lớp Employee:

Employee::Employee(char *FName, char *LName, int BMonth, int BDay, int BYear,

int HMonth, int HDay, int HYear)

:BirthDate(BMonth, BDay, BYear), HireDate(HMonth, HDay, HYear)

Constructor lấy tám tham số (FName, LName, BMonth, BDay, BYear, HMonth, HDay, và HYear). Dấu hai chấm trong phần đầu phân tách các bộ khởi tạo từ danh sách các tham số. Các bộ khởi tạo định rõ các tham số truyền chon constructor của các đối tượng thành viên. Vì thế BMonth, BDay và BYear được truyền cho constructor của đối tượng BirthDate, và HMonth, HDay, và HYear được truyền cho constructor của đối tượng HireDate. Nhiều bộ khởi tạo được phân tách bởi dấu phẩy.

Questions & Answers

where we get a research paper on Nano chemistry....?
Maira Reply
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
Google
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
revolt
da
Application of nanotechnology in medicine
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
Nasa has use it in the 60's, copper as water purification in the moon travel.
Alexandre
nanocopper obvius
Alexandre
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
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
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
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