You are on page 1of 11

Mn PPLTHT

Hng dn thc hnh tun 5

Mc ch
Tm hiu v thnh phn tnh ca lp i tng, gii thiu th vin STL vi cc lp string
v vector, lm bi tp p dng.

Ni dung
- Thnh phn tnh.
- Th vin STL - string.
- Th vin STL vector.
- Bi tp.

Yu cu
Nm vng nhng ni dung c trnh by trong cc bi hng dn thc hnh t tun 1
n tun 3.

1
Hng dn thc hnh tun Thnh phn
5 tnh

1. Thnh phn tnh


Cc thnh phn tnh (static members) ca mt lp i tng bao gm cc thuc tnh tnh
(static attributes) v cc phng thc tnh (static methods). Thnh phn tnh c khai
bo bt u bng t kha static.

Thuc tnh tnh


Cc thuc tnh tnh c xem nh l cc thuc tnh ca lp (class variables) bi v chng
ch c mt gi tr duy nht cho tt c cc i tng thuc lp . Gi tr ca thuc tnh
tnh l nh nhau tt c cc i tng.

V d chng ta c th dng thuc tnh tnh m s lng i tng c to ra ca


mt lp.
#include <iostream>

using namespace std;

class PhanSo
{
private:
int m_iTuSo;
int m_iMauSo;

1
Hng dn thc hnh tun Thnh phn
5 tnh
public:
static int m_iNumberOfInstances; // Thuoc tinh ti~nh luu so luong doi tuong.

PhanSo()
{
m_iNumberOfInstances++;
}

virtual ~PhanSo()
{
m_iNumberOfInstances--;
}
};

int PhanSo::m_iNumberOfInstances = 0; // Khoi tao gia tri ban dau.

void main()
{
PhanSo a; // m_iNumberOfInstances = 1.
PhanSo b[5]; // m_iNumberOfInstances = 6.
PhanSo *c;

2
c = new PhanSo; // m_iNumberOfInstances = 7.

cout << a.m_iNumberOfInstances << endl;


delete c; // m_iNumberOfInstances = 6.
cout << PhanSo::m_iNumberOfInstances << endl;
}

Trong on chng trnh trn, m_iNumberOfInstances l thuc tnh tnh ca lp PhanSo.


Gi tr ca n l nh nhau tt c cc i tng ca lp PhanSo. Cn gi tr ca
m_iTuSo v m_iMauSo l khc nhau gia cc i tng PhanSo. Trong hm dng ca
lp PhanSo, chng ta tng bin m m_iNumberOfInstances ln 1. Trong hm hy,
chng ta gim bin m i 1.

Chng ta c 2 cch truy xut vo thnh phn tnh ca mt lp i tng:


- S dng tn lp: PhanSo::m_iNumberOfInstances.
- S dng tn i tng: a.m_iNumberOfInstances.

Cc thuc tnh tnh c tnh cht nh cc bin ton cc ngoi tr vic n c khai bo
trong mt lp i tng no .

Phng thc tnh


Cc phng thc tnh c xem nh cc phng thc ca lp (class methods). Chng ta
dng phng thc tnh vit li v d m s lng i tng c to ra ca mt lp.
#include <iostream>

using namespace std;

class PhanSo
{
private:
int m_iTuSo;
int m_iMauSo;

static int m_iNumberOfInstances; // Thuoc tinh ti~nh luu so luong doi tuong.
public:
// Phuong thuc tinh tra ve so luong doi tuong.
static int GetNumberOfInstances()
{
return m_iNumberOfInstances;
}

PhanSo()
{
m_iNumberOfInstances++;
}

virtual ~PhanSo()
{
m_iNumberOfInstances--;
}
};

int PhanSo::m_iNumberOfInstances = 0; // Khoi tao gia tri ban dau.

void main()
{
PhanSo a; // m_iNumberOfInstances = 1.
PhanSo b[5]; // m_iNumberOfInstances = 6.
PhanSo *c;

c = new PhanSo; // m_iNumberOfInstances = 7.

cout << a.GetNumberOfInstances() << endl;


delete c; // m_iNumberOfInstances = 6.
cout << PhanSo::GetNumberOfInstances() << endl;
}

Trong phng thc tnh, chng ta ch c th truy xut vo cc thuc tnh tnh ca lp ch
khng c php truy xut vo cc thuc tnh ca i tng hoc s dng con tr this.
Hng dn thc hnh tun Th vin STL - string
5

2. Th vin STL - string


Gii thiu
Th vin chun STL (Standard Template Library) ca C++ c h tr kiu string cng
vi cc php ton v phng thc kh tin li cho ngi lp trnh. Hin ti th Visual
C++ v hu ht cc trnh bin dch C++ trn Linux/Unix u c sn th vin STL nn ta
c th dng kiu string. Ring cc phin bn hin nay ca Borland C++ th dng th vin
template ring m khng bao gm th vin STL.

Chng trnh sau y cho thy vic s dng kiu string nh vo dng th vin STL kh
n gin v tin li.
#include <iostream.h>
#include <string>

using namespace std;

void Sort(int n, string str[]);

void main()
{
string str1("012");
string str2("345");
string s = str1 + str2;

cout << "Result :" << s << endl;

string country[] = {"Viet nam", "Lao", "Campuchia", "Thai lan", "Trung quoc"};
int n = 5;

Sort(n, country);
cout << "After sorting" << endl;
for (int i = 0;i < n; i++)
cout << "\t"<< country[i] << endl;
}

void Sort(int n, string str[])


