Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Chapter 10.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Classes: A Deeper Look Systems Programming.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Chapter 7 Defining Your Own Data Types. 2 What Is a struct ?  A structure is a user-defined type You define it using the keyword struct so it is often.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Chapter 8 Destructor & Operator Overloading. 2 Destructor  A destructor is a function that is called when an object is no longer required. A constructor.
Inheritance One of the most powerful features of C++
Overview of Previous Lesson(s) Over View .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows.  It.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Object-Oriented Programming in C++
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
Understanding Polymorphism tMyn1 Understanding Polymorphism Polymorphism requires the use of derived classes. It always involves the use of a pointer to.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
11 Introduction to Object Oriented Programming (Continued) Cats.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Memory Management.
Constructors and Destructors
Overview 4 major memory segments Key differences from Java stack
Chapter 5 Classes.
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Introduction to Classes
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
Review: C++ class represents an ADT
Video: The Sound of Raining
Chapter 8 Destructor (P.323) & Operator Overloading
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving to Java B Smith: Save this for later or use in Fa06, or is this redundant? B Smith: Save this for later or use in Fa06, or is this redundant? B Smith: IS this too long? Should it be shortened? B Smith: IS this too long? Should it be shortened? B Smith: Covering derived classes more important B Smith: Covering derived classes more important

2 Overview

3 Destructors When a class object goes out of scope, Destructors “clean house”  When a function is called, it may work with temporary variables  A linked-list class might use a constructor to build nodes (malloc() ), and a destructor to free nodes (free() ) It’s good practice to clean up after your objects and free any allocated memory Use the name of the class, preceded by a tilde (~) Destructors receive no arguments and return no values

4 Destructors The class destructor neither takes an argument nor returns a value For the class Date, the prototype of the class destructor is  ~Date(); For the class Cat, the prototype of the class destructor is  ~Cat(); It's an error to specify a return value or parameters for a destructor. B Smith: redundant here? IS there a better place? B Smith: redundant here? IS there a better place?

5 Default Destructor All objects have been destroyed automatically by the default destructor In the examples where we’ve not explicitly defined a destructor, the compiler creates one Note that the default destructor does not automatically delete/free objects that have been allocated on the system heap (free store)!

