<< Chapter < Page Chapter >> Page >


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: }


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};


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: }


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

64: void Date::Print() const

65: {

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

67: }


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);


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: }


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: }


98: int main()

99: {

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


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

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
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
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
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.
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
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
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
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?