Constant member functions in OOP are designed to prevent modification of class data members, indicated by the 'const' keyword in their declaration. Objects can also be declared as const, allowing only const member functions to be invoked. In contrast, non-constant functions can modify an object's state and do not use the const keyword, with typical use cases including setters and state-changing methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views11 pages
const vs non const lect-8(1)
Constant member functions in OOP are designed to prevent modification of class data members, indicated by the 'const' keyword in their declaration. Objects can also be declared as const, allowing only const member functions to be invoked. In contrast, non-constant functions can modify an object's state and do not use the const keyword, with typical use cases including setters and state-changing methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11
Const member functions in OOP
• Constant member functions are those functions that are
denied permission to change the values of the data members of their class. To make a member function constant, the keyword const is appended to the function prototype and also to the function definition header. • Like member functions and member function arguments, the objects of a class can also be declared as const. An object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object. A const object can be created by prefixing the const keyword to the object declaration. Syntax • The const member function can be defined in three ways: 1. For function declaration within a class. return_type function_name() const; Example: int get_data() const; 2. For function definition within the class declaration. return_type function_name () const { //function body } Example: int get_data() const { //function body } 3. For function definition outside the class. return_type class_name::function_name() const { //function body } Example: int Demo :: get_data() const { } Important Points: When a function is declared as const, it can be called on any type of object, const object as well as non-const objects. Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the object initialization while declaring is possible only with the help of constructors. • A function becomes const when the const keyword is used in the function’s declaration. The idea of const functions is not to allow them to modify the object on which they are called.
Examples of Const Member Functions
The below oop program demo that data members can be
updated in a member function that is not constant. // Java program to demonstrate that data members can be // updated in a member function that is not constant.
public class Demo {
private int x;
// Member function to set data
public void setData(int a) { x = a; } // Non-constant member function // Data can be updated public int getData() { ++x; return x; }
public static void main(String[] args) {
Demo d = new Demo(); d.setData(10); System.out.println(d.getData()); } } Explanation • Class Definition: The Demo class has a private data member x. • setData Method: This member function is used to set the value of the data member x. • getData Method: This member function increments the data member x and returns its value. It is not constant, allowing it to modify the data member. • Main Method: • An object of Demo is created. • The setData function is called to set the value of x to 10. • The getData function is called, which increments x and prints its value. When you run this Java program, the output will be: 11 Non-Constant Functions In Object-Oriented Programming (OOP), a non-constant function (or non-const member function) is a member function that can modify the state of an object, i.e., it can change the values of the object's data members. This is in contrast to a constant function (or const member function), which guarantees not to alter the object's state. Characteristics of Non-Constant Functions
• Modifying State: The primary characteristic of a non-
constant function is that it can modify the state of an object. This means it can change the values of the object's data members.
• Absence of const Keyword: In languages like C++, non-
constant member functions do not have the const keyword after their declaration. In Java, the concept of const member functions does not exist, so all member functions are non-constant by default unless the data members themselves are marked as final. • Typical Use Cases: Non-constant functions are used for operations that need to modify the object's data. This includes setters, functions that perform calculations and update members, or any method that changes the internal state of the object.