1 / 26

C++

C++. Lecture 4 Tuesday, 15 July 2003. Struct & Classes. Structure in C++ Classes and data abstraction Class scope Constructors and destructors Examples. OOP Key Concepts. OOP encapsulates data (attributes) and function (behavior) into packages called classes.

kish
Download Presentation

C++

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. C++ Lecture 4 Tuesday, 15 July 2003

  2. Struct & Classes • Structure in C++ • Classes and data abstraction • Class scope • Constructors and destructors • Examples

  3. OOP Key Concepts • 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.

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

  5. Declaration Time timeObject, // a variable timeArray[10], // array *timePtr, // pointer to... &timeRef = timeObject; // ref

  6. Accessing Members of Structure cout << timeObject.hour; cout << timeRef.hour; timePtr = &timeObject; cout << timePtr -> hour; cout << (*timePtr).hour; // the same

  7. Example Fig.6.1 • 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.

  8. Abstract Data Type with a Class class Time { public: Time(); // constructor void setTime(int, int, int); void printMilitary(); void printStandard(); private: int hour; int minute; int second; };

  9. Declaration of Objects of Type "Time" Time sunset, // object of type Time arrayOfTimes[5], // array of Time obj *ptrToTime, // pointer to Time obj &dinnerTime = sunset; // reference

  10. What is an Object? • 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).

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

  12. Class Example Fig.6.3 • Class definition • Member function definition • Main program C.f. Fig. 6.3.

  13. Class Scope • 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.

  14. The Handles • 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

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

  16. Public and Private Data or Function • 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

  17. Initializing Class Objects with Constructors • 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).

  18. Destructors • 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).

  19. When Constructors and Destructors are Called • Example of Fig. 6.15-17, constructor and destructor of a class. C.f. Fig. 6.15-17.

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

  21. Assignment by Default Memberwise Copy • If date1 and date2 are objects of the same type, we can say date2 = date1; • The date members values are copied memberwise. C.f. Fig.6.24

  22. Questions • 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?

  23. Copy Constructor • 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(Date &d) C.f. an example in Fig.8.4-8.6

  24. Problems • 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);

  25. Problems • 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; };

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

More Related