---
title: "Constant object and constant function"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:const-object-function"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/const-object-function
site: BCT Engineering
---

# Constant object and constant function

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2074 Asoj, 5 marks
- Video: [#11 [oop] - Constant Object Constant Member Function](https://www.youtube.com/watch?v=lVtnHm1zc4o) — Adel Nasim
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/const-object-function
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Explain the relation between constant object and constant function with example.

## Short answer

A const member function promises not to modify the object's data and can be called by const objects; non-const functions cannot be called on const objects. Example: class Test with const get() and non-const set(); a const object t1 can call get() but not set(), while a non-const object can call both.

## Answer

#### Constant member function

A member function declared with `const` after its parameter list promises not to change any data member of the object. The compiler gives an error if it tries.

```cpp
int getValue() const;
```

#### Constant object

An object declared `const` cannot be modified after creation. Its data must be set by the constructor.

```cpp
const Test t(10);
```

#### Relation

- A **const object can call only const member functions** (and constructor and destructor). Calling a non-const function on it is an error, since that function might modify the object.
- A **non-const object can call both** const and non-const functions.
- So const functions are the way to give read-only access to a const object. Functions that only display or return values should be made const.

#### Example

```cpp
#include <iostream>
using namespace std;

class Test
{
    int value;
public:
    Test(int v) { value = v; }
    int get() const            // const function: cannot modify value
    {
        return value;
    }
    void set(int v)            // non-const function
    {
        value = v;
    }
};

int main()
{
    const Test t1(10);
    cout << t1.get() << endl;  // OK: const function on const object
    // t1.set(20);             // Error: non-const function on const object

    Test t2(5);
    t2.set(8);                 // OK
    cout << t2.get() << endl;  // OK, const function on normal object
    return 0;
}
```

#### Output

```text
10
8
```

A `mutable` data member is the only member a const function may change.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does int get() const; promise?
   A. It won't change data members
   B. It returns a constant
   C. It is static
   D. It can only be called once
   Answer: A. A const after the parameter list promises not to modify the object. (Key point: Const function meaning)

2. Which functions can a const object call?
   A. Only const member functions
   B. Any function
   C. Only non-const functions
   D. Only static
   Answer: A. A const object can call only const member functions (plus constructor and destructor). (Key point: Const object calls)

3. Which functions can a non-const object call?
   A. Both const and non-const
   B. Only const
   C. Only non-const
   D. None
   Answer: A. Non-const objects can call both kinds. (Key point: Non-const object)

4. What may a const function change?
   A. Any member
   B. Only a mutable data member
   C. Nothing at all
   D. Static functions
   Answer: B. A mutable member is the only member a const function may modify. (Key point: Mutable)

## Related in Object Oriented Programming

- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Inline function: significance and limits](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Ainline-function) — Object Oriented Programming, chapter 3, asked 3×
- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×

## Related in other subjects

- [White noise: relation between PSDF and AC function](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Awhite-noise-psdf-ac-relation) — Communication Systems, chapter 9
- [PSD–autocorrelation relation; stereo FM](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Apsd-autocorrelation-stereo-fm) — Communication Systems, chapter 3
- [Parseval's theorem; PSD–autocorrelation relation for white noise](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Aparseval-psd-autocorrelation-white-noise) — Communication Systems, chapter 9
- [R-H relation between K and T for stability](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Arh-k-t-relation) — Control Systems, chapter 4
