Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
siniflar

siniflar

Ratings: (0)|Views: 68 |Likes:
Published by api-3832867

More info:

Published by: api-3832867 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

SINIFLAR

S\u0131n\u0131flar, nesne y\u00f6nelimli programlama tekni\u011finin temel yap\u0131 ta\u015f\u0131d\u0131r. Nesne y\u00f6nelimli
programlama tekni\u011fine "s\u0131n\u0131flar\u0131 kullanarak program yazma" tekni\u011fi diyebiliriz. Bu
b\u00f6l\u00fcmde s\u0131n\u0131flara bir giri\u015f yapacak, s\u0131n\u0131flara ili\u015fkin temel kavramlar\u0131 a\u00e7\u0131klayaca\u011f\u0131z.
Bundan sonraki b\u00f6l\u00fcmlerde ise a\u011f\u0131rl\u0131kl\u0131 olarak s\u0131n\u0131flar\u0131n kullan\u0131lmas\u0131 ve s\u0131n\u0131flar \u00fczerinde
yap\u0131lan i\u015flemler \u00fcst\u00fcnde duraca\u011f\u0131z.

S\u0131n\u0131f Nedir

S\u0131n\u0131flar\u0131 \u00f6ncelikle s\u00f6zdizim a\u00e7\u0131s\u0131ndan ele alaca\u011f\u0131z.S \u0131n\u0131 f C++ dilinin, programc\u0131n\u0131n yeni bir t\u00fcr yaratmas\u0131na olanak veren bir arac\u0131d\u0131r. C'de, programc\u0131n\u0131n yeni bir t\u00fcr yaratmas\u0131, yap\u0131 (struct), birlik (union) vee num ara\u00e7lar\u0131yla m\u00fcmk\u00fcn oluyordu. C++ dilinde bu ara\u00e7lara bir de s\u0131n\u0131f (class) eklenmi\u015ftir.

S\u0131n\u0131f( class) nesne y\u00f6nelimli programlama tekni\u011finin uygulanmas\u0131na olanak sa\u011flayan,C
dilinde olmayan yeni bir yaz\u0131l\u0131msal birimdir. S\u0131n\u0131flar,C\u2019deki yap\u0131lara benzetilebilir. Ancak
C\u2019deki yap\u0131lar yaln\u0131zca eleman (member)i \u00e7e ri rke n, C++\u2019da s\u0131n\u0131flar yap\u0131lardan fazla olarak

hem veri eleman\u0131 hem de \u00fcye i\u015flevleri (member function) i\u00e7erir. S\u0131n\u0131flar, yap\u0131lara g\u00f6re ek
bir \u00e7ok \u00f6zelli\u011fe sahiptir. Bu \u00f6zelliklerin \u00e7o\u011fu Nesne Y\u00f6nelimli Programlama Tekni\u011fi\u2019ni
destekleme amac\u0131yla eklenmi\u015ftir.

Nas\u0131l bir "yap\u0131" t\u00fcr\u00fc programc\u0131 taraf\u0131ndan tan\u0131mlanm\u0131\u015f bir t\u00fcr (user defined type) ise,
s\u0131n\u0131flar da programc\u0131n\u0131n tan\u0131mlam\u0131\u015f oldu\u011fu t\u00fcrd\u00fcr. Programc\u0131, \u00f6nce yeni bir t\u00fcr\u00fc
derleyiciye tan\u0131t\u0131r, daha sonra bu yeni t\u00fcrden nesne, g\u00f6sterici, referans tan\u0131mlayabilir.
S\u0131n\u0131flar\u0131 kullanabilmek i\u00e7in ilk yap\u0131lmas\u0131 gereken i\u015flem, bir s\u0131n\u0131f\u0131n bildirimini yapmakt\u0131r. Bir
s\u0131n\u0131f\u0131n bildirimini yapmak, bu s\u0131n\u0131f hakk\u0131nda derleyiciye bilgi vermek anlam\u0131na gelir.
Derleyici ald\u0131\u011f\u0131 bilginin sonucunda, bu s\u0131n\u0131f t\u00fcr\u00fcnden bir nesne tan\u0131mlanmas\u0131 durumunda,
hem bellekte ne kadar yer ay\u0131raca\u011f\u0131n\u0131 bilir, hem de programc\u0131n\u0131n yazm\u0131\u015f oldu\u011fu koda
ili\u015fkin baz\u0131 kontrol i\u015flemlerini yapma olana\u011f\u0131na kavu\u015fur.

