Home | | Programming and Data Structure II | Constant Members

Chapter: Programming and Data structures : Object Oriented Programming Fundamentals

Constant Members

Constant Objects and Constant Functions

CONSTANT MEMBERS:

 

Constant Objects and Constant Functions

 

·        Once the object is declared as constant it cannot be modified by any function.

 

·        Initially the value for the data members is set by constructor during the object creation.

 

·        Constant objects can access only the constant member function .The constant function is read only function it cannot alter the value of object data members

 

 

Syntax- Constant Function & Constant objects

 

class class_name{

 

class_name(parameters)

 

{

 

 

initialize the datamember with parameters;

 

}

 

return_type function_name() const        //Constant member function

 

{

 

//read only function

 

}

 

};

 

void main()

 

{

 

const class_name object( parameter);   //constant objects

 

object.function();       //constant objects access Constant member function

 

}

 

 

 

Ex:

 

void printdetails() const

 

{

 

cout<<”Roll No is:”<<rollno;

cout<<”Name is:”<<name;

cout<<”Address is:”<<address;

rollno=10;  / / the following line is erroneous

 

}

 

 

Mutable Data Members

 

In some cases if the static function need to modify the data member of an object, then the data member should be declared as mutable, so that static function can modify it.

 

mutable int rollno; void printdetails() const

 

{

 

rollno=10;   //it is allowed

 

}

 

Example program for constant function and constant objects

 

#include<iostream.h>

 

#include<conio.h>

class time

{

 

public:

 

int hour,minute,second;

 

time(int temphr,int tempmin,int tempsec)

 

{

 

hour=temphr;

 

minute=tempmin;

 

second=tempsec;

 

}

 

void disp() const               //constant member function

 

{

 

cout<<"Time is:"<<hour<<"Hour:\t"<<minute<<"Minutes:\t"<<second<<"Seconds:\n"; }

 

~time()

 

{

 

}

 

void get() const

 

{

in>>hour>>minute>>second; }

};      

void main()

{       

clrscr();      

const time t1(8,55,50);   //Constant objects

t1.disp();    

t1.get();       //cannot modify the datamembers of t1

t1.disp();    

getch();       

}       

 

 

Output

 

Time is:8Hour: 55Minutes:     50Seconds:

 

5

 

50

 

40

 

Time is:8Hour: 55Minutes:     50Seconds:

 

 

 

In this example, the object of this time class is set with the default value of 8 hour 55 minutes and 50 seconds and it is initialized with constructor. Since this object t1 is a n constant object it cannot be modified and it can access only the constant function in a class.

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : Object Oriented Programming Fundamentals : Constant Members |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.