Download presentation
Presentation is loading. Please wait.
Published byStephen Collins Modified over 9 years ago
1
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315
2
Copyright © 2002 W. A. Tucker2 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
3
Copyright © 2002 W. A. Tucker3 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
4
Copyright © Jade Lindquist4 Encapsulation data attributes public member functions
5
Copyright © 2002 W. A. Tucker5 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)
6
Copyright © 2002 W. A. Tucker6 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
7
Copyright © 2002 W. A. Tucker7 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.
8
Copyright © 2002 W. A. Tucker8 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.
9
Copyright © 2002 W. A. Tucker9 Class Definition Syntax class Temperature { private: int temp; char scale; public: void setTemp (double);// Modification Function void setScale (char);// Modification Function void convertTemp();// Modification Function double getTemp();// Accessor Function char getScale();// Accessor Function };
10
Copyright © 2002 Jade Lindquist10 Temperature Class double temp; char scale; double getTemp(); void setTemp(double); char getScale(); void setScale(char); void convertTemp();
11
Copyright © 2002 W. A. Tucker11 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
12
Copyright © 2002 W. A. Tucker12 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’);
13
Copyright © 2002 W. A. Tucker13 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 :: )
14
Copyright © 2002 W. A. Tucker14 Accessor Functions double Temperature::getTemp() { return temp; } char Temperature::getScale() { return scale; } NOTE: Member functions may directly access private data elements
15
Copyright © 2002 W. A. Tucker15 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
16
Copyright © 2002 W. A. Tucker16 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; }
17
Copyright © 2002 W. A. Tucker17 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’);
18
Copyright © 2002 W. A. Tucker18 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.
19
Copyright © 2002 W. A. Tucker19 Syntax of Constructor Functions Temperature:: 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
20
Copyright © 2002 W. A. Tucker20 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();
21
Copyright © 2002 W. A. Tucker21 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.