CS301-Lec19 handout - CS301 Data Structures Lecture No. 19...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 19 ___________________________________________________________________ Data Structures Lecture No. 19 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.4 Summary Usage of const keyword Degenerate Binary Search Tree AVL tree Usage of const keyword In the previous lecture, we dealt with a puzzle of constant keyword. We send a parameter to a function by using call by reference and put const with it. With the help of the reference variable, a function can change the value of the variable. But at the same time, we have used the const keyword so that it does not effect this change. With the reference parameter, we need not to make the copy of the object to send it to the calling function. In case of call by value, a copy of object is made and placed at the time of function calling in the activation record. Here the copy constructor is used to make a copy of the object. If we don’t want the function to change the parameter without going for the use of time, memory creating and storing an entire copy of, it is advisable to use the reference parameter as const . By using the references, we are not making the copy. Moreover, with the const keyword, the function cannot change the object. The calling function has read only access to this object. It can use this object in the computation but can not change it. As we have marked it as constant, the function cannot alter it, even by mistake. The language is supportive in averting the mistakes. There is another use of keyword const . The const keyword appears at the end of class member’s function signature as: EType& findMin( ) const; This method is used to find the minimum data value in the binary tree. As you have noted in the method signature, we had written const at the end. Such a function cannot change or write to member variables of that class. Member variables are those which appear in the public or private part of the class. For example in the BinaryTree, we have root as a member variable. Also the item variable in the node class is the member variable. These are also called state variables of the class. When we create an object from the factory, it has these member variables and the methods of this class which manipulate the member variables. You will also use set and get methods, generally employed to set and get the values of the member variables. The member function can access and change the public and private member variables of a class.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 19 ___________________________________________________________________ Suppose, we want that a member function can access the member variable but cannot change it. It means that we want to make the variables read only for that member function. To impose that constraint on the member function, a programmer can put the keyword const in the end of the function. This is the way in the C++ language. In other languages, there may be alternative methods to carry out it. These features are
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

CS301-Lec19 handout - CS301 Data Structures Lecture No. 19...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online