Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Alter/Modify the values of data members of a class inside const member function - Functions
Home
>>
Category
>>
C++ (MCQ) questions and answers
>>
Functions
Q. Can we alter/modify the values of data members of a class inside const member function?
- Published on 19 Oct 15
a.
Yes
b.
No
ANSWER: Yes
Related Content
Constructor and Destructor (
10
)
Operator Overloading (
11
)
Inheritance (
16
)
Polymorphism and Abstract Classes (
12
)
C++ basic concepts (
39
)
Exception Handling (
22
)
File Handling (
11
)
Functions (
20
)
Memory Management (
6
)
Pointers (
3
)
Discussion
ramesh
-Posted on 08 Oct 15
In general we cannot alter/modify the values of data members of a class inside const member function but if we declare data member as mutable then we can modify the data in cons function.
Example:
class CountDemo
{
int count;
public:
CountDemo()
{
count=0;
}
CountDemo(int c)
{
count=c;
}
void getCount() const
{
count++;
cout << "\n count= " << count;
}
};
void main()
{
CountDemo c(10);
c.getCount();
}
The above program will give the compile time error because getCount() function is constant, but if you declare count data member as mutable then you can modify the count variable.
➨
Post your comment / Share knowledge
Required!
Required!
Invalid Email Id!
Required!
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
MCQs
English
Tutorials
Download
▲