Download presentation
Presentation is loading. Please wait.
Published byBernard McCormick Modified over 9 years ago
1
C++ Lecture 4 Tuesday, 15 July 2003
2
Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l Examples
3
OOP Key Concepts l OOP encapsulates data (attributes) and function (behavior) into packages called classes. l Class is like a blueprint of house. One can build many houses (of the same type) with one blueprint. l 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 hour; cout << (*timePtr).hour; // the same
7
Example Fig.6.1 l Structure - declaration and use. l Conditional expression expr ? A : B l 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? l Object is a variable, l Object is a function? l Object is data and functions l Object is an abstraction of something that has attributes (property) and operations (function calls).
11
Member Functions l Member functions of Time are defined outside the class with :: binary scope resolution operator. l E.g., void Time::setTime(int h, int m, int s) { hour = ( h>= 0 && h < 24) ? H : 0;... }
12
Class Example Fig.6.3 l Class definition l Member function definition l Main program C.f. Fig. 6.3.
13
Class Scope l Class data members and member functions belong to that class's scope. l Within a class's scope, class members are references by name. l Outside a class's scope, class members are referenced through one of the handles on an object.
14
The Handles l Use dot (.) notation for object and references. l Use arrow (->) for pointer to the object l E.g., c.x, cpt -> x C.f. Fig. 6.4
15
Separating interface from implementations l Header files contains class declarations only l Class function definition in source file l Driver program in another file
16
Public and Private Data or Function l Public data or functions are accessible from outside l Private data or functions are not directly accessible by the user outside the class scope l Public functions are used as an interface to access or modify private data
17
Initializing Class Objects with Constructors l A constructor is a class member function with the same name as the class. l The constructor is called whenever an object of that class is created. l The constructor does not return a value (not even void).
18
Destructors l The name of the destructor for a class is the tilde (~) character followed by the class name. l Destructor is called when an object is going to disappear (out of scope).
19
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.
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 l If date1 and date2 are objects of the same type, we can say date2 = date1; l The date members values are copied memberwise. C.f. Fig.6.24
22
Questions l In the class assignment example, if the member data are arrays, are the values of the array elements copied? l If it is a pointer, is the pointer value (address) copied? l If a pointer pointing to an array, are the values of array elements copied?
23
Copy Constructor l You can override what the class assignment a=b means by write a so-called copy constructor l 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 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);
25
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; };
26
Problems l Find error(s) – Assuming the following prototype is declared in class Employee: int Employee(const char *, const char *)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.