The C++ programming language

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
Introduction to Programming Lecture 39. Copy Constructor.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Memory Management Issues, Solutions, and Examples.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
More C++ Features True object initialisation
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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Chapter 14: More About Classes.
The C++ programming language
By Muhammad Waris Zargar
Andy Wang Object Oriented Programming in C++ COP 3330
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9 Classes: A Deeper Look, Part 1
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
The C++ programming language
Chapter 7: User-Defined Functions II
CS148 Introduction to Programming II
The C++ programming language
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
9-10 Classes: A Deeper Look.
The C++ programming language
The C++ programming language
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

The C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./0. The C++ programming language Handling objects having dynamically allocated members Objects having dynamically allocated data members Definition of the operator = ( ) function The String class

Objects having dynamically allocated data members Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./1. Objects having dynamically allocated data members In C++ language the = operator can be overloaded too. The default operation of it, namely copying the static fields of the right hand side object to the memory address or into the object that is determined by the left value standing on the left hand side can only be applied if there is not such pointer among static data members that points to a data that is part of the data members of the object but stored outside of the static area of the object. For assignment or copying the content of objects that have such "attachments" the programmer has to define the operator=() functions. Using the previous example the objects of the List class that allocate the names[ ] vector by the constructor and deallocates it by the destructor in the Heap memory are exactly such instances. Heap List list; int count; names[ ] t_name* names;

list2 = list1; // using the default = operator Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./2. An assignment performed using the default = operator could not copy the attachment that is in the Heap memory: List list2; list2 = list1; // using the default = operator int count; 8 t_name* names; Problem: a list1.names[ ] and the list2.names[ ] are the same, modifying one of them the other will be modified too, against the intention. Deleting one of the object the destructor frees the memory area which can be overwritten by a new allocation destroying such a manner the content of the names vector for the other object. 4873 Heap List list1; 4873 int count; 8 names[ ] t_name* names; 4873 The attached vector of list2 is left in the Heap and its deleting becomes impossible because the list1.names pointer overwrote the list2.names pointer.

List2.operator =(list1); Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./3. Solution: Own operator=() function for the objects of the List class, that will duplicate the names[ ] vector at assignment operations. List2.operator =(list1); List list2; Heap int count; 5912 8 names[ ] t_name* names; Thanks to the new = operator the contents of the vectors will be duplicated after assignment and can live their own life. 5912 List list1; int count; 4873 8 t_name* names; names[ ] 4873

Definition of the operator = ( ) function Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./4. Definition of the operator = ( ) function class List {… public: List& operator = (List& otherList ); // Declaration. ... }; … List& List::operator = (List& otherList) // Definition. { if (this != &otherList) // Else self-assignment: lista2 = lista2; { delete [ ] names; // Destroying the old attachment, works as a destructor. names = new t_name[otherList.count ]; // Allocation to prepare the copying. count= otherList.count; // Copying into the new vector from the other: for (int i = 0; i <count; i++) strcpy(names[ i ], otherList.names[ i ]); } return *this; // Gives back itself. }

Department of Information Engineering INFORMATION TECHNOLOGY dr Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./5. // The usage of the overloaded assignment operator: List list3(15), list4(17); // Defining of two list objects. void main( ) {… // The list objects can get new values here. list4 = list3; // Using the overloaded = operator. ... } Remarks: The input and output are transferred using references. This method is suggested in case of objects to avoid moving of data. Passing a reference means passing a pointer. The first test is needed to avoid reading from the just deleted vector when the operator is used in the rarely programmed list2 = list2 form. Though the assignment between List-type objects is solved by the overloaded = operator, the initialisation of a newly defined object by an other one raises similar problem. In the next initialisation List list1(23); … List list2 = list1; the introduced overloaded = operator does not work because the = sign means initialisation here.

Department of Information Engineering INFORMATION TECHNOLOGY dr Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./6. The solution is given for this problem by overloading the default work of the copy constructor. The form of this in case of List class is the next: class List {… public: List (List& otherList); // Declaration of the copy constructor. ... }; … List :: Lista(List& otherList) // Definition. { names = new t_name[otherList.count ]; // Allocation before copying. count = otherList.count; // Copying to the new vector from the other: for (int i = 0; i < count; i++) strcpy(names [ i ], otherList.names [ i ]); } It can be noticed that the copy constructor comes from the operator=() function leaving out the test, the deleting that corresponds to the task of the destructor and the returning of the object.

Department of Information Engineering INFORMATION TECHNOLOGY dr Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./7. Lastly let us solve the addition operator task that was mentioned in the introduction of the operator polymorphism theme! The operation that have to be fulfilled by the overloaded + operator is to join two lists and give the joined list. This result can immediately be assigned to a third list object by the overloaded = operator. The solution will be easy because our List class has already a plenty of functions. class List {… public: List operator+(List& otherList); // The declaration of the operator+() funct. ... }; … // If the operators are at disposal the programming becomes easy: void main( ) { … List l1(33),l2(22),l3(3); … l3= l1+ l2; ...} // Applying. List List :: operator+(List& otherList) // Definition. { List helpList = *this; // Creation and initialisation with the first list (copy constructor) helpList += otherList; // Adjoining the second list (operator+=() ) return helpList; }

Department of Information Engineering INFORMATION TECHNOLOGY dr Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 30./8. The String class We have discussed the essential information that are needed for programming objects having dynamic data structures too. The List class and similar dynamic classes accommodate themselves flexibly to the memory requirements in running time. Theirs objects can be created as global variables as shown in the earlier example, or as local variables in a block, or can be defined dynamically too. Theirs usage becomes easy due to the defined overloaded operators. In better C++ environments the String class is available. A string object is the same instance that can dynamically change the length of its data structure as the instances of the previously introduced List class. In the class had been defined all the services, operators that can be required for handling strings. Using the String class the character vectors can be forgotten because the String class hides them. Assigning, joining, comparisons with relational operators, etc. can be performed with ease of Pascal programming language. A very rich class-library makes C++ to a language that can be applied effectively.