You are on page 1of 8

[C/C++] Tch chng trnh ln thnh cc file ring

Ti sao cn phi tch


Khi lp trnh vi bt c ngn ng no th cc bn cng u gp phi mt vn l khi chng trnh
bt u ln th s kh qun l m ngun. l lc bn mun chia chng thnh cc file ring, mi
file lm mt nhim v ring v khi cn th include chng vo chng trnh.
Trong C++ hoc cc ngn ng lp trnh hng i tng nh Java, C#, chng ta cn mun chia
mi lp vo mt file ring, nh vy s tin hn thay v tng tt c cc lp vo mt ch.
Vic tch ra cng gip bn d dng hn trong vic ti s dng m ngun, thay v mi ln ti s
dng bn phi copy-paste m ngun th by gi bn ch cn include nhng file no cn s dng
l c th dng.
Chng trnh khi cha tch
V d sau y l mt chng trnh n gin (c ti thit k theo m hnh MVC ti s c mt
bi vit v MVC dp khc) cho php thit lp gi tr cho mt bin, tng, gim gi tr bin v
hin th gi tr bin ra mn hnh.
1 /* MVC example */
2 #include <iostream>
3 using namespace std;
4
5 class Model {
6
int value;
7
public:
8
Model() { value = 0; }
9
int getValue() { return value; }
10
void setValue(int);
11
void decrease() { value--; }
12
void increase() { value++; }
13 };
14
15
16
17
18
19
20
21
22
23
24
25
26

void Model::setValue(int value) {


this->value = value;
}
class View {
Model *model;
void showValue();
public:
View(Model *);
void showMenu();
void process();
};

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