S\u0131n\u0131flar yap\u0131lara g\u00f6re temel olarak iki \u00f6nemli farkl\u0131l\u0131\u011fa sahiptir:
i. S\u0131n\u0131f bildirimi i\u00e7inde s\u0131n\u0131flar\u0131n elemanlar\u0131, isminep ub li c,p ro te cte d ya dap ri vate denilen
\u00fc\u00e7 ayr\u0131 b\u00f6lgede yer alabilir.
ii. S\u0131n\u0131flar yaln\u0131zca veri elemanlar\u0131 de\u011fil i\u015flevler de i\u00e7erir. Bu i\u015flevlere s\u0131n\u0131f\u0131n \u00fcye i\u015flevleri
(member functions)d enir .
\u00d6nce bu iki yeni \u00f6zellik \u00fcst\u00fcnde ayr\u0131nt\u0131l\u0131 bir bi\u00e7imde duraca\u011f\u0131z:
S\u0131n\u0131f Bildirimi
Bir s\u0131n\u0131f\u0131n bildirimi, yani derleyiciye tan\u0131t\u0131lmas\u0131 \u00f6zel bir s\u00f6zdizim ile olur:
S\u0131n\u0131f bildiriminin genel bi\u00e7imi \u015f\u00f6yledir:
class [ s\u0131n\u0131f_ismi] {
[private:]
//\u2026
[protected:]
//\u2026
[public:]
//\u2026
};
class bir anahtar s\u00f6zc\u00fckt\u00fcr. T\u00fcrk\u00e7e "s\u0131n\u0131f" anlam\u0131na gelir. Genel bi\u00e7imdeki s\u0131n\u0131f_
ismi(class tag), bildirimi yap\u0131lan s\u0131n\u0131f\u0131n ismidir. S\u0131n\u0131f ismi, isimlendirme kurallar\u0131na uygun

herhangi bir isim olabilir. Bir s\u0131n\u0131f bildiriminde yer alan blok,p ri vate,p ro te cte d vep ubl ic
isimli \u00fc\u00e7 b\u00f6l\u00fcmden olu\u015fur. public, private vep ro te cte d C++\u2019\u0131n anahtar s\u00f6zc\u00fc\u011f\u00fcd\u00fcr. Bu
s\u00f6zc\u00fcklere bundan sonra eri\u015fim belirteci diyece\u011fiz. Bir b\u00f6l\u00fcm, eri\u015fim belirtecini izleyen iki
nokta \u00fcst \u00fcste( :) ayrac\u0131 ile ba\u015flat\u0131l\u0131r, di\u011fer bir eri\u015fim belirtecinin kullan\u0131lmas\u0131na kadar
s\u00fcrer. \u00dc\u00e7 b\u00f6l\u00fcm\u00fcn, hepsinin bir s\u0131n\u0131f bildiriminde bulunmas\u0131 zorunlu de\u011fildir. Hi\u00e7 bir
eri\u015fim belirtecinin kullan\u0131lmamas\u0131 durumunda s\u0131n\u0131f\u0131np rivat e b\u00f6l\u00fcm\u00fc anla\u015f\u0131l\u0131r. Yani s\u0131n\u0131f
bildirimi i\u00e7inde varsay\u0131lan (default) b\u00f6l\u00fcmp ri vate b\u00f6l\u00fcmd\u00fcr.

\u00dcye i\u015flevlerin yaln\u0131zca bildirimleri s\u0131n\u0131f bildirimi i\u00e7ine yaz\u0131l\u0131r. Bu i\u015flevlerin tan\u0131mlamalar\u0131, normal bir i\u015flev gibi ancak farkl\u0131 bir s\u00f6zdizim kural\u0131 ile s\u0131n\u0131f bildiriminin d\u0131\u015f\u0131nda yap\u0131l\u0131r. Ancak \u00fcye i\u015flevlerin tan\u0131m\u0131 s\u0131n\u0131f bildiriminin i\u00e7inde de yap\u0131labilir. Bu durumu ileride, s\u0131n\u0131f i\u00e7ii nli ne i\u015flevler ba\u015fl\u0131\u011f\u0131yla inceleyece\u011fiz.

