Module 15: Programming in C++: Partha Pratim Das
Module 15: Programming in C++: Partha Pratim Das
Partha Pratim
Das
Module 15: Programming in C++
Objectives &
Outline
Const-ness
Constant
Objects
Constant Data
Department of Computer Science and Engineering
Members Indian Institute of Technology, Kharagpur
Credit Card
Example [email protected]
mutable
Members
Module 15
Constant
Objects
Constant
Member
Functions
Constant Data
Members
Credit Card
Example
mutable
Members
Summary
Module 15
Constant
mutable Data members - logical and bitwise const-ness
Member Example
Functions
Constant Data
logical and bitwise const-ness
Members Usage of mutable
Credit Card
Example
mutable
Members
Summary
Module 15
Constant
Member
Functions
Constant Data
Members
Credit Card
Example
mutable
Members
Summary
Like objects of built-in type, objects of user-defined types can also be made
Module 15
constant
Partha Pratim
Das
If an object is constant, none of its data members can be changed
The type of the this pointer of a constant object of class, say, MyClass is:
Objectives &
Outline
// Const Pointer to Const Object
Constant const MyClass * const this;
Objects
Constant
Member instead of
Functions
Constant Data
Members
void setMember(int i) const
Credit Card { myMember_ = i; } // data member cannot be changed
Example
mutable
Members
gives an error
Summary
Interesting, non-constant objects can invoke constant member functions (by
casting – we discuss later) and, of course, non-constant member functions
Constant objects, however, can only invoke constant member functions
All member functions that do not need to change an object must be
declared as constant member functions
NPTEL MOOCs Programming in C++ Partha Pratim Das 8
Program 15.03: Example:
Constant Member Functions
#include <iostream>
Module 15 using namespace std;
• Now myConstObj can invoke getMember() and print(), but cannot invoke setMember()
• Naturally myConstObj cannot update myPubMember
• myObj can invoke all of getMember(), print(), and setMember()
NPTEL MOOCs Programming in C++ Partha Pratim Das 9
Constant Data members
Often we need part of an object, that is, one or more data members to be
Module 15
constant (non-changeable after construction) while the rest of the data
Partha Pratim members should be changeable. For example:
Das
For an Employee: employee ID and DoB should be non-changeable
Objectives & while designation, address, salary etc. should be changeable
Outline For a Student: roll number and DoB should be non-changeable while
Constant year of study, address, gpa etc. should be changeable
Objects For a Credit Card: card number and name of holder should be
Constant non-changeable while date of issue, date of expiry, address, cvv
Member number gpa etc. should be changeable
Functions
Do this by making the non-changeable data members as constant
Constant Data
Members To make a data member constant, we need to put the const keyword
Credit Card before the declaration of the member in the class
Example
Module 15
We now illustrate constant data members with a complete
Partha Pratim
Das example of CreditCard class with the following supporting
Objectives &
classes:
Outline
String class
Constant
Objects Date class
Constant
Member Name class
Functions
Constant Data
Address class
Members
Credit Card
Example
mutable
Members
Summary
5321711934640027 David Cameron 10 Downing Street London SW1A 2AA 1/Jul/2017 1/Jun/2019 127
• We prefix Name holder with const. Now the holder name cannot be changed after construction
• In setHolder(), we get a compilation error for holder = h; in an attempt to change holder
• With const prefix Name holder becomes constant – unchangeable
5321711934640027 Sharlock Holmes 10 Downing Street London SW1A 2AA 1/Jul/2017 1/Jun/2019 127
Module 15
Constant
Member
Functions
Constant Data
Members
Credit Card
Example
mutable
Members
Summary
Module 15
Constant
Member
Functions
Constant Data
Members
Credit Card
Example
mutable
Members
Summary
Module 15
While a constant data member is not changeable even in a non-constant
Partha Pratim object, a mutable data member is changeable in a constant object
Das
mutable is provided to model Logical (Semantic) const-ness against the
Objectives &
default Bit-wise (Syntactic) const-ness of C++
Outline
Note that:
Constant
Objects
mutable is applicable only to data members and not to variables
Reference data members cannot be declared mutable
Constant Static data members cannot be declared mutable
Member
Functions const data members cannot be declared mutable
Constant Data If a data member is declared mutable, then it is legal to assign a value to it
Members from a const member function
Credit Card
Example Let us see an example
mutable
Members
Summary
• Here a MathObject is logically constant; but we use mutable members for computation
• Employee is not logically constant. If it is, then salary should also be const
• Design on right makes that explicit
Module 15
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Constant
Srijoni Majumdar, TA [email protected] 9674474267
Objects Himadri B G S Bhuyan, TA [email protected] 9438911655
Constant
Member
Functions
Constant Data
Members
Credit Card
Example
mutable
Members
Summary