ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Const Vs Nonconst Func…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    2 min read
    Topic 8 of 23
    Topic Study Note Illustration
    08

    Const vs non-const functions

    Const vs Non-Const Functions in C++

    Const vs Non-Const Functions in C++

    In C++, member functions of a class can be marked as const to indicate that they do not modify the object’s data members. This helps protect data and lets the compiler catch errors if you accidentally try to change something in a function that shouldn't.


    Non-Const Function

    A non-const function can modify the object’s data members. It is the default type of function if you don’t add const after the function declaration.

    Example:

    class Box {
    private:
        int length;
    public:
        void setLength(int l) {
            length = l;  // Can modify data
        }
    };
    

    Const Function

    A const member function cannot change any data members of the class. It is declared by adding const after the function parentheses.

    Syntax:

    return_type function_name() const
    

    Example:

    class Box {
    private:
        int length;
    public:
        int getLength() const {
            // length = 10;  ❌ Not allowed, will cause error
            return length;
        }
    };
    

    In this example, getLength() is a const function, so it cannot change the value of length.


    Why Use Const Functions:

    1. Safety: Prevents accidental modification of object data.
    2. Clarity: Shows clearly which functions are meant to just read data.
    3. Allows calling on const objects.

    Calling Functions on Const Objects

    You must use const functions if you're calling them on a const object.

    const Box b1;
    b1.getLength();  // ✔ Allowed only if getLength() is a const function
    

    If getLength() is not const, it will give a compiler error.


    Summary Table:

    Feature Const Function Non-Const Function
    Modifies object data No Yes
    Can be called by const object Yes No
    Syntax function() const function()

    Using const with functions helps write safer, more reliable code, especially when working with large classes or sharing objects across different parts of a program.

    ← Previous topic 7
    Access modifiers
    Next topic 9 →
    Static data members & functions

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    2mremaining
    303words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming