Video: The Sound of Raining

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

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.
Informática II Prof. Dr. Gustavo Patiño MJ
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Chapter 10.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
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.
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.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Dynamic memory allocation and Pointers Lecture 4.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Pointer to an Object Can define a pointer to an object:
Chapter 1.2 Introduction to C++ Programming
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
論語 先進第十一 暮春者,春服既成 冠者五六人,童子六七人 浴乎沂,風乎舞雩,詠而歸 ㄧˊ 〔~河〕水名,源出中國山東省,至江蘇省入海。
Standard Version of Starting Out with C++, 4th Edition
CSC241: Object Oriented Programming
CS410 – Software Engineering Lecture #11: C++ Basics IV
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Lecture 6 C++ Programming
8 Pointers.
This pointer, Dynamic memory allocation, Constructors and Destructor
void Pointers Lesson xx
Dynamic Memory Allocation Reference Variables
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 2 Elementary Programming
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Arrays Kingdom of Saudi Arabia
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
7 Arrays.
9-10 Classes: A Deeper Look.
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Recitation Course 0603 Speaker: Liu Yu-Jiun.
CS410 – Software Engineering Lecture #5: C++ Basics III
C++ Programming Lecture 20 Strings
Review: C++ class represents an ADT
Standard Version of Starting Out with C++, 4th Edition
登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
Chapter 8 (Part 2) Destructor & Operator Overloading (P.331)
送孟浩然之廣陵 ~李白 故人西辭黃鶴樓, 煙花三月下揚州。 孤帆遠影碧空盡, 惟見長江天際流。.
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Chapter 8 Destructor (P.323) & Operator Overloading
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
4.1 Introduction Arrays A few types Structures of related data items
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Video: The Sound of Raining

HW: TETRIS (7) & TETRIS (8)

Chapter 8 Destructor & Operator Overloading

Destructor A destructor is a function that is called when an object is no longer required. A constructor is a function which is called when a new object is created. A constructor is usually used to initiate an object. A destructor is usually used to destroy an object.

The Default Destructor The destructor for a class is a member function with the same name as the class, preceded by a tilde (~). For the CBox class, the prototype of the clas destructor is ~CBox(); A destructor has no parameters. Ex8_01.cpp on P.436 ~CBox() { cout << "Destructor called." << endl; } When some data members are dynamically allocated (see Chapter 4), a destructor must be used to destroy an object.

When Do We Dynamically Allocate Memory? Sometimes depending on the input data, you may allocate different amount of space for storing different types of variables at execution time int n = 0; cout << "Input the size of the vector - "; cin >> n; int vector[n]; error C2057: expected constant expression

Free Store (Heap, P.201) To hold a string entered by the user, there is no way you can know in advance how large this string could be. Free Store - When your program is executed, there is unused memory in your computer. You can dynamically allocate space within the free store for a new variable.

The new Operator Request memory for a double variable, and return the address of the space double* pvalue = NULL; pvalue = new double; Initialize a variable created by new pvalue = new double(9999.0); Use this pointer to reference the variable (indirection operator) *pvalue = 1234.0;

The delete Operator When you no longer need the (dynamically allocated) variable, you can free up the memory space. delete pvalue; Release memory pointed to by pvalue pvalue = 0; Reset the pointer to 0 After you release the space, the memory can be used to store a different variable later.

Allocating Memory Dynamically for Arrays Allocate a string of twenty characters char* pstr; pstr = new char[20]; delete [] pstr; Note the use of square brackets to indicate that you are deleting an array. pstr = 0; Set pointer to null

A Simple Example (cf. P.436) 3 a 5 b class CData { public: int* pdata; CData(int n=0) pdata = new(int); *pdata = n; cout << "Constructor called with initial value " << n << endl; } ~CData() cout << "Destructor called to release the memory storing " << *pdata << endl; delete pdata; }; int main() CData a(3); CData b(5); return 0; 3 a 5 b Constructor called with initial value 3 Constructor called with initial value 5 Destructor called to release the memory storing 5 Destructor called to release the memory storing 3

Q: Why Should I Write My Destructor? Why isn’t C++ compiler smart enough to automatically release the memory pointed by the pointer in my object? Consider some more complicated cases: 3 a 5 b c d

Class CMessage (1) P.438 Suppose you want to define a class Each object contains a text string. You don’t want to declare a data member as a large character array (like char [200]), So you’ll allocate memory in the free store for the message when an object is created. This is your constructor: CMessage(const char* text = "Default message") { pmessage = new char[strlen(text) + 1]; strcpy(pmessage, text); }

strlen, strcmp, strcpy #include <iostream> #include <cstring> using std::cout; using std::endl; int main() { char a[20] = "NCNU"; char b[20] = "Sunday"; cout << sizeof a << " " << strlen(a) << endl; // size = 20, string length = 4 if (strcmp(a,b) < 0) cout << "The string " << a << " is less than " << b << endl; strcpy(a, b); cout << a << endl; }

Destructors and Dynamic Memory Allocation (P.438) CMessage(const char* text = “Default message”) { pmessage = new char[strlen(text) + 1]; strcpy_s(pmessage, strlen(text)+1, text); } ~CMessage() cout << “Destructor called.” << endl; delete [] pmessage; A miss is as good as a mile. A cat can look at a queen. Destructor called. Q: What is the size of an object of the CMessage class?

Ex8_02.cpp on P.440 As the output indicates, the destructor is called only once. The object motto is created automatically, so the compiler also calls the destructor automatically. If you manually “delete pM”, it will free the memory pointed to by pM. Because the pointer pM points to a CMessage object, this causes the destructor to be invoked. Output becomes A miss is as good as a mile. A cat can look at a queen. Destructor called.

Dynamic Allocation Causes Troubles Not Only for Destructors class CData { public: int* pdata; CData(int n=0) pdata = new(int); *pdata = n; cout << "Constructor called with initial value " << n << endl; } void Print() cout << *pdata << endl; }; int main() CData a(3); CData b = a; a.Print(); b.Print(); *(a.pdata) = 5; 3 a 5 3 3 b 5 5

Behavior of a Default Copy Constructor CMessage motto1(“Radiation fades your genes.”); CMessage motto2(motto1); // Calls default copy constructor FIGURE 8-1 (P.442)

Q: What will the second motto2.ShowIt() display? CMessage motto1("A stitch in time saves nine."); CMessage motto2(motto1); motto2.ShowIt(); // Display 2nd message strcpy(motto1.pmessage, "Time and tide wait for no man."); motto1.ShowIt(); // Display 1st message Exercise: Let’s check figure8-1.cpp (P.442), but please ignore the error message at this moment. (Explained in next slides)

Implementing a Copy Constructor We don’t want the two objects sharing the same string in the memory. If motto1 is destroyed, the pointer in motto2 will become invalid. Let us implement a copy constructor to generate an object which is identical but independent of the old one. CMessage(const CMessage& initM) { pmessage = new char [ strlen(initM.pmessage) +1 ]; strcpy(pmessage, initM.pmessage); } Exercise: Modify Ex8_02.cpp (P.440) to implement this copy constructor.

Be sure to read the remaining part of Chapter 8 because we shall cover the topic of “operator overloading” in next class. Exercise Define a class CRational with two data members (numerator and denominator), and two member functions: Addition: Reduction (約分): Test your class with the following main program: int main() { CRational a(1, 4); CRational b(3, 4); CRational c = a.Addition(b); c.Print(); c.Reduction(); c.Print(); return 0; }