ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    COMP2111
    Progress0 / 23 topics
    Topics
    1. Introduction to object oriented design2. History and advantages of object oriented design3. Introduction to object oriented programming concepts4. Classes and objects5. Data encapsulation6. Constructors and destructors7. Access modifiers8. Const vs non-const functions9. Static data members & functions10. Function overloading11. Operator overloading12. Identification of classes and their relationships13. Composition and aggregation14. Inheritance15. Multiple inheritance16. Polymorphism17. Abstract classes and interfaces18. Generic programming concepts19. Function & class templates20. Standard template library21. Object streams22. Data and object serialization using object streams23. Exception handling
    COMP2111›Const vs non-const functions
    Object Oriented ProgrammingTopic 8 of 23

    Const vs non-const functions

    2 minread
    303words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time2 min
      Word count303
      Code examples0
      DifficultyBeginner