C++\u2019da, s\u0131n\u0131f\u0131n bildirimi i\u00e7inde bildirilen i\u015flevlerle s\u0131n\u0131f\u0131n d\u0131\u015f\u0131nda bildirimleri yap\u0131lan
i\u015flevleri birbirlerinden ay\u0131rabilmek i\u00e7in yeni terimler kullan\u0131l\u0131r. S\u0131n\u0131f\u0131n i\u00e7inde bildirilen bir
i\u015fleve, o s\u0131n\u0131f\u0131n \u00fcye i\u015flevi (member function) denirken, di\u011fer i\u015flevlere global i\u015flevler
(global functions) denir. Yani C dilinde daha \u00f6nce tan\u0131mlamaya al\u0131\u015fm\u0131\u015f oldu\u011fumuz
i\u015flevlere art\u0131k bundan b\u00f6yle global i\u015flevler diyece\u011fiz.

S\u0131n\u0131f\u0131n elemanlar\u0131, yap\u0131larda oldu\u011fu gibi s\u0131n\u0131f i\u00e7inde t\u00fcr bilgileri ile bildirilir. A\u015fa\u011f\u0131daki
\u00f6rne\u011fi inceleyin:
class A {

private:
int a;
void func1();

protected:
long b;
int func2(int);

public:
double c;
double func3();

};

Yukar\u0131daki s\u0131n\u0131f bildiriminde, s\u0131n\u0131f\u0131n \u00fc\u00e7 b\u00f6l\u00fcm\u00fcn\u00fcn de kullan\u0131lm\u0131\u015f oldu\u011funu g\u00f6r\u00fcyorsunuz.
S\u0131n\u0131f\u0131n her b\u00f6l\u00fcm\u00fcnde, birer eleman ile birer i\u015flev bildiriliyor. S\u0131n\u0131f bildirimi i\u00e7inde, birden
fazla eri\u015fim belirteci kullan\u0131labilir. \u00d6rne\u011fin yukar\u0131daki bildirim a\u015fa\u011f\u0131daki gibi de
yap\u0131labilirdi:

class A {
protected:
long b;
private:
int a;
public:
double func3();
protected:
int func2(int);
public:
double c;
private:
void func1();
};
S\u0131n\u0131f bildirimi bir eri\u015fim belirteci ile ba\u015flat\u0131lmam\u0131\u015fsa,p rivat e b\u00f6l\u00fcm \u00fczerinde i\u015flem
yap\u0131ld\u0131\u011f\u0131 anla\u015f\u0131l\u0131r. A\u015fa\u011f\u0131daki \u00f6rne\u011fi inceleyin:

class A {
long b;
int a;

public:
double func3();
protected:
int func2(int);
public:
double c;
private:
void func1();
};
Yukar\u0131daki \u00f6rnekte,A isimli s\u0131n\u0131f\u0131na veb elemanlar\u0131 s\u0131n\u0131f\u0131np ri vate b\u00f6l\u00fcm\u00fcnde bildiriliyor.
\u015eimdi de a\u015fa\u011f\u0131daki s\u0131n\u0131f bildirimini inceleyin:

class Date {
int day, mon, year;
bool verify_date();

public:
void set_date(int , int, int);
void display_date();

};

Yukar\u0131da, ismiD at e olan bir s\u0131n\u0131f bildiriliyor. Yukar\u0131daki bildirimle,D ate isimli yeni bir veri
t\u00fcr\u00fc yarat\u0131lm\u0131\u015f olur. C ile C++\u2019\u0131n bu noktadaki fark\u0131n\u0131 da hat\u0131rlay\u0131n: C++\u2019da bu veri
t\u00fcr\u00fcn\u00fcn ismi hem class Date hem deD ate 'dir. Yani birt yp ed ef bildirimi yap\u0131lmaks\u0131z\u0131n

Date ismi bu t\u00fcr\u00fcn ismi olarak kullan\u0131labilir. Oysa C dilinde yap\u0131lar s\u00f6z konusu oldu\u011funda,

bir yap\u0131 ismini (structure tag) bir t\u00fcr ismi olarak kullanmak i\u00e7in birt yp ed ef bildirimi
yapmak gerekir.
Bir s\u0131n\u0131f ismi (class tag), isimlendirme kurallar\u0131na uygun olmak ko\u015fuluyla, istenildi\u011fi gibi
se\u00e7ilebilir. Ancak programc\u0131lar\u0131n \u00e7o\u011fu, yaln\u0131zca ilk harfi b\u00fcy\u00fck di\u011fer harfleri k\u00fc\u00e7\u00fck olan
isimleri se\u00e7erler.

