c++ lecture 4 tuesday, 15 july 2003. struct & classes l structure in c++ l classes and data...

26
C++ Lecture 4 Tuesday, 15 July 2003

Upload: bernard-mccormick

Post on 04-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

C++

Lecture 4Tuesday, 15 July 2003

Page 2: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Struct & Classes

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

Page 3: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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.

Page 4: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Structure Definition

struct Time { int hour; int minute; int second;

};

Page 5: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Declaration

Time timeObject, // a variable timeArray[10], // array *timePtr, // pointer to...

&timeRef = timeObject; // ref

Page 6: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Accessing Members of Structure

cout << timeObject.hour;cout << timeRef.hour;timePtr = &timeObject;cout << timePtr -> hour;cout << (*timePtr).hour; // the same

Page 7: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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.

Page 8: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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;};

Page 9: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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

Page 10: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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).

Page 11: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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;...

}

Page 12: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Class Example Fig.6.3

Class definition Member function definition Main program

C.f. Fig. 6.3.

Page 13: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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.

Page 14: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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

Page 15: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Separating interface from implementations

Header files contains class declarations only

Class function definition in source file

Driver program in another file

Page 16: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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

Page 17: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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).

Page 18: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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).

Page 19: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

When Constructors and Destructors are Called

Example of Fig. 6.15-17, constructor and destructor of a class.

C.f. Fig. 6.15-17.

Page 20: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Default Constructor

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

Page 21: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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

Page 22: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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?

Page 23: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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 DateDate(Date &d)

C.f. an example in Fig.8.4-8.6

Page 24: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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);

Page 25: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

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;};

Page 26: C++ Lecture 4 Tuesday, 15 July 2003. Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l

Problems

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