<< Chapter < Page Chapter >> Page >

1: #include<iostream.h>

2: #include<string.h>

3: #include<assert.h>

4:

5: class Employee

6: {

7: public:

8: Employee(const char*, const char*); // Constructor

9: ~Employee(); // Destructor

10: char *GetFirstName() const; // Trả về first name

11: char *GetLastName() const; // Trả về last name

12: // Hàm thành viên tĩnh

13: static int GetCount(); // Trả về số đối tượng khởi tạo

14: private:

15: char *FirstName;

16: char *LastName;

17: // static data member

18: static int Count; // Số đối tượng khởi tạo

19: };

20:

21: // Khởi tạo thành viên dữ liệu tĩnh

22: int Employee::Count = 0;

23:

24://Định nghĩa hàm thành viên tỉnh mà trả về số đối tượng khởi tạo

25: int Employee::GetCount()

26: {

27: return Count;

28: }

29:

30: // Constructor cấp phát động cho first name và last name

31: Employee::Employee(const char *First, const char *Last)

32: {

33: FirstName = new char[ strlen(First) + 1 ];

34: assert(FirstName != 0); // Bảo đảm vùng nhớ được cấp phát

35: strcpy(FirstName, First);

36: LastName = new char[ strlen(Last) + 1 ];

37: assert(LastName != 0); // Bảo đảm vùng nhớ được cấp phát

38: strcpy(LastName, Last);

39: ++Count; // Tăng số đối tượng lên 1

40: cout<<"Employee constructor for "<<FirstName

41:      <<' '<<LastName<<" called."<<endl;

42: }

43:

44: // Destructor giải phóng vùng nhớ đã cấp phát

45: Employee::~Employee()

46: {

47: cout<<"~Employee() called for "<<FirstName

48:      <<' '<<LastName<<endl;

49: delete FirstName;

50: delete LastName;

51: --Count; // Giảm số đối tượng xuống 1

52: }

53:

54: // Trả về first name

55: char *Employee::GetFirstName() const

56: {

57: char *TempPtr = new char[strlen(FirstName) + 1];

58: assert(TempPtr != 0); // Bảo đảm vùng nhớ được cấp phát

59: strcpy(TempPtr, FirstName);

60: return TempPtr;

61: }

62:

63: // Trả về last name

64: char *Employee::GetLastName() const

65: {

66: char *TempPtr = new char[strlen(LastName) + 1];

67: assert(TempPtr != 0); // Bảo đảm vùng nhớ được cấp phát

68: strcpy(TempPtr, LastName);

69: return TempPtr;

70: }

71:

72: int main()

73: {

74: cout<<"Number of employees before instantiation is "

75:      <<Employee::GetCount()<<endl; // Sử dụng tên lớp

76: Employee *E1Ptr = new Employee("Susan", "Baker");

77: Employee *E2Ptr = new Employee("Robert", "Jones");

78: cout<<"Number of employees after instantiation is "

79:      <<E1Ptr->GetCount()<<endl;

80: cout<<endl<<"Employee 1: "

81:      <<E1Ptr->GetFirstName()

82:      <<" "<<E1Ptr->GetLastName()

83:      <<endl<<"Employee 2: "

84:      <<E2Ptr->GetFirstName()

85:      <<" "<<E2Ptr->GetLastName()<<endl<<endl;

86: delete E1Ptr;

87: delete E2Ptr;

88: cout<<"Number of employees after deletion is "

89:      <<Employee::GetCount()<<endl;

90: return 0;

91: }

Thành viên dữ liệu Count được khởi tạo là zero ở phạm vi file với lệnh:

int Employee::Count = 0;

Thành viên dữ liệu Count duy trì số các đối tượng của lớp Employee đã được khởi tạo. Khi đối tượng của lớp Employee tồn tại, thành viên Count có thể được tham chiếu thông qua bất kỳ hàm thành viên nào của một đối tượng Employee – trong ví dụ này, Count được tham chiếu bởi cả constructor lẫn destructor. Khi các đối tượng của lớp Employee không tồn tại, thành viên Count có thể vẫn được tham chiếu nhưng chỉ thông qua một lời gọi hàm thành viên tĩnh public GetCount() như sau:

Questions & Answers

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 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
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
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
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
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.
Tarell
what is the actual application of fullerenes nowadays?
Damian
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.
Tarell
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.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
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.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
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