{
for (int i = 0; i < n - 1; i++)
for (int j = i + 1;j < n;j++)
if (str[i] > str[j])
{
string s = str[i];
str[i] = str[j];
str[j] = s;
}
Hng dn thc hnh tun Th vin STL - string
5
}

Trong chng trnh trn, chng ta cn ch cc im sau:


- Cc ch th include v using namespace cn thit khai bo s dng kiu
string.
- C th khi ng v sao chp gi tr nh php gn =, c th dng php + ghp
chui.
- Trong hm strSort() ta c th dng php so snh > so snh 2 bin chui.
- C th dng ton t << vi cout xut mt chui ra mn hnh hoc dng ton t
>> vi cin nhp mt chui k t n khi gp mt khong trng th dng.

Cc phng thc/php ton tin ch ca kiu string


Cc php ton v phng thc c bn
- Php cng + dng ghp hai chui v cng ghp mt k t vo chui.
- Cc php so snh theo th t t in : == , !=,>,>=,<.<=.
- Phng thc length() v php ly ch s, ton t [ ] duyt tng k t ca
chui: nu s l bin kiu string th s[i] l k t th i ca s vi 0 <= i < s.length().
- Php gn = dng gn bin kiu string bng mt hng chui.
{
char st[]="ABCDEF";
string s = "XYZ";

cout << s << endl;


s = st;
cout << s.length() << ":" << s << endl;
}

Cc phng thc chn, xo, ly chui con


- Ly chui con: substr(int pos, int nchar)
V d: str.substr(2,4) tr v chui con gm 4 k t ca str k t v
tr th 2.
- Chn thm k t hay chui vo mt v tr no ca chui str. C nhiu cch
dng:
str.insert(int pos, char* s): chn s (kt thc bng \0) v v tr
pos ca str.
str.insert(int pos, string s): chn chuii s vo v tr pos ca chui
str.
str.insert(int pos, int n, int ch): chn n ln k t ch vo v tr pos
ca str.
- Xo erase(int pos, int n): xo n k t ca chui str k t v tr pos, nu khng
quy nh gi tr n th tt c cc k t ca str t v tr pos tr i u b xo.
Hng dn thc hnh tun Th vin STL - string
5
Cc phng thc tm kim v thay th:
- Phng thc find() tm kim xem mt k t hay mt chui no c xut hin
trong mt chui str cho trc hay khng. C nhiu cch dng phng thc ny:
str.find(int ch, int pos = 0): tm k t ch k t v tr pos n cui
chui str.
str.find(char* s, int pos = 0): tm chui con s k t v tr pos n
cui chui str.
str.find(string s,int pos=0): tm chui s k t v tr pos n cui
chui str.
Nu khng qui nh gi tr pos th hiu mc nhin l 0, nu tm c
kt qu th phng thc tr v v tr xut hin u tin, ngc li
tr v gi tr -1.
- Phng thc replace() thay th mt on con trong chui str cho trc (on con
k t mt v tr pos v m ti nchar k t v pha cui chui) bi mt chui s
no hoc bi n k t ch no . C nhiu cch dng, th t tham s nh sau:
str.replace(int pos, int nchar, char *s)
str.replace(int pos, int nchar, string s)
str.replace(int pos, int nchar, int n, int ch)

Mt s phng thc khc


Cn nhiu phng thc tin ch khc nh: append(), rfind(), find_first_not_of(),
find_last_not_of(), swap(), c_str(). Cch dng cc hm ny u c trnh by trong h
thng hng dn (help) ca cc mi trng c h tr STL (trong VC++ l MSDN).
Ngoi ra cc phng thc nh find_first_of() tng t nh find(), find_last_of() tng
t nh rfind().
Hng dn thc hnh tun Th vin STL - vector
5

3. Th vin STL vector


Lp mng ng vector<T> c sn trong th vin chun STL ca C++ cho php nh
ngha mt mng ng cc phn t kiu T, c th xc nh kch thc mng vo lc ang
chy chng trnh, c th thay i kch thc mng khi cn thit.

vector<T>::size() Tr v kch thc hin hnh ca mng.


vector<T>::resize(int iNewSize) Thay i kch thc mng.
T & vector<T>::operator [](int iIndex) Tham chiu n phn t v tr iIndex trong
mng (0 <= iIndex <= this.size()).

Chng hn on chng trnh sau khai bo v nhp vo n = 10 s thc:


#include <iostream>
#include <vector>

using namespace std;

void main()
{
int n = 10;
vector<float> a(n);

for (int i = 0; i < a.size(); i++)


{
cout << "a[" << i << "] = ";
cin >> a[i];
}

cout << "After entering data" << endl;

for (i = 0; i < a.size(); i++)


cout << a[i] << endl;
}

Trong on chng trnh trn, kch thc ca mng a c xc nh bi bin n.

Trng hp xc nh kch thc mng khi chng trnh ang chy, chng ta dng hm
dng mc nh (default constructor) khai bo mng cha xc nh kch thc, sau
dng phng thc resize() xc nh kch thc ca mng khi cn. Chng trnh sau
y nhp vo n t (word) mi t l mt chui kiu string:
#include <iostream>
#include <string>
#include <vector>

using namespace std;


Hng dn thc hnh tun Bi
5 tp
void main()
{
int iWordNum;
vector<string> arrWords;

cout << "Enter number of words = ";


cin >> iWordNum;

if (iWordNum <= 0)
{
cout << "Fail!";
return;
}

arrWords.resize(iWordNum);

for (int i = 0; i < arrWords.size(); i++)


{
cout << "Enter word " << i << " = ";
cin >> arrWords[i];
}

cout << "After entering data..." << endl;

for (i = 0; i < arrWords.size(); i++)


cout << arrWords[i] << endl;
}