View::View(Model *model) {
this->model = model;
}
void View::showValue() {
if (model != NULL) {
cout << "n============";
cout << "n====" << model->getValue() << "====";
cout << "n============n";
}
}
void View::showMenu() {
cout << "n------------------------n";
cout << "1 - Setn";
cout << "2 - Increasen";
cout << "3 - Decreasen";
cout << "4 - Exitn";
cout << "Choose: ";
}
void View::process() {
int choice;
do {
showMenu();
cin >> choice;
switch (choice) {
case 1:
cout << "Enter a integer value: ";
int value;
cin >> value;
model->setValue(value);
showValue();
break;
case 2:
model->increase();
cout << "Increasing successful!";
showValue();
break;
case 3:
model->decrease();
cout << "Decreasing successful!";
showValue();
break;
case 4:
break;
default:

74
75
76
77
78 }
79
80 int
81
82
83
84
85
86 }

cout << "Please choose number form 1 to 4!n";


break;
}
} while (choice != 4);
main() {
Model* model = new Model;
View view(model);
view.process();
delete model;
return 0;

Cc bn save file ny vi tn l main.cpp ri bin dch bng g++ v chy nh sau:


1 $ g++ main.cpp
2 $ ./a.out
Nh cc bn thy, chng trnh ny c 2 lp l Model v View v u c nh ngha v ci t
trong mt file duy nht. Mc d chng trnh n gin nhng do tt c nm trong cng mt file
nn vn cm thy ri ri th no ng khng no.
Bt u tch
By gi ti s bt u tch chng trnh ny ra mi lp t trn mi file ring. C th th sau
khi tch ti s c c 3 file chnh l: main.cpp, Model.cpp (cha d liu) v View.cpp (m nhn
vic hin th).
Header file (.h)
bt u tch, bn cn s dng header file, header file cho php bn nh ngha cc thnh phn
ca chng trnh cc file ring, v khi cn s dng li th c th gi d dng bng cch include
vo chng trnh nh sau:
1 #include "filename.h"
Vic include ny thc cht l nhng ton b ni dung ca filename.h vo chng trnh hin c.
y, ti s to 2 header file l Model.h v View.h khai bo cc lp Model v View:
Model.h (Khai bo chng trnh con cho Model.cpp)
1 #ifndef MODEL_H
2 #define MODEL_H
3 class Model {
4
int value;
5
6
public:
7
Model();

8
9
10
11
12 };
13 #endif

void setValue(int);
int getValue();
void increase();
void decrease();

View.h (Khai bo chng trnh con cho View.cpp)


1 #ifndef VIEW_H
2 #define VIEW_H
3
4 #include "Model.h"
5 class View {
6
Model *model;
7
8
public:
9
View(Model *);
10
void showValue();
11
void showMenu();
12
void process();
13 };
14 #endif
Nh cc bn thy, file Model.h khai bo lp Model, file View.h khai bo lp View. Trong lp
View c s dng con tr Model *model cho nn cn phi include header Model.h vo nh dng
th 4: #include Model.h.
Cc bn c th thy 2 dng u tin v dng cui cng ca mi header file trng l l. l include
guard, n c dng m bo rng mt header file ch c include mt ln trong chng trnh,
nu header file c include nhiu hn 1 ln th s xy ra tnh trng mt lp, bin, nh ngha
ri li c nh ngha ln na v chng trnh s bo li: previous definition.
S dng include guard rt n gin, ch cn to header file ca bn vi cu trc nh sau:
1
2
3
4

#ifndef FILENAME_H
#define FILENAME_H
// t code ca bn vo y
#endif

Nhng lu khi s dng header file:


Lun lun s dng include guard.
Ch khai bo ch khng ci t gi tr cho bin, tr trng hp n l hng.
Ch khai bo ch khng ci t hm trong header file (nn ci t hm file .cpp ring) gip
chng trnh d c hn.
Mi header file nn lm mt nhim v ring.

C gng hn ch include cc header file khc trong header file ca bn.


(file header ch m nhn vic khai bo, ging nh phi khai bo chng trnh con trc khi vit
n sau hm main).

To file ci t cho cc lp trong header file


Nh ni trn, header file ch nn m nhn vic khai bo, cn vic ci t c t vo mt
file .cpp ring.
y ti to file Model.cpp ci t cho lp c nh ngha trong Model.h: (lu l bn c th
t tn file .cpp l g cng c nhng nn t ging vi file header m n ci t cho tin qun
l).
Model.cpp (Vit chng trnh con khi c khai bo bi Model.h)
1 #include "Model.h"
2
3 Model::Model() {
4
value = 0;
5 }
6
7 void Model::setValue(int value) {
8
this->value = value;
9 }
10
11 int Model::getValue() {
12
return value;
13 }
14
15 void Model::increase() {
16
value++;
17 }
18
19 void Model::decrease() {
20
value--;
21 }
Tng t i vi file View.h cng c file View.cpp ci t:
View.cpp (Vit chng trnh con khi c khai bo bi View.h)
1 #include <iostream>
2 #include "Model.h"
3 #include "View.h"
4 using namespace std;
5
6 View::View(Model *model) {
7
this->model = model;
8 }

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

void View::showValue() {
cout << "===============n";
cout << "====" << model->getValue() << "====n";
cout << "================n";
}
void View::showMenu() {
cout << "n==============n";
cout << "0 - Show valuen";
cout << "1 - Set valuen";
cout << "2 - Increasen";
cout << "3 - Decreasen";
cout << "4 - Exitn";
cout << "Choose: ";
}
void View::process() {
int choice = -1;
do {
showMenu();
cin >> choice;
switch (choice) {
case 0:
showValue();
break;
case 1:
cout << "Enter a integer number: ";
int tmp;
cin >> tmp;
model->setValue(tmp);
showValue();
break;
case 2:
model->increase();
showValue();
break;
case 3:
model->decrease();
showValue();
break;
case 4:
break;
default:
cout << "Please choose from 0 to 4!n";
break;
}

56
57 }

} while (choice != 4);

S dng code cc file ring


Nh ni trn, ti s dng code cc file ring cc bn ch cn include header file vo
chng trnh.
y ti c file main.cpp s dng 2 lp Model v View hon thin thnh mt chng trnh nn
ti include Model.h v View.h vo:
main.cpp (Vit chng trnh chnh, trong khi thc hin s gi cc chng trnh con trong
Model.cpp v View.cpp vo)
1 /* Seprate class in file */
2 #include <iostream>
3 #include "Model.h"
4 #include "View.h"
5 using namespace std;
6
7 int main() {
8
Model *model = new Model();
9
View view(model);
10
view.process();
11
return 0;
12 }
Bin dch chng trnh
Khc vi trng hp chng trnh ch c mt file, cc bn ch cn g: g++ filename.cpp l xong.
Vi chng trnh c nhiu file nu cc bn thc hin cch bin dch tng t th s khng thnh
cng, v d nu ti bin dch mi file main.cpp th s gp li nh sau:
1 $ g++ main.cpp
2 /tmp/ccPzJkJB.o: In function `main':
3 main.cpp:(.text+0x1d): undefined reference to `Model::Model()'
4 main.cpp:(.text+0x35):undefined reference
to`View::View(Model*)'
5 main.cpp:(.text+0x41): undefined reference to `View::process()'
6 collect2: ld returned 1 exit status
Vy phi bin dch th no? Cng n gin thi, cc bn bin dch nh sau:
1 $ g++ Model.cpp View.cpp main.cpp
hoc cng c th bin dch tng file ra object file ri s dng object file bin dch tip:
1 $ g++ -c Model.cpp # c object file Model.o
2 $ g++ -c View.cpp # c object file View.o
3 $ g++ -c main.cpp # c object file main.o

4 $ g++ main.o Model.o View.o # c file thc thi a.out


hoc cng c th to Makefile nh sau:
1 all : main.o Model.o View.o
2
g++ main.o Model.o View.o
3 main.o : main.cpp
4
g++ -c main.cpp
5 Model.o : Model.cpp
6
g++ -c Model.cpp
7 View.o : View.cpp
8
g++ -c View.cpp
9 clean :
10
rm *.o ; rm a.out
Ri bin dch bng cch gi:
1 $ make
dn dp folder, gi:
1 $ make clean
Cui cng, chy chng trnh ta g:
1 $ ./a.out

You might also like