C Lecture 4 Tuesday 15 July 2003

Скачать презентацию C Lecture 4 Tuesday 15 July 2003 Скачать презентацию C Lecture 4 Tuesday 15 July 2003

50ccd8103a4bdf13ad26e5d70ec228ff.ppt

  • Количество слайдов: 26

C++ Lecture 4 Tuesday, 15 July 2003 C++ Lecture 4 Tuesday, 15 July 2003

Struct & Classes l l l Structure in C++ Classes and data abstraction Class Struct & Classes l l l Structure in C++ Classes and data abstraction Class scope Constructors and destructors Examples

OOP Key Concepts l l l OOP encapsulates data (attributes) and function (behavior) into OOP Key Concepts l l l OOP encapsulates data (attributes) and function (behavior) into packages called classes. Class is like a blueprint of house. One can build many houses (of the same type) with one blueprint. Out of a class, programmer can create object.

Structure Definition struct Time { int hour; int minute; int second; }; Structure Definition struct Time { int hour; int minute; int second; };

Declaration Time time. Object, // a variable time. Array[10], // array *time. Ptr, // Declaration Time time. Object, // a variable time. Array[10], // array *time. Ptr, // pointer to. . . &time. Ref = time. Object; // ref

Accessing Members of Structure cout << time. Object. hour; cout << time. Ref. hour; Accessing Members of Structure cout << time. Object. hour; cout << time. Ref. hour; time. Ptr = &time. Object; cout << time. Ptr -> hour; cout << (*time. Ptr). hour; // the same

Example Fig. 6. 1 l l l Structure - declaration and use. Conditional expression Example Fig. 6. 1 l l l Structure - declaration and use. Conditional expression expr ? A : B the value is A if expr is true and B if expr is false. C. f. Fig. 6. 1.

Abstract Data Type with a Class class Time { public: Time(); // constructor void Abstract Data Type with a Class class Time { public: Time(); // constructor void set. Time(int, int); void print. Military(); void print. Standard(); private: int hour; int minute; int second; };

Declaration of Objects of Type Declaration of Objects of Type "Time" Time sunset, // object of type Time array. Of. Times[5], // array of Time obj *ptr. To. Time, // pointer to Time obj &dinner. Time = sunset; // reference

What is an Object? l l Object is a variable, Object is a function? What is an Object? l l Object is a variable, Object is a function? Object is data and functions Object is an abstraction of something that has attributes (property) and operations (function calls).

Member Functions l l Member functions of Time are defined outside the class with Member Functions l l Member functions of Time are defined outside the class with : : binary scope resolution operator. E. g. , void Time: : set. Time(int h, int m, int s) { hour = ( h>= 0 && h < 24) ? H : 0; . . . }

Class Example Fig. 6. 3 l l l Class definition Member function definition Main Class Example Fig. 6. 3 l l l Class definition Member function definition Main program C. f. Fig. 6. 3.

Class Scope l l l Class data members and member functions belong to that Class Scope l l l Class data members and member functions belong to that class's scope. Within a class's scope, class members are references by name. Outside a class's scope, class members are referenced through one of the handles on an object.

The Handles l l l Use dot (. ) notation for object and references. The Handles l l l Use dot (. ) notation for object and references. Use arrow (->) for pointer to the object E. g. , c. x , cpt -> x C. f. Fig. 6. 4

Separating interface from implementations l l l Header files contains class declarations only Class Separating interface from implementations l l l Header files contains class declarations only Class function definition in source file Driver program in another file

Public and Private Data or Function l l l Public data or functions are Public and Private Data or Function l l l Public data or functions are accessible from outside Private data or functions are not directly accessible by the user outside the class scope Public functions are used as an interface to access or modify private data

Initializing Class Objects with Constructors l l l A constructor is a class member Initializing Class Objects with Constructors l l l A constructor is a class member function with the same name as the class. The constructor is called whenever an object of that class is created. The constructor does not return a value (not even void).

Destructors l l The name of the destructor for a class is the tilde Destructors l l The name of the destructor for a class is the tilde (~) character followed by the class name. Destructor is called when an object is going to disappear (out of scope).

When Constructors and Destructors are Called l Example of Fig. 6. 15 -17, constructor When Constructors and Destructors are Called l Example of Fig. 6. 15 -17, constructor and destructor of a class. C. f. Fig. 6. 15 -17.

Default Constructor class Time { public: Time(); // default constructor Time(int, int); …. } Default Constructor class Time { public: Time(); // default constructor Time(int, int); …. } Used as Time t, t(12, 06, 0); C. f. Fig. 18 -20

Assignment by Default Memberwise Copy l l If date 1 and date 2 are Assignment by Default Memberwise Copy l l If date 1 and date 2 are objects of the same type, we can say date 2 = date 1; The date members values are copied memberwise. C. f. Fig. 6. 24

Questions l l l In the class assignment example, if the member data are Questions l l l In the class assignment example, if the member data are arrays, are the values of the array elements copied? If it is a pointer, is the pointer value (address) copied? If a pointer pointing to an array, are the values of array elements copied?

Copy Constructor l l You can override what the class assignment a=b means by Copy Constructor l l You can override what the class assignment a=b means by write a so-called copy constructor We declare and implement a function, e. g. , for the class Date(Date &d) C. f. an example in Fig. 8. 4 -8. 6

Problems l Find error(s) in each of the following and explain how to correct Problems l Find error(s) in each of the following and explain how to correct it. • Assuming the following prototype is declared in class Time. • void ~Time(int);

Problems l Find error(s) - The following is a partial definition of class Time: Problems l Find error(s) - The following is a partial definition of class Time: class Time { public: // function prototypes private: int hour = 0; int minute = 0; int second = 0; };

Problems l Find error(s) – Assuming the following prototype is declared in class Employee: Problems l Find error(s) – Assuming the following prototype is declared in class Employee: int Employee(const char *, const char *)




  • Мы удаляем страницу по первому запросу с достаточным набором данных, указывающих на ваше авторство. Мы также можем оставить страницу, явно указав ваше авторство (страницы полезны всем пользователям рунета и не несут цели нарушения авторских прав). Если такой вариант возможен, пожалуйста, укажите об этом.