21
Copyright © 2002 W. A. Tu cker 1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Embed Size (px)

Citation preview

Page 1: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 1

Chapter 10 Lecture Notes

Bill Tucker

Austin Community College

COSC 1315

Page 2: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 2

Procedural Programming Design

• Collection of functions that operate on another collection of data variables

• Function calls are done to perform a task and the names are usually verbs

• Functions have long lists of parameters that must be passed in various ways

• Functions are often involved and difficult to modify

Page 3: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 3

Object Oriented Design

• Design methodology combines data and functions that operate on that data into a class, whose name is usually a noun

• The data elements represent the attributes of the class

• The functions (methods) represent the behavior of the class. Their names are usually verbs.

• Combining behavior and attributes together is called encapsulation

Page 4: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © Jade Lindquist 4

Encapsulation

data attributes

public member functions

Page 5: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 5

Users of a Class

• Only need to know how to interface to the class

• Do not need to know the inner workings of the class.

• Are often restricted from being able to directly access or directly modify the data attributes

• Specific implementation details are hidden from the user. (Abstraction)

Page 6: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 6

Temperature Class Example

• Temperature is a noun– Style would have us capitalize the first letter

• Attributes – (data values)– The temperature in degrees– The temperature scale (F or C)

• We want the data elements hidden so that the user may not directly modify them – This is done by classifying the data elements as

private, which allows only the member functions of the class to directly access the values

Page 7: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 7

Class Definition Syntax

• Reserved words are class, private and public• Note that the class ends with a semicolon

class Temperature{private:

double temp;char scale;

};

NOTE: You CAN NOT initialize values in the declaration statements in a class definition because no memory has been allocated at this point.

Page 8: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 8

Class Behavior (Functions)

• Class functions fall into several categories– Accessor Functions (functions to access (retrieve) the private

data elements)– Modification Functions (functions to modify the private data

elements)– Constructor Functions (functions to allow for initialization of the

private data elements)– Friend Functions (covered in later courses)– Destructor Functions (covered later)– Utility Functions (covered in later courses)

• The functions may be classified as public or private. Public functions may be called by functions that are not part of the class. Private functions may only be called from other member functions of the same class.

Page 9: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 9

Class Definition Syntax

class Temperature{private:

int temp;char scale;

public:void setTemp (double); // Modification Functionvoid setScale (char); // Modification Functionvoid convertTemp(); // Modification Functiondouble getTemp(); // Accessor Functionchar getScale(); // Accessor Function

};

Page 10: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 Jade Lindquist 10

Temperature Class

double temp;char scale;

double getTemp();

void setTemp(double);

char getScale();

void setScale(char);

void convertTemp();

Page 11: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 11

Declaring an Object of a Class

• A class is considered a “user defined data type”, thus the following declaration statement would be valid in a main function

Temperature refrigerator;• OOP terminology

– The declaration statement creates refrigerator as an object of class Temperature

– The declaration statement creates an instance of class Temperature called refrigerator

– The declaration statement instantiates the object refrigerator of class Temperature

Page 12: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 12

Invoking Behaviors

• To call a member function of a class you need to use the dot member operator

(a single period between the object name and the function name)

Temperature refrigerator;

refrigerator.setTemp(35);

refrigerator.setScale(‘F’);

Page 13: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 13

Member Function Implementation

• Each one of the member functions must be defined with a complete function definition.

• Since the function definitions will be in a separate file for hiding, they must be associated with the class name by the use of the scope resolution operator

(a double colon :: )

Page 14: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 14

Accessor Functions

double Temperature::getTemp(){

return temp;}char Temperature::getScale(){

return scale;}

NOTE: Member functions may directly access private data elements

Page 15: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 15

Modification Functions

void Temperature::setTemp(double t){

temp = t;}void Temperature::setScale(char s){

scale = s;}

NOTE: Member functions may directly access private data elements

Page 16: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 16

Modification Functions (cont)void Temperature::convertTemp(){

if (scale == 'C'){

scale = 'F';temp = 9.0 * temp / 5.0 + 32;

}else

if (scale == 'F'){

scale = 'C';temp = 5.0 *(temp - 32) / 9.0;

}}

Page 17: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 17

Constructor Functions

• Constructor functions are used to provide the capability to initialize objects

• This provides the capability to optionally initialize the objects when they are instantiated (created)

• Constructor functions are automatically called when the object is instantiated

• Constructor functions are often overloaded, especially useful when no arguments are given

Temperature oven;Temperature refrigerator (32, ‘F’);

Page 18: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 18

More on Constructors

• Constructor functions have the exact same name as the name of the class.

• Constructor functions are neither void nor value returning. They are automatically called when the object is instantiated and no return type is appropriate.

• Default parameters may also be used with constructor functions.

Page 19: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 19

Syntax of Constructor FunctionsTemperature:: Temperature() // Default Constructor – NO parameters{

temp = 0;scale = 'C';

}Temperature::Temperature(double t, char s){

temp = t;scale = s;

}NOTE: NO RETURN TYPE is specified (void or datatype) since the

constructor functions are called automatically when the objects are instantiated and no return value is appropriate

Page 20: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 20

Destructor Function

• Is also neither void nor value returning. • Is automatically called to free the memory used

by the instance when the class object goes out of scope

• Is named the same as the Class, but with a tilde (~) in front of the Class name

• At this point, we can let the compiler take care of it automatically.

• The syntax in the class definition would be~Temperature();

Page 21: Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315

Copyright © 2002 W. A. Tucker 21

Implementation of a Class

• To ensure information hiding, a class is implemented with three different C++ files

• Class definition file Temperature.h

• Class implementation file Temperature.cpp

• Client / driver program (test code) tempTest.cpp