---
title: "Task of const keyword"
subject: "Object Oriented Programming"
chapter: 2
key: "semester_3/object_oriented_programming:const-keyword-task"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/const-keyword-task
site: BCT Engineering
---

# Task of const keyword

Object Oriented Programming · Chapter 2: Introduction to C++

- Asked in: 2070 Chaitra, 2 marks
- Video: [CONST in C++](https://www.youtube.com/watch?v=4fJBrditnJU) — The Cherno
- 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-keyword-task
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-2

## Question

What is the task of const keyword?

## Short answer

The const keyword declares an entity as read-only after initialization, preventing modification. It applies to variables, pointers, function arguments, member functions, and objects, ensuring compile-time safety against unintended changes.

## Answer

The `const` keyword declares that a value cannot be modified after initialisation. Any attempt to change it gives a compile-time error.

**Uses:**

1. **Constant variable** - replaces `#define` and is type safe: `const float PI = 3.14159;`
2. **Pointer to constant** - the data cannot change through the pointer: `const int *p;`
3. **Constant pointer** - the pointer cannot point elsewhere: `int *const p = &x;`
4. **Constant function argument** - protects the argument: `void show(const int &x);`
5. **Constant member function** - promises not to change the object's data: `int getX() const { return x; }`
6. **Constant object** - can call only const member functions.

```cpp
const int MAX = 100;
// MAX = 200;   // error: assignment of read-only variable
```

## Check yourself

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

1. What does const do?
   A. Allocates memory
   B. Prevents modification after initialisation
   C. Makes variable global
   D. Makes function inline
   Answer: B. Assignment gives compile error. (Key point: Task)

2. Which declares a constant pointer?
   A. const int *p;
   B. int const x;
   C. int *const p = &x;
   D. const int &p;
   Answer: C. Pointer cannot point elsewhere. (Key point: Constant pointer)

3. What does int getX() const promise?
   A. Not to return
   B. Run faster
   C. Be static
   D. Not to change the object's data
   Answer: D. Constant member function. (Key point: Const member function)
