Lecture
No.10             

 

o  
There
are situations where designer wants to return reference to current object from
a function

o  
In
such cases reference is taken from this pointer like (*this)

Example

Student Student::setRollNo(int aNo)

{

           

            return
*this;

}

Student Student::setName(char *aName)

{

           

            return
*this;

}

Usage:

int main()

{

            Student
aStudent;

            Student
bStudent;


            bStudent
= aStudent.setName(“Ahmad”);

           

            bStudent
= aStudent.setName(“Ali”).setRollNo(2);

            return
0;

}

o  
Public
member functions exposed by a class are called interface.

o  
Separation
of implementation from the interface is good software engineering.

Benefits of
separating interface and implementation:

Consider
the example of following complex no. class, this complex no. class two forms of
implementations one is new and one is old implementation you can observe that
if you have separated interface and implementation then we can easily change
implementation without changing interface,

o  
There
are two representations of complex number

·        
Euler
form

z = x + i y

·        
 Phasor
form

z
= |z| (cos
q + i sin q)

z
is known as the complex modulus and
q is known as the
complex argument or phase

Example

Example

class Complex{    //old

            float
x;

            float
y;

public:

            void
setNumber(float i, float j){

                        x
= i;

                        y
= j;

            }

           

};

Example

class Complex{ //new

            float
z;

            float
theta;

public:

            void
setNumber(float i, float j){

                        theta =
arctan(j/i);

                       

            }

           

};

Advantages

  1. User is only
    concerned about ways of accessing data (interface)

  2. User has no
    concern about the internal representation and implementation of the class

Separation of
interface and implementation

In c++ generally we
can relate the concept of interface of a class to its header (.h) file and and
implementation of a class to its (.cpp) file. However it is not complete
separation of interface and implementation.

·        
Usually
functions are defined in implementation file (.cpp) while the class definition
is given in header file (.h)

·        
Some
authors also consider this as separation of interface and implementation

Student.h

class Student{

            int
rollNo;

public:

            void
setRollNo(int aRollNo);

            int
getRollNo();

           

};

Student.cpp

#include “student.h”

void Student::setRollNo(int aNo){

           

}

int Student::getRollNo(){

}

We only need to include header (.h) file in
main.cpp to use the Student class as shown below,

Main.cpp  (main file
to run the program)

#include “student.h”

int main(){

            Student
aStudent;

}

Some functions in our programs are general
purpose functions to show or access data, they are supposed to do read only
tasks only however there are chances that they can change the state of data
members of the class while accessing the data members due to programming
mistake, c++ provides the solution of this problem using constant member
functions.

We make those functions as constant who
need only read only access (for example such functions that will only display
data or will return the value of data members).

When we make them constant compiler
generates an error if these functions try to change the value of data members
of the class.

const Member
Functions

Keyword
const is placed at the end of the parameter list to make any function as
constant.

Declaration:

Inside class

class ClassName{

            ReturnVal
Function() const;

};

Definition:

Outside class

ReturnVal ClassName::Function() const{

           

}

Example

class Student{

public:

            int
getRollNo() const            {

                        return
rollNo;

            }

};

const Functions

  • Constant
    member functions cannot modify the state of any object

  • They are just
    “read-only”

  • Errors due to
    typing are also caught at compile time

Example

Consider
the function given below that is being used to check if roll no is equal to
entered value if in this function we replace comparison statemtn == with
assignment = statement it will compile correctly but whole code logic will
change and we will get un expected result,

bool Student::isRollNo(int aNo){

            if(rollNo
= = aNo){

                        return
true;

            }

            return
false;

}

Example

bool Student::isRollNo(int aNo){

            /*undetected
typing mistake*/

            if(rollNo
= aNo){

                        return
true;

            }

            return
false;

}

But
if we have implemented as constant then compiler will catch this error and will
produce compile time error as shown below,

Example

bool Student::isRollNo

                                                            (int
aNo)const{

            /*compiler
error*/

            if(rollNo
= aNo){

                        return
true;

            }

            return
false;

}

const Functions

Constructors and Destructors cannot be const
because
Constructors and destructors are used to modify the object to a
well defined state or to clean the memory occupied by the object.

Example

class Time{

public:

            Time()
const {}   //error…

            ~Time()
const {}  //error…

};

const Function

  • Constant member function cannot
    change data member

  • We cannot call non constant
    functions in constant functions because non constant member functions may
    have code for changing state of the object that is not allowed in the
    constant functions.

Example

class Student{

            char
* name;

public:

            char
*getName();

            void
setName(char * aName);

            int
ConstFunc() const{

                        name = getName();   //error

                        setName(“Ahmad”);//error

            }

};

As
we know that when a class function is called an implicit this pointer is passed
to tell the function about the object it has to operate same is true for
constant function with the difference that it will bbe passed as constant
pointer to const data in case of constant member functions so that this pointer
can not be used now to change the value of data members of the object,

const Student *const this;

//
In case of constant member functions

instead
of

Student * const this;

//
In case of ordinary member functions

Facebook
Twitter
LinkedIn
WhatsApp

Leave a Reply

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