1 / 26

Nesnelerin Özellikleri

Nesnelerin Özellikleri. - Üye nesneler - friend belirtesi - Nesnelerin operatörlere yüklenmesi - this yerel (lokal) değişkeni - inline tanımlı üye fonksiyonlar - Ortak genel (global) değişkenler - Nesne göstericileri - Nesne dizileri.

ave
Télécharger la présentation

Nesnelerin Özellikleri

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nesnelerin Özellikleri

  2. - Üye nesneler- friend belirtesi- Nesnelerin operatörlere yüklenmesi- this yerel (lokal) değişkeni- inline tanımlı üye fonksiyonlar- Ortak genel (global) değişkenler- Nesne göstericileri- Nesne dizileri

  3. 4.1 Nesne üyesi nesneler Bir nesnenin üye değişkenleri arasında diğer nesneler de yer alabilir. Kullanıp kullanmama bakımından nesnelerin diğer değişkenlerden bir farkı yoktur. class B {A a, b; //… public: B(); B(B&); A(int, int); //… }; class A {int i; //… public: A(); A(A&); A(int); //… };

  4. İkinci sınıfın koıdlamasında ne olabılır? • B::B() {…}//A için otomatikman çalışır • B::B() // tasarımcı ‘by default’ : a(32), b(21) // her değişken için // ayrı bir (sabit) tasarımcı {…} • B::B(int u, int v) // ‘normal’ tasarımcı : a(u), b(v) // ilk değerlerde bir fark olabilir {…}

  5. // nokta.cpp -- Demonstrates a simple class (I) #include <math.h> #include <stdio.h> class Nokta {float X, Y, Z; //access rights?? public: Nokta (float=0, float=0, float=0); //tasarımcı tipi? Nokta (Nokta&); // --- "" ---? int Tasi (float, float, float); // member function int Yaz (); // member function float Uzaklik (Nokta&); // member function };

  6. Class implementation (I) Nokta::Nokta (float x, float y, float z) { X=x; Y=y; Z=z; } Nokta::Nokta ( Nokta& N) {X=N.x; Y=N.y; Z=N.z;} int Nokta::Yaz (){printf("%f, %f, %f", X, Y, Z);} #define DIFSQR (p) ((p-Ikinci*##p)*(p-Ikinci*##p) float Nokta::Uzaklik (Nokta& Ikinci) return sqrt(DIFSQR(X)+ DIFSQR(Y) + DIFSQR(Z)) #undef DIFSQR

  7. İkinci sınıf (Ia) class Dogru { Nokta Baslangıc, Bitis; public: Dogru(Nokta&, Nokta&); Dogru (Dogru&); float Uzunluk(); int Yaz(); };

  8. Class implementation (Ia) Dogru::Dogru (Nokta&, Nokta&) : Baslangıc(A), Bitis(B){} Dogru::Dogru ( Dogru& D ) : Baslangıc(D.Baslangıc),Bitis(D.Bitis) {} float Dogru:Uzunluk() {return Baslangıc.Uzaklık(Bitis); } int Dogru::Yaz () {printf ("["); Baslangıc.Yaz(); Printf ("-"); Bitis.Yaz(); printf ("]"); return 0; }

  9. Main() program I #include<conio.h> void main(){ Nokta A; Nokta B(30, 40, 50); clrscr; A.Yaz();printf ("/n");B.Yaz(); printf("/niki nokta arasindaki mesafe=%f\n", A.Uzaklik(B)); B.Tasi(10, -10, 60); Dogru D(Nokta(10, 20, 30), B); D.Yaz(); printf("/dogrunun uzakligi=%f\n", D.Uzunluk()); return 0; }

  10. 4.2 Friend (Arkadaş) Bilertici #include <math.h> #include <stdio.h> class Nokta {float X, Y, Z; public: Nokta (float=0, float=0, float=0); Nokta (Nokta&); int Tasi (float, float, float); friend int Yaz (Nokta&); friend float Uzaklik (Nokta&, Nokta&); };

  11. Class ımplementatıon (II) Fonksıyonlarda bir değişiklik : int Yaz (){printf("%f, %f, %f", N.X, N.Y, N.Z);} float Uzaklik (Nokta& Birinci, Nokta & İkinci) { return sqrt( (Birinci.X – İkinci.X)*(Birinci.X – İkinci.X) + (Birinci.Y – İkinci.Y)*(Birinci.Y – İkinci.Y)+ (Birinci.Z – İkinci.Z)*(Birinci.Z – İkinci.Z) ); }

  12. Sınıf (IIa) class Dogru { Nokta Baslangic, Bitis; public: Dogru(Nokta&, Nokta&); Dogru (Dogru&); float Uzunluk(); friend int Yaz(); };

  13. Class Implementatıon (IIa) Aynı, hariç: int Yaz (Dogru& D) { printf ("[");Yaz(D.Baslangıc); printf ("-"); Yaz(D.Bitis); printf ("]"); return 0; }

  14. Main() program (II) #include<conio.h> void main(){ Nokta A; Nokta B(30, 40, 50); clrscr; Yaz(A);printf ("/n");Yaz(B); printf("/niki nokta arasındaki mesafe=%f\n", Uzaklik(A,B)); B.Tasi(10, -10, 60); Dogru D(Nokta(10, 20, 30), B); Yaz(D); printf("/dogrunun uzaklığı=%f\n", D.Uzunluk()); return 0; }

  15. Arkadaş-sınıf class A { friend class B; // friend için: private,public, protected – önemi yok! // data members and member functions }; class B { // A sınıfın tüm dataları (private, protected bile) kullanabilir ! //... }

  16. 4.3 Nesnelerin Operatörlere Yüklenmesi class Vector {public: float X, Y, Z; Vector ( float=0, float=0, float=0 ); Vector (Vector&); } Vector::Vector(float a, float b, float c) {X=a; Y=b; Z=c; } Vector::Vector(Vector & v) { X=v.X; Y=v.Y; Z=v.Z; }

  17. Operatörler: Vector& operator + (Vector& A, Vector& B) {return Vector(A.X+B.X, A.Y+B.Y, A.Z+B.Z); } Vector & operator – (Vector & A, Vector & B) {return Vector (A.X-B.X, A.Y-B.Y, A.Z-B.Z); } //bu tanımlamalardan sonra ...

  18. Vector A(3, 4); Vector B(8, 6.513, 1); Vector C=A+B; Vector D=A-B; float f=67; Vector E = A - Vector (16, 1, -10)+ Vector (f, f*3,f/4); //yazmak mümkün olacaktır. Fakat * operasyon için // (Vector*float) 2 fonksiyon yazmak gerekiyor! Neden?

  19. 4.4 this Yerel Değişkeni void Nokta::Rapor() { printf(“Bu nokta ”); Yaz(*this); printf(“konumundadir\n”); } Nokta::Nokta (float X, float Y, float Z ) { this->X=X; this->Y=Y; this->Z=Z; }

  20. 4.5 Genel Ortak DeğişkenlerHer nesne sahip olduğunu bilgileri dığer tüm nesnelere karşi korur. Eğer bazı bilgilerin sahipsizce tüm programlama öğeleri tarafından kullanılması istenirse, bu değişkenler global olarak tanımlanırlar. Eğer bir değişkeni sadece belli bir sınıfa mensup nesnelerin kullanması istenirse, bu istek sınıf tanımlaması içinde bildirilir. Bu bildiri değişken tanımlamasının başina static kelimesi ilave edilerek yapılır.

  21. #include<stdio.h> //static1.cpp #include<iostream.h> class Test {int a; static int b; public: Test(int a, int x) {this->a=a; b+=x;} int SayA() {return a;} int SayB() {return b;} }; int Test::b; main() { Test A(5, 10); cout<<"A a = "<<A.SayA()<<" A b = "<<A.SayB()<<endl; Test B(8, 15); cout<<"B a = "<<B.SayA()<<" B b = "<<B.SayB()<<endl; cout<<"A a = "<<A.SayA()<<" A b = "<<A.SayB()<<endl; return 0; } //static nesneler de olabilir: static Nokta(8, 7, -12);

  22. 4.6Durağan (static) fonksiyonlar //STATIC.CPP //////// #include<stdio.h> class Static_Test { int X; static int Y; public: Static_Test(int); int X_Value(); void Auto_Display(); static void Static_Display(); static void Static_Message(); };

  23. int Static_Test::Y=300; Static_Test::Static_Test(int x) {X=x;} int Static_Test::X_Value(){return X;} //automatic function definition void Static_Test::Auto_Display() { printf("x= %d\t", X); printf("** %d **\n", X_Value()); printf("y= %d\n", Y); Static_Message(); //static function call }

  24. //static function definition void Static_Test::Static_Display() { /* printf("x= %d\t", X); // Error! No object! printf("** %d **\n", X_Value()); // Error! Use . or -> to call Static_Test::X_Value with an object! */ printf("y= %d\n", Y); Static_Message(); }

  25. void Static_Test::Static_Message() { printf("Static_Test, Static_Message function\n"); } ///////////////////////////////////////////////////// main() { Static_Test A(8); A.Auto_Display(); // Static_Test::Auto_Display(); - Error ! A.Static_Display(); printf("\n"); Static_Test::Static_Display(); return 0; }

  26. 4.7 const Fonksiyonlar Tamamlasının sonunda const(sabıt) kelimesi bulunan fonksiyonlar bilgi fonksiyonu olarak adlandırılırlar. Bu fonksionlar üye değişkenlerin değerlerini değişteremiyecekleri gibi fonksiyonu olmayan diğer fonksiyonları da çağıramazlar. classsınıf_adı{…int const_fonksiyon(…) const;…} intsınıf_adı::const_fonksiyon(…) const {…return değer;}

More Related