Date isimli s\u0131n\u0131f\u0131n bildirimini incelemeyi s\u00fcrd\u00fcrelim:
intt \u00fcrde n day, mon, year isimli elemanlar s\u0131n\u0131f\u0131n private b\u00f6l\u00fcm\u00fcnde bildirilmi\u015f. Bir eri\u015fim

belirteci kullan\u0131lmad\u0131\u011f\u0131 zaman s\u0131n\u0131f\u0131np ri vate b\u00f6l\u00fcm\u00fcn\u00fcn anla\u015f\u0131ld\u0131\u011f\u0131 s\u00f6ylenmi\u015fti. Ba\u015fka bir
deyi\u015fle,i nt,m on veye ar,D ate s\u0131n\u0131f\u0131n\u0131np rivat e elemanlar\u0131d\u0131r.D ate s\u0131n\u0131f\u0131n\u0131n bildirimi
i\u00e7inde,ver i fy_d ate,se t_ dat e ved isplay _da te isimli \u00fc\u00e7 i\u015flevin bildiriminin yap\u0131ld\u0131\u011f\u0131n\u0131
g\u00f6r\u00fcyoruz. Bu i\u015flevler,D at e s\u0131n\u0131f\u0131n\u0131n\u00fc ye i\u015flevidir.ve rify_ dat e isimli i\u015flevin bildirimi s\u0131n\u0131f\u0131n

private b\u00f6l\u00fcm\u00fcnde yap\u0131lm\u0131\u015f iken, set_date ve display_date i\u015flevlerinin bildirimleri Date
s\u0131n\u0131f\u0131n\u0131np ubl ic b\u00f6l\u00fcm\u00fcnde yap\u0131lm\u0131\u015f. \u015e\u00f6yle de s\u00f6ylenebilirdi:se t_ dat e ved ispl ay_ date,
Dates\u0131 n\u0131f \u0131n\u0131 n public \u00fcye i\u015flevleridir. verify_date Dates\u0131 n\u0131f \u0131n\u0131 n private \u00fcye i\u015flevidir. Peki,

s\u0131n\u0131f\u0131n \u00fcye i\u015flevleri ile bizim daha \u00f6nceden bildi\u011fimiz i\u015flevler aras\u0131nda bir fark var m\u0131?
S\u0131n\u0131flar\u0131n \u00fcye i\u015flevleri ne i\u015fe yar\u0131yorlar, nas\u0131l tan\u0131mlan\u0131yorlar? B\u00fct\u00fcn bu konular\u0131 ayr\u0131nt\u0131l\u0131
bir \u015fekilde ele alaca\u011f\u0131z.

Mant\u0131ksal olarak bir s\u0131n\u0131f ile ili\u015fkilendirilmi\u015f i\u015flevlere \u201c\u00fcye i\u015flevler\u201d (member functions)
denir. Her \u00fcye i\u015flev, s\u0131n\u0131f\u0131n elemanlar\u0131na do\u011frudan eri\u015febilir. S\u0131n\u0131f\u0131n elemanlar\u0131, \u00fcye
i\u015flevler taraf\u0131ndan ortakla\u015fa kullan\u0131lan de\u011fi\u015fkenlerdir. S\u0131n\u0131f\u0131n \u00fcye i\u015flevleri, bir konuya
ili\u015fkin \u00e7e\u015fitli alt i\u015flemleri yapar. Bu i\u015flemleri yaparken de s\u0131n\u0131f\u0131n elemanlar\u0131n\u0131 ortakla\u015fa
olarak kullan\u0131rlar. Bir i\u015fi ger\u00e7ekle\u015ftiren bir dizi i\u015flevin s\u0131n\u0131f ad\u0131 alt\u0131nda ele al\u0131nmas\u0131,
alg\u0131lamay\u0131 ve tasar\u0131m\u0131 kolayla\u015ft\u0131r\u0131r, derleyicinin baz\u0131 denetimleri yapmas\u0131na olanak verir.

Yap\u0131larla oldu\u011fu gibi, s\u0131n\u0131flarla da \u00e7al\u0131\u015fmak i\u00e7in \u00f6nce s\u0131n\u0131f bildirimi yapmak, sonra da bu
s\u0131n\u0131f t\u00fcr\u00fcnden nesneler tan\u0131mlamak gerekir.

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->