6 int main() { Box Boxes[5]; // Array of Box objects declared Box Cigar(8.0, 5.0, 1.0); // Declare Cigar box Box Match(2.2, 1.1, 0.5); // Declare Match box // Initialize pointer to Cigar object address Box* pB1 = &Cigar; Box* pB2 = 0; // Pointer initialized to null cout << endl << "Volume of Cigar is " Volume(); // Volume of obj. pointed to pB2 = Boxes; // Set to address of array Boxes[2] = Match; // Set 3rd element to Match cout << endl // Now access thru pointer Volume(); cout << endl; return 0; } B Smith: Should really be focused on using references B Smith: Should really be focused on using references

7 A Simple Destructor // Ex7_01.cpp // Class with an explicit destructor #include using namespace std; class Box // Class definition at global scope { public: // Destructor definition ~Box() { cout << "Destructor called." << endl; } // Constructor definition Box(double lv=1.0, double bv=1.0, double hv=1.0) { cout << endl << "Constructor called."; length = lv; // Set values of breadth = bv; // data members height = hv; } // Function to calculate the volume of a box double Volume() { return length * breadth * height; } // Function to compare two boxes which // returns true if the first is greater // than the second, and false otherwise int compare(Box* pBox) { return Volume() > pBox->Volume(); } private: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches }; int main() { Box Boxes[5]; // Array of Box objects declared Box Cigar(8.0, 5.0, 1.0); // Declare Cigar box Box Match(2.2, 1.1, 0.5); // Declare Match box // Initialize pointer to Cigar object address Box* pB1 = &Cigar; Box* pB2 = 0; // Pointer initialized to null cout << endl << "Volume of Cigar is " Volume(); // Volume of obj. pointed to pB2 = Boxes; // Set to address of array Boxes[2] = Match; // Set 3rd element to Match cout << endl // Now access thru pointer Volume(); cout << endl; return 0; } B Smith: Use colors and provide as a basic handout. Put online as a separate file. B Smith: Use colors and provide as a basic handout. Put online as a separate file. B Smith: only useful in that it shows what happens with an array. Student had a great question regarding which Constructor is called. Better explanation needed. ….. Uhhh, here, there is only one constructor B Smith: only useful in that it shows what happens with an array. Student had a great question regarding which Constructor is called. Better explanation needed. ….. Uhhh, here, there is only one constructor

8 // Ex6_03.cpp // Using a constructor #include using namespace std; class Box // Class definition at global scope { private: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches public: // Constructor definition Box(double lv, double bv, double hv) { cout << endl << "Constructor called."; length = lv; // Set values of breadth = bv; // data members height = hv; } // Function to calculate the volume of a box double Volume() { return length * breadth * height; } }; Box Class

9 int main(void) { Box Box1(78.0,24.0,18.0); // Declare and initialize Box1 Box CigarBox(8.0,5.0,1.0); // Declare and initialize CigarBox double volume = 0.0; // Store the volume of a box here volume = Box1.Volume(); // Calculate volume of Box1 cout << endl << "Volume of Box1 = " << volume; cout << endl << "Volume of CigarBox = " << CigarBox.Volume(); cout << endl; return 0; } Box Class Client

10 How It Works The constructor, Box(), has been written with three parameters of type double, corresponding to the initial values for the length, breadth and height members of a Box object. The first statement in the constructor outputs a message so that we can tell when it's been called. You wouldn't do this in production programs, but, since it's very helpful in showing when a constructor is called, it's often used when testing a program. We'll use it regularly for the purposes of illustration. The code in the body of the constructor is very simple. It just assigns the arguments passed to the corresponding data members. If necessary, we could also include checks that valid, non-negative arguments are supplied and, in a real context, you probably would want to do this, but our primary interest here is in seeing how the mechanism works. Within main(), we declare the object Box1 with initializing values for the data members length, breadth, and height, in sequence. These are in parentheses following the object name. This uses the functional notation for initialization, which can also be applied to initializing ordinary variables of basic types. We also declare a second object of type Box, called CigarBox, which also has initializing values. The volume of Box1 is calculated using the member function Volume() as in the previous example and is then displayed on the screen. We also display the value of the volume of CigarBox. The output from the example is: The first two lines are output from the two calls of the constructor, Box(), once for each object declared. The constructor that we've supplied in the class definition is automatically called when a Box object is declared, so both Box objects are initialized with the initializing values appearing in the declaration. These are passed to the constructor as arguments, in the sequence that they are written in the declaration. As you can see, the volume of Box1 is the same as before and CigarBox has a volume looking suspiciously like the product of its dimensions, which is quite a relief.

11 The Default Constructor Try modifying the last example by adding the declaration for Box2 that we had previously:  Box Box2; // Declare Box2 of type Box Here, we've left Box2 without initializing values. When you rebuild this version of the program, you'll get the error message:  error C2512: 'Box': no appropriate default constructor available

12 Supply a Default Constructor // Ex6_04.cpp // Supplying and using a default constructor #include using namespace std; class Box // Class definition at global scope { public: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches // Constructor definition Box(double lv, double bv, double hv) { cout << endl << "Constructor called."; length = lv; // Set values of breadth = bv; // data members height = hv; } // Default constructor definition Box() { cout << endl << "Default constructor called."; } // Function to calculate the volume of a box double Volume() { return length * breadth * height; } };

13 int main(void) { Box Box1(78.0,24.0,18.0); // Declare and initialize Box1 Box Box2; // Declare Box2 - no initial values Box CigarBox(8.0,5.0,1.0); // Declare and initialize CigarBox double volume = 0.0; // Store the volume of a box here volume = Box1.Volume(); // Calculate volume of Box1 cout << endl << "Volume of Box1 = " << volume; Box2.height = Box1.height - 10; // Define Box2 Box2.length = Box1.length/2.0; // members in Box2.breadth = 0.25*Box1.length; // terms of Box1 cout << endl << "Volume of Box2 = " << Box2.Volume(); cout << endl << "Volume of CigarBox = " << CigarBox.Volume(); cout << endl; return 0; } Box Class Client

14 Problems? class Box // Class definition at global scope { public: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches // Constructor definition Box(double lv = 1.0, double bv = 1.0, double hv = 1.0) { cout << endl << "Constructor called."; length = lv; // Set values of breadth = bv; // data members height = hv; } // Default constructor definition Box() { cout << endl << "Default constructor called."; } // Function to calculate the volume of a box double Volume() { return length * breadth * height; } };

15 The Fix // Ex6_05.cpp // Supplying default values for constructor arguments #include using namespace std; class Box // Class definition at global scope { public: double length; // Length of a box in inches double breadth; // Breadth of a box in inches double height; // Height of a box in inches // Constructor definition Box(double lv=1.0, double bv=1.0, double hv=1.0) { cout << endl << "Constructor called."; length = lv; // Set values of breadth = bv; // data members height = hv; } // Function to calculate the volume of a box double Volume() { return length * breadth * height; } };

16 int main(void) { Box Box2; // Declare Box2 - no initial values cout << endl << "Volume of Box2 = " << Box2.Volume(); cout << endl; return 0; }

17 Destructors and malloc()/free() Allocate memory for class data members dynamically with new or malloc Memory requested is subseqently free’d with free() Destructors enable a type of “garbage collection”  they’re automatically run when an object goes out of scope With a suitable destructor, memory is automatically cleaned up B Smith: Discuss malloc/free and new/delete! Use linked-list as an example?? Or Josephus?? B Smith: Discuss malloc/free and new/delete! Use linked-list as an example?? Or Josephus??

18 delete/new

19 Destructor using delete[] //Listing 02_01 class Message { private: char* pmessage; //Pointer to object text string public: // Function to display a message void ShowIt(void) { cout << endl << pmessage; } // Constructor definition Message(const char* text = "Default message") { // Allocate space for text pmessage = new char[strlen(text)+1]; // Copy text to new memory strcpy(pmessage, text); } ~Message(); // Destructor prototype }; // Listing 02_02 // Destructor to free memory allocated by new Message::~Message() { cout << "Destructor called." // track what happens << endl; delete[] pmessage; // Free memory assigned to ptr // Ex7_02.cpp // Using a destructor to free memory #include // For stream I/O #include // For strlen() and strcpy() using namespace std; // Put the Message class definition here (Listing 02_01) // Put the destructor definition here (Listing 02_02) int main() { // Declare object Message Motto("A miss is as good as a mile."); // Dynamic object Message* pM = new Message("A cat can look at a queen."); Motto.ShowIt(); // Display 1st message pM->ShowIt(); // Display 2nd message cout << endl; // delete pM; // Manually delete object created with new return 0; }

20 //Listing 02_01 class Message { private: char* pmessage; //Pointer to object text string public: // Function to display a messageoid ShowIt(void) { cout << endl << pmessage; } // Constructor definition Message(const char* text = "Default message") { // Allocate space for text pmessage = new char[strlen(text)+1]; // Copy text to new memory strcpy(pmessage, text); } ~Message(); // Destructor prototype }; class Message

21 // Listing 02_02 // Destructor to free memory allocated by new Message::~Message() { cout << "Destructor called." // track what happens << endl; delete[] pmessage; // Free the memory assigned to ptr } Message Destructor

22 Message Class driver function // Using a destructor to free memory #include // For stream I/O #include // For strlen() and strcpy() using namespace std; // Put the Message class definition here (Listing 02_01) // Put the destructor definition here (Listing 02_02) int main() { // Declare object Message Motto("A miss is as good as a mile."); // Dynamic object Message* pM = new Message("A cat can look at a queen."); Motto.ShowIt(); // Display 1st message pM->ShowIt(); // Display 2nd message cout << endl; // delete pM; // Manually delete object created with new return 0; }