Object Oriented Programming C++ Lecture No 7

Outline

  • The this pointer
  • const member functions
  • const member function arguments
  • const objects
  • const data members

The ‘this’ Pointer

 

  • In the Box example, we wrote the function volume() in terms of the class data member names.
  • Every object of box contains these data members.
  • There must be a mechanism for the function to refer to the members of the particular object for which it has been called.
  • When any class member function executes, it automatically contains a hidden pointer called this, which contains the address of the object for which the function was called.
  • Example

cout<<firstbox.volume();

The pointer this in the function volume will contain the address of firstbox.

  • When volume() is called on another Box object, this will be set to contain the address of that object.
  • When the data member length is accessed in the function volume() during execution, it is actually referring to

     this->length

  • Compiler takes care of adding the necessary pointer name this to the member names in the function.
  • Compiler will implement the function as:

double Box::volume()

{return this->length*this->breadth*this->height;}

 

 

Output

1

935

648

Press any key to continue.

  • In cases like volume and compareVolume functions, we don’t need to write this pointer explicitly.
  • We only need to use the this pointer explicitly in limited circumstances.

–We might use this explicitly to resolve ambiguity.

  • If a function parameter had the same name as a data member.

–We will also use this if we want to return the address of the current object from a member function.

  • Using this, any member function can find out the address of the object for which it is invoked.

     

Output

My Object’s Address is: 0012FF74

My Object’s Address is: 0012FF68

My Object’s Address is: 0012FF5C

 

  • Example: We add a new function to Point class: farAway.
  • This function will return the address of an object which has the largest distance from (0,0).

Example: The ‘this’ Pointer

Output

X= 100, Y= 50

The const Qualifier.

  • The keyword const (for constant) precedes the data type of a variable.
  • It specifies that the value of a variable will not change throughout the program.
  • An attempt to alter the value of a const variable elicits a compiler error.
  • Example

const float PI = 3.14159F;//type constant float

Const Function Arguments.

  • Passing an argument by reference can be used to allow a function to modify a variable in the calling program.
  • The main motivation behind pass by reference is efficiency.
  • If we want to pass an argument by reference for efficiency, but we want a guarantee that the function cannot modify it.
  • To obtain such a guarantee, we can apply the const modifier to the variable in the function declaration.

 

const and Classes

  • We can introduce more uses of const:

–On member functions

–On member function arguments

–On objects

–On data members

  • These concepts work together to provide some surprising benefits.

const Member Functions.

  • A const member function guarantees that it will never modify any of its class’s member data.
  • A member function is made into a constant member function by placing the keyword const after the declaration but before the method body.
  • If there is a separate method declaration, const must be used in both declaration and definition.

Const Member Function Arguments.

  • If we want to pass an argument to a member function by reference, and we don’t want the member function to modify it, then the argument should be made const in the member function declaration and definition.
  • If any attempt is made to modify a const member function argument, compiler flags an error.

A Distance Example

A Distance Example

Const Objects.

  • const can be applied to objects of classes, in a similar way, as it is applied to variables of basic data types.
  • When an object is declared as const, it cannot be modified.
  • We can only use const member functions with a const object, because they guarantee not to modify it.

Constant Objects and Const Member Functions.

  • A const member function can invoke only other const member functions, because a const member function is not allowed to alter an object’s state either directly or indirectly, that is, by invoking some non-const member function.

Const Objects.

Point Example.

Changing const Object Data Using mutable.

  • Ordinarily, when we create a const object, we want a guarantee the
    at none of its member data can be changed.
  • However, a situation occasionally arises where we want to create const objects that have some specific member data item that needs to be modified despite the object’s constness.
  • That’s where the mutable keyword comes in.

Const Data Members.

  • Instead of assignment statements constructor Initializer List can be used to initialize data members of an object.
  • Specially, to assign initial value to a constant data member using the constructor Initializer List is the only way.
  • Consider the class:

  • The example below is not correct, either:

  • The solution is to use constructor Initializer List.

Point to be Noted.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

eight + seventeen =