<< Chapter < Page Chapter >> Page >

15: };

16:

17: //Hàm constructor khởi tạo mảng

18: SalesPerson::SalesPerson()

19: {

20: for (int I = 0; I<12; I++)

21: Sales[I] = 0.0;

22: }

23:

24://Hàm th.lập một trong 12 hình của những hàng bán hằng tháng

25: void SalesPerson::SetSales(int Month, double Amount)

26: {

27: if (Month>= 1&&Month<= 12&&Amount>0)

28: Sales[Month - 1] = Amount;

29: else

30: cout<<"Invalid month or sales figure"<<endl;

31: }

32:

33: //Hàm tiện íchđể tính tổng hàng bán hằng năm

34: double SalesPerson::TotalAnnualSales()

35: {

36: double Total = 0.0;

37:

38: for (int I = 0; I<12; I++)

39: Total += Sales[I];

40: return Total;

41: }

42:

43: //In tổng hàng bán hằng năm

44: void SalesPerson::PrintAnnualSales()

45: {

46: cout<<setprecision(2)

47:      <<setiosflags(ios::fixed | ios::showpoint)

48:      <<endl<<"The total annual sales are: $"

49:      <<TotalAnnualSales()<<endl;

50: }

51:

52: int main()

53: {

54: SalesPerson S;

55: double salesFigure;

56:

57: for (int I = 1; I<= 12; I++)

58: {

59: cout<<"Enter sales amount for month "<<I<<": ";

60: cin>>salesFigure;

61: S.SetSales(I, salesFigure);

62: }

63: S.PrintAnnualSales();

64: return 0;

65: }

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

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

Khởi động các đối tượng của lớp : constructor

Khi một đối tượng được tạo, các thành viên của nó có thể được khởi tạo bởi một hàm constructor. Một constructor là một hàm thành viên với tên giống như tên của lớp. Lập trình viên cung cấp constructor mà được gọi tự động mỗi khi đối tượng của lớp đó được tạo. Các thành viên dữ liệu của một lớp không thể được khởi tạo trong định nghĩa của lớp. Hơn nữa, các thành viên dữ liệu phải được khởi động hoặc trong một constructor của lớp hoặc các giá trị của chúng có thể được thiết lập sau sau khi đối tượng được tạo. Các constructor không thể mô tả các kiểu trả về hoặc các giá trị trả về. Các constructor có thể được đa năng hóa để cung cấp sự đa dạng để khởi tạo các đối tượng của lớp.

Constructor có thể chứa các tham số mặc định. Bằng cách cung cấp các tham số mặc định cho constructor, ngay cả nếu không có các giá trị nào được cung cấp trong một constructor thì đối tượng vẫn được bảo đảm để trong một trạng thái phù hợp vì các tham số mặc định. Một constructor của lập trình viên cung cấp mà hoặc tất cả các tham số của nó có giá trị mặc định hoặc không có tham số nào được gọi là constructor mặc định (default constructor). Chỉ có thể có một constructor mặc định cho mỗi lớp.

Ví dụ 3.7: Constructor với các tham số mặc định

#include<iostream.H>

class Time

{

public:

Time(int = 0, int = 0, int = 0); //Constructor mac dinh

void SetTime(int, int, int);

void PrintMilitary();

void PrintStandard();

private:

int Hour;

int Minute;

int Second;

};

//Ham constructor de khoi dong du lieu private

//Cac gia tri mac dinh la 0

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

{

SetTime(Hr, Min, Sec);

}

//Thiet lap cac gia tri cua Hour, Minute va Second

//Gia tri khong hop le duoc thiet lap la 0

void Time::SetTime(int H, int M, int S)

{

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

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

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

}

//Hien thi thoi gian theo dang gio quan doi: HH:MM:SS

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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