<< 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 biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
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