Introduction to Classes (Chapter 13.1 –...
Transcript of Introduction to Classes (Chapter 13.1 –...
![Page 1: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/1.jpg)
Introduction to Classes (Chapter 13.1 – 13.12)
Dr. Yingwu Zhu
![Page 2: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/2.jpg)
13.1 Procedural and Object-Oriented Programming
![Page 3: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/3.jpg)
13-3
Procedural and Object-Oriented Programming
• Procedural programming focuses on the process/actions that occur in a program
• Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of ADTs that represent the data and its functions
![Page 4: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/4.jpg)
13-4
Limitations of Procedural Programming
• If the data structures change, many functions must also be changed
• Programs that are based on complex function hierarchies are: – difficult to understand and maintain
– difficult to modify and extend
– easy to break
![Page 5: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/5.jpg)
13-5
Object-Oriented Programming Terminology
• class: like a struct (allows bundling of related variables), but variables and functions in the class can have different properties than in a struct
• object: an instance of a class, in the same way that a variable can be an instance of a struct
![Page 6: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/6.jpg)
13-6
Classes and Objects
• A Class is like a blueprint and objects are like houses built from the blueprint
![Page 7: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/7.jpg)
13-7
Object-Oriented Programming Terminology
• attributes: members of a class
• methods or behaviors: member functions of a class
![Page 8: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/8.jpg)
13-8
More on Objects
• data hiding: restricting access to certain members of an object
• public interface: members of an object that are available outside of the object. This allows the object to provide access to some data and functions without sharing its internal details and design, and provides some protection from data corruption
![Page 9: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/9.jpg)
13.2 Introduction to Classes
![Page 10: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/10.jpg)
13-10
Introduction to Classes
• Objects are created from a class
• Format:
class ClassName
{
declaration;
declaration;
};
![Page 11: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/11.jpg)
13-11
Class Example
![Page 12: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/12.jpg)
13-12
Access Specifiers
• Used to control access to members of the class
• public: can be accessed by functions outside of the class
• private: can only be called by or accessed by functions that are members of the class
![Page 13: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/13.jpg)
13-13
Class Example
Private Members
Public Members
![Page 14: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/14.jpg)
13-14
More on Access Specifiers
• Can be listed in any order in a class
• Can appear multiple times in a class
• If not specified, the default is private
![Page 15: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/15.jpg)
13-15
Using const With Member Functions
• const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object.
![Page 16: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/16.jpg)
13-16
Defining a Member Function
• When defining a member function: – Put prototype in class declaration
– Define function using class name and scope resolution operator (::)
int Rectangle::setWidth(double w)
{
width = w;
}
![Page 17: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/17.jpg)
13-17
Accessors and Mutators
• Mutator: a member function that stores a value in a private member variable, or changes its value in some way
• Accessor: function that retrieves a value from a private member variable. Accessors do not change an object's data, so they should be marked const.
![Page 18: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/18.jpg)
13.3 Defining an Instance of a Class
![Page 19: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/19.jpg)
13-19
Defining an Instance of a Class
• An object is an instance of a class
• Defined like structure variables: Rectangle r;
• Access members using dot operator: r.setWidth(5.2);
cout << r.getWidth();
• Compiler error if attempt to access private member using dot operator
![Page 20: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/20.jpg)
13-20
![Page 21: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/21.jpg)
13-21
Program 13-1 (Continued)
![Page 22: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/22.jpg)
13-22
Program 13-1 (Continued)
![Page 23: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/23.jpg)
13-23
Program 13-1 (Continued)
![Page 24: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/24.jpg)
13-24
Avoiding Stale Data
• Some data is the result of a calculation.
• In the Rectangle class the area of a rectangle is calculated. – length x width
• If we were to use an area variable here in the Rectangle class, its value would be dependent on the length and the width.
• If we change length or width without updating area, then area would become stale.
• To avoid stale data, it is best to calculate the value of that data within a member function rather than store it in a variable.
![Page 25: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/25.jpg)
13-25
Pointer to an Object
• Can define a pointer to an object: Rectangle *rPtr;
• Can access public members via pointer: rPtr = &otherRectangle;
rPtr->setLength(12.5);
cout << rPtr->getLenght() << endl;
![Page 26: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/26.jpg)
13-26
Dynamically Allocating an Object
• We can also use a pointer to dynamically allocate an object.
![Page 27: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/27.jpg)
13.4 Why Have Private Members?
![Page 28: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/28.jpg)
13-28
Why Have Private Members?
• Making data members private provides data protection
• Data can be accessed only through public functions
• Public functions define the class’s public interface
![Page 29: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/29.jpg)
13-29
Code outside the class must use the class's public member functions to interact with the object.
![Page 30: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/30.jpg)
13.5 Separating Specification from Implementation
![Page 31: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/31.jpg)
13-31
Separating Specification from Implementation
– Place class declaration in a header file that serves as the class specification file. Name the file ClassName.h, for example, Rectangle.h
– Place member function definitions in ClassName.cpp, for example, Rectangle.cpp File should #include the class specification file
– Programs that use the class must #include the class specification file, and be compiled and linked with the member function definitions
![Page 32: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/32.jpg)
13.6 Inline Member Functions
![Page 33: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/33.jpg)
13-33
Inline Member Functions
• Member functions can be defined
– inline: in class declaration
– after the class declaration
• Inline appropriate for short function bodies: int getWidth() const
{ return width; }
![Page 34: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/34.jpg)
13-34
Rectangle Class with Inline Member Functions 1 // Specification file for the Rectangle class 2 // This version uses some inline member functions.
3 #ifndef RECTANGLE_H
4 #define RECTANGLE_H
5
6 class Rectangle
7 {
8 private:
9 double width;
10 double length;
11 public:
12 void setWidth(double);
13 void setLength(double);
14
15 double getWidth() const
16 { return width; }
17
18 double getLength() const
19 { return length; }
20
21 double getArea() const
22 { return width * length; }
23 };
24 #endif
![Page 35: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/35.jpg)
13-35
Tradeoffs – Inline vs. Regular Member Functions
• Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc.
• Code for an inline function is copied into program in place of call – larger executable program, but no function call overhead, hence faster execution
![Page 36: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/36.jpg)
Constructors
13.7
![Page 37: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/37.jpg)
13-37
Constructors
• Member function that is automatically called when an object is created
• Purpose is to construct an object
• Constructor function name is class name
• Has no return type
![Page 38: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/38.jpg)
13-38
![Page 39: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/39.jpg)
13-39
Continues...
![Page 40: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/40.jpg)
13-40
Contents of Rectangle.ccp Version3 (continued)
![Page 41: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/41.jpg)
13-41
![Page 42: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/42.jpg)
13-42
Default Constructors
• A default constructor is a constructor that takes no arguments.
• If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing.
• A simple instantiation of a class (with no arguments) calls the default constructor:
Rectangle r;
![Page 43: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/43.jpg)
Passing Arguments to Constructors
13.8
![Page 44: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/44.jpg)
13-44
Passing Arguments to Constructors
• To create a constructor that takes arguments:
– indicate parameters in prototype: Rectangle(double, double);
– Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }
![Page 45: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/45.jpg)
13-45
Passing Arguments to Constructors
• You can pass arguments to the constructor when you create an object:
Rectangle r(10, 5);
![Page 46: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/46.jpg)
13-46
More About Default Constructors
• If all of a constructor's parameters have default arguments, then it is a default constructor. For example:
Rectangle(double = 0, double = 0);
• Creating an object and passing no arguments will cause this constructor to execute: Rectangle r;
![Page 47: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/47.jpg)
13-47
Classes with No Default Constructor
• When all of a class's constructors require arguments, then the class has NO default constructor.
• When this is the case, you must pass the required arguments to the constructor when creating an object.
![Page 48: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/48.jpg)
Destructors
13.9
![Page 49: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/49.jpg)
13-49
Destructors
• Member function automatically called when an object is destroyed
• Destructor name is ~classname, e.g., ~Rectangle
• Has no return type; takes no arguments
• Only one destructor per class, i.e., it cannot be overloaded
• If constructor allocates dynamic memory, destructor should release it
![Page 50: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/50.jpg)
13-50
![Page 51: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/51.jpg)
13-51
Contents of InventoryItem.h Version1 (Continued)
![Page 52: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/52.jpg)
13-52
![Page 53: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/53.jpg)
13-53
![Page 54: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/54.jpg)
13-54
Constructors, Destructors, and Dynamically Allocated Objects
• When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *r = new Rectangle(10, 20);
• When the object is destroyed, its destructor executes: delete r;
![Page 55: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/55.jpg)
Overloading Constructors
13.10
![Page 56: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/56.jpg)
13-56
Overloading Constructors
• A class can have more than one constructor
• Overloaded constructors in a class must have
different parameter lists:
Rectangle();
Rectangle(double);
Rectangle(double, double);
![Page 57: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/57.jpg)
13-57
From InventoryItem.h (Version 2)
![Page 58: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/58.jpg)
13-58
From InventoryItem.h (Version 2)
![Page 59: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/59.jpg)
13-59
From InventoryItem.h (Version 2)
![Page 60: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/60.jpg)
13-60
Only One Default Constructor and One Destructor
• Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters
Square();
Square(int = 0); // will not compile
• Since a destructor takes no arguments, there can only be one destructor for a class
![Page 61: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/61.jpg)
13-61
Member Function Overloading
• Non-constructor member functions can also be overloaded:
void setCost(double);
void setCost(char *);
• Must have unique parameter lists as for constructors
![Page 62: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/62.jpg)
Using Private Member Functions
13.11
![Page 63: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/63.jpg)
13-63
Using Private Member Functions
• A private member function can only be called by another member function
• It is used for internal processing by the class, not for use outside of the class
• See the createDescription function in InventoryItem.h (Version 3)
![Page 64: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/64.jpg)
Arrays of Objects
13.12
![Page 65: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/65.jpg)
13-65
Arrays of Objects
• Objects can be the elements of an array: InventoryItem inventory[40];
• Default constructor for object is used when array is defined
![Page 66: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/66.jpg)
13-66
Arrays of Objects
• Must use initializer list to invoke constructor that takes arguments: InventoryItem inventory[3] =
{ "Hammer", "Wrench", "Pliers" };
![Page 67: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/67.jpg)
13-67
Arrays of Objects
• If the constructor requires more than one argument, the initializer must take the form of a function call:
![Page 68: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/68.jpg)
13-68
Arrays of Objects
• It isn't necessary to call the same constructor for each object in an array:
![Page 69: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/69.jpg)
13-69
Accessing Objects in an Array
• Objects in an array are referenced using subscripts
• Member functions are referenced using dot notation: inventory[2].setUnits(30);
cout << inventory[2].getUnits();
![Page 70: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/70.jpg)
13-70
![Page 71: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/71.jpg)
13-71
Program 13-3 (Continued)
![Page 72: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/72.jpg)
Classes vs. Structs
![Page 73: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/73.jpg)
73
Structs and Classes: Similarities
• Essentially the same syntax
• Both are used to model objects with multiple attributes (characteristics)
– represented as data members
– also called fields
• Thus, both are used to process non-homogeneous data sets.
![Page 74: Introduction to Classes (Chapter 13.1 – 13.12)fac-staff.seattleu.edu/zhuy/web/teaching/Winter13/cpsc152/Classes.pdf13-5 Object-Oriented Programming Terminology •class: like a struct](https://reader034.fdocuments.us/reader034/viewer/2022050121/5f513eb5e5f918157102af27/html5/thumbnails/74.jpg)
74
Differences Structs vs. Classes
• Members public by default
• Can be specified private
• Class members are private by default
• Can be specified public