Sunum yükleniyor. Lütfen bekleyiniz

Sunum yükleniyor. Lütfen bekleyiniz

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Sınıflar ve Veri Soyutlama (Classes and.

Benzer bir sunumlar


... konulu sunumlar: "© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Sınıflar ve Veri Soyutlama (Classes and."— Sunum transkripti:

1 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Sınıflar ve Veri Soyutlama (Classes and Data Abstraction ) Outline 16.1Giriş 16.2Bir Sınıf ile Zaman Soyut Veri Tipinin Uygulanması 16.3Sınıf Faaliyet Alanı ve Sınıf Üyelerine Erişim 16.4Arayüzü Uygulamadan Ayırmak 16.5Üyelere Erişimi Kontrol Etmek 16.6Erişim Fonksiyonları ve Hizmet Fonksiyonları 16.7Sınıf Nesnelerine İlk Değer Atamak: Yapıcılar 16.8Yapıcılarla Varsayılan argumanları Kullanmak 16.9Yıkıcıları Kullanmak 16.10Yapıcılar ve Yıkıcılar ne zaman Çağırılır? 16.11Veri Üyeleri ve Üye Fonksiyonları Kullanmak 16.12Gizli Bir Tuzak 16.13Kopyalayarak Atama Yapmak 16.14Yazılımın Yeniden Kullanılırlığı

2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Amaçlar •Bu bölümde neler öğreneceksiniz? –Yazlım mühendisliğinin konularından kapsülleme (en- capsulation) ve veri gizleme (data hiding) konuları –Veri soyutlama (data abstraction) and soyut veri tipleri (abstract data types) (ADTs). –C++ soyut veri tipleri oluşturmak:sınıflar (classes). –Sınıf oluşturmak, kullanmak ve yıkmak. –Bir nesnenin veri üyeleri ve fonksiyon üylerine erişimi.

3 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Giriş •Nesne-yönelimli programlama (Object-oriented programming) (OOP) –Veri (nitelik) ve fonksiyonlar (davranışlar) sınıfların içinde paketlenir. –Veri ve fonksiyonlar birbirleriyle yakından ilişkilidirler. •Bilgi Saklama (Information hiding) –Sınıflarla uygulamanın detayları gizlenir. •C++ programlama: sınıf –Sınıf bir plana benzer : – yeniden kullanılabilir –Nesneler sınıflardan oluşturulurlar. –Örneğin, bir evin bir plan yapılması gibi –C programcıları fonksiyon odaklı çalışırlar.

4 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1class Time { 2public: 3 Time(); 4 void setTime( int, int, int ); 5 void printMilitary(); 6 void printStandard(); 7private: 8 int hour; // int minute; // int second; // }; // end class Time 16.2 Sınıf ile Zaman Veri Tipinin UyguIaması •Sınıflar –Nitelikleri (attributes) (veri üyeleri-data members) ve davranışları (behaviors) (fonksiyon üyeler) içinde barındıran nesne modelleridir. –Class anahtar kelimesi ile başlanır. Public: and Private: are member-access specifiers. setTime, printMilitary, and printStandard are member functions. Time is the constructor. hour, minute, and second are data members.

5 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Sınıf ile Zaman Veri Tipinin UyguIaması (II) •Format –Sınıf tanımlamasının gövdesi küme parantezleri ( { ve } ) ile beraber yapılmıştır. –Sınıf tanımlaması noktalı virgül ile biter. •Veri ve Fonksiyon Üyeler –Üye erişim belirteçleri Public - public erişim belirtecinden sonra bildirilen tüm sınıf üyeleri, Time sınıfından oluşturulmuş bir nesne ile erişilebilir. Private – private erişim belirtecinden sonra bildirilen tüm sınıf üyeleri sadece sınıfın üye fonksiyonları tarafından erişilebilir. Protected – daha sonra işlenecektir.

6 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Sınıf ile Zaman Veri Tipinin UyguIaması (III) •Yapıcı –Sınıf nesnesinin veri üyelerine ilk değerleri atayan özel bir fonksiyondur. –Yapıcılar geri değer döndürmezler. –Sınıf ile aynı isme sahiptirler. •Tanımlama –Bir sınıf tanımlanmış ise bu sınıf daha sonra bir veri tipi olarak kullanılabilir. Time sunset, // object of type Time arrayOfTimes[ 5 ], // array of Time objects *pointerToTime, // pointer to a Time object &dinnerTime = sunset; // reference to a Time object Note: The class name becomes the new type specifier.

7 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Implementing a Time Abstract Data Type with a Class (IV) •İkili faaliyet alanı çözünürlük operatörü (Binary scope resolution operator ) ( :: ) –Sınıfın üye adını sınıf adına bağlar. –Farklı sınıflar aynı isimli üyelere sahip olabilirler. •Bir sınıfa ait fonksiyon üyesini tanımlamak geridonustipi SınıfIsmi :: FonksiyonUyeİsmi ( ){ … }

8 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Implementing a Time Abstract Data Type with a Class (V) •Eğer fonksiyon üyesi sınıfın içinde tanımlanırsa –:: operatörü ve sınıf ismi gerekmez. –Sınıf üyesini sınıfın dışında tanımlamak sınıfın public veya private olmasını değiştirmez. •Takibeden slaytlarda –Time yapıcısı veri üyelerine 0 değerini atamaktadır.

9 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_02.cpp (Part 1 of 3)

10 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_02.cpp (Part 2 of 3)

11 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_02.cpp (Part 3 of 3)

12 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output The initial military time is 00:00 The initial standard time is 12:00:00 AM Military time after setTime is 13:27 Standard time after setTime is 1:27:06 PM After attempting invalid settings: Military time: 00:00 Standard time: 12:00:00 AM

13 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Class Scope and Accessing Class Members •Class scope –Data members and member functions •File scope –Nonmember functions •Function scope –Variables defined in member functions, destroyed after function completes •Inside a scope –Members accessible by all member functions –Referenced by name

14 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Class Scope and Accessing Class Members (II) •Outside a scope –Use handles •An object name, a reference to an object or a pointer to an object •Accessing class members –Same as struct s –Dot (. ) for objects and arrow ( -> ) for pointers –Example: t.hour is the hour element of t –TimePtr->hour is the hour element

15 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_03.cpp (Part 1 of 2)

16 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_03.cpp (Part 2 of 2) Program Output Assign 7 to x and print using the object's name: 7 Assign 8 to x and print using a reference: 8 Assign 10 to x and print using a pointer: 10

17 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Separating Interface from Implementation •Separating interface from implementation –Easier to modify programs –C++ programs can be split into Header files – contains class definitions and function prototypes Source-code files – contains member function definitions •Program Outline: –Using the same Time class as before, create a header file –Create a source code file •Load the header file to get the class definitions •Define the member functions of the class

18 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time1.h

19 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time1.cpp (Part 1 of 2)

20 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time1.cpp (Part 2 of 2)

21 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_04.cpp (Part 1 of 2)

22 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_04.cpp (Part 2 of 2) Program Output The initial military time is 00:00 The initial standard time is 12:00:00 AM Military time after setTime is 13:27 Standard time after setTime is 1:27:06 PM After attempting invalid settings: Military time: 00:00 Standard time: 12:00:00 AM

23 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Controlling Access to Members •Purpose of public –Give clients a view of the services the class provides (interface) •Purpose of private –Default setting –Hide details of how the class accomplishes its tasks (implementation) –Private members only accessible through the public interface using public member functions

24 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_05.cpp

25 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Borland C++ command-line compiler error messages Time1.cpp: Fig16_05.cpp: Error E2247 Fig16_05.cpp 15: 'Time::hour' is not accessible in function main() Error E2247 Fig16_05.cpp 18: 'Time::minute' is not accessible in function main() *** 2 errors in Compile *** Microsoft Visual C++ compiler error messages Compiling... Fig16_05.cpp D:\Fig16_05.cpp(15) : error C2248: 'hour' : cannot access private member declared in class 'Time' D:\Fig16_05\time1.h(18) : see declaration of 'hour' D:\Fig16_05.cpp(18) : error C2248: 'minute' : cannot access private member declared in class 'Time' D:\time1.h(19) : see declaration of 'minute' Error executing cl.exe. test.exe - 2 error(s), 0 warning(s)

26 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Access Functions and Utility Functions •Utility functions –private functions that support the operation of public functions –Not intended to be used directly by clients •Access functions –public functions that read/display data or check conditions –For a container, it could call the isEmpty function •Next –Program to take in monthly sales and output the total –Implementation not shown, only access functions

27 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. salesp.h

28 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. salesp.cpp (Part 1 of 3)

29 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. salesp.cpp (Part 2 of 3)

30 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. salesp.cpp (Part 3 of 3)

31 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_06.cpp Program Output Enter sales amount for month 1: Enter sales amount for month 2: Enter sales amount for month 3: Enter sales amount for month 4: Enter sales amount for month 5: Enter sales amount for month 6: Enter sales amount for month 7: Enter sales amount for month 8: Enter sales amount for month 9: Enter sales amount for month 10: Enter sales amount for month 11: Enter sales amount for month 12: The total annual sales are: $

32 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Sınıf Nesnelerine İlk Değerlerini Atamak: Yapıcılar •Yapıcı fonksiyon –Sınıf üyelerine ilk değerlerini verir. –Sınıf ile aynı isme sahiptir, geri dönüş veri tipi yoktur. –Veri üyeleri yapıcılar veya set fonksiyonları ile değer alırlar. •Nesnenin Tanımlanması –Nesnenin tanımlanması sırasında verilen parametreler yapıcı fonksiyona geçer.

33 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Sınıf Nesnelerine İlk Değerlerini Atamak: Yapıcılar (II) •Format VeriTipi SınıfIsmi ( deger1, deger2, … ); –Yapıcı deger1, deger2, vs. üye değişkenlere atar –Veri üyesi kadar parametre yollanmamış ise, en sağdaki parametreler varsayılan (default) değerler alırlar. myClass myObject( 3, 4.0 );

34 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Yapıcılarla beraber Varsayılan Argümanları Kullanmak •Varsayılan (Default) yapıcılar –Her sınıf için bir tane –Parametresiz çağrılabilirler. –Parametrelerin varsayılan (default) değerleri olabilir. •Varsayılan (Default) parametreler –Varsayılan yapıcı prototipinde (sınıf tanımlanırken) belirlemek •Sınıf dışında, fonksiyon tanımlanırken varsayılan parametre değerleri verilmez. –Örnek: SampleClass( int = 0, float = 0); •Yapıcı sınıf ile aynı isme sahip

35 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time2.h

36 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time2.cpp (Part 1 of 2)

37 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time2.cpp (Part 2 of 2)

38 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_07.cpp (Part 1 of 2)

39 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_07.cpp (Part 2 of 2)

40 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Constructed with: all arguments defaulted: 00:00 12:00:00 AM hour specified; minute and second defaulted: 02:00 2:00:00 AM hour and minute specified; second defaulted: 21:34 9:34:00 PM hour, minute, and second specified: 12:25 12:25:42 PM all invalid values specified: 00:00 12:00:00 AM

41 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Yıkıcıların Kullanımı •Yıkıcılar –Sınıfın fonksiyon üyesi –Yıkıcılar nesne yok edilirken çağrılırlar. Sistem hafızayı geri alırken sonlandırma temizliğini yapar, böylece hafıza alanı yeni nesnelerin tutulması için tekrar kullanılabilir. –Yapıcıların tersi işlem yapar. –Yıkıcı isimlendirilirken tilda ( ~ ) sınıf isminden sonra konulur. •~Time –Parametre ve geri dönüş tipi yok. –Yıkıcı fonksiyonlar aşırı yüklenemez.

42 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Yapıcı ve Yıkıcılar Ne Zaman Çağrılır? •Yapıcı ve yıkıcılar otomatik olarak çağrılırlar. •Global nesneler –Yapıcılar main de dahil diğer tüm fonksiyonlardan önce çağırılırlar. –Yıkıcılar main bittiği zaman çağrılır (veya exit fonksiyon çağrıldığında) –Global nesneler için yıkıcılar program abort komutu ile bitirildiğinde çağrılmazlar.

43 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Yapıcı ve Yıkıcılar Ne Zaman Çağrılır? (II) •Yerel nesneler –Nesne tanımlandığı zaman yapıcı çağrılır. –Nesne faaliyet alanı dışına çıktığında yıkıcı çağrılır. –Program exit veya abort ile çıktığında yıkıcı çağrılmaz •static yerel nesneler –Yapıcı static nesnenin tanımlandığı yerde bir kez çağrılır. –Yıkıcı main sonlandığında yada exit ile çıkış yaptığında çağrılır. –Program abort ile çıktığında static nesne yıkıcısı çağrılmaz

44 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. create.h

45 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. create.cpp

46 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_08.cpp (Part 1 of 2)

47 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_08.cpp (Part 2 of 2)

48 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Object 1 constructor (global created before main) Object 2 constructor (local automatic in main) Object 3 constructor (local static in main) Object 5 constructor (local automatic in create) Object 6 constructor (local static in create) Object 7 constructor (local automatic in create) Object 7 destructor Object 5 destructor Object 4 constructor (local automatic in main) Object 4 destructor Object 2 destructor Object 6 destructor Object 3 destructor Object 1 destructor

49 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Data Members and Member Functions •Classes provide public member functions –Set (i.e., write) or get (i.e., read) values of private data members –Adjustment of bank balance (a private data member of class BankAccount ) by member function computeInterest •Naming –Member function that sets interestRate typically named setInterestRate –Member function that gets interestRate would typically be called getInterestRate

50 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Data Members and Member Functions (II) •Do set and get capabilities effectively make data members public ? –No! –Programmer decides what the function can set and what information the function can get •public set functions should –Check attempts to modify data members –Ensure that the new value is appropriate for that data item –Example: an attempt to set the day of the month to 37 would be rejected –Programmer must include these features

51 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time3.h (Part 1 of 2)

52 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time3.h (Part 2 of 2)

53 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time3.cpp (Part 1 of 3)

54 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time3.cpp (Part 2 of 3)

55 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time3.cpp (Part 3 of 3)

56 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_09.cpp (Part 1 of 3)

57 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_09.cpp (Part 2 of 3)

58 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_09.cpp (Part 3 of 3) Program Output Result of setting all valid values: Hour: 17 Minute: 34 Second: 25 Result of attempting to set invalid hour and second: Hour: 0 Minute: 43 Second: 0 Incrementing minute 3 times: Start time: 11:58:00 AM minute + 1: 11:59:00 AM minute + 1: 12:00:00 PM minute + 1: 12:01:00 PM

59 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved A Subtle Trap: Returning a Reference to a Private Data Member •Reference to an object –Alias for the name of the object –May be used on the left side of an assignment statement –Reference can receive a value, which changes the original object as well •One way to use this capability (unfortunately!) –Have a public member function of a class return a non- const reference to a private data member –This reference can be modified, which changes the original data

60 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time4.h

61 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time4.cpp (Part 1 of 2)

62 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. time4.cpp (Part 2 of 2)

63 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_10.cpp (Part 1 of 2)

64 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_10.cpp (Part 2 of 2) Program Output Hour before modification: 20 Hour after modification: 30 ********************************* POOR PROGRAMMING PRACTICE!!!!!!!! badSetHour as an lvalue, Hour: 74 *********************************

65 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Assignment by Default Memberwise Copy •Assignment operator ( = ) –Sets variables equal, i.e., x = y ; –Can be used to assign an object to another object of the same type –Memberwise copy — member by member copy myObject1 = myObject2 ; •Objects may be –Passed as function arguments –Returned from functions (call-by-value default) •Use pointers for call by reference

66 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_11.cpp (Part 1 of 2)

67 Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig16_11.cpp (Part 2 of 2) Program Output date1 = date2 = After default memberwise copy, date2 =

68 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Software Reusability •Object-oriented programmers –Concentrate on implementing useful classes •Tremendous opportunity to capture and catalog classes –Accessed by large segments of the programming community –Class libraries exist for this purpose •Software –Constructed from existing, well-defined, carefully tested, portable, widely available components –Speeds development of powerful, high-quality software


"© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Sınıflar ve Veri Soyutlama (Classes and." indir ppt

Benzer bir sunumlar


Google Reklamları