Download presentation
Presentation is loading. Please wait.
1
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
2
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;
3
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.
4
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
5
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. }
6
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.
7
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.
8
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; }
9
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.