Need understanding of question regarding Const Function in C++

Viewed 102

Regarding my understanding of this question:

Question # 03:

  • Take any class from your course project. "Each member should have different class from their project".

  • Make one of its int data member constant.

  • Create 4 functions to see the use of Constant keyword. Each function should return the modified integer (not void).

Function Name Data to be modified Function Type
Int NonConstant( int x) Non-Constant Non-Constant
Int ConstantData( int x) Constant Non-Constant
Int NonFunction( int x) Non-Constant Constant
Int ConstantBoth( int x) Constant Constant

I'm not asking anyone to do this for me, I just want to understand what the question is really trying to say, as I'm getting so many errors in my code if I try to apply changes as shown in the figure.

#include<iostream>
using namespace std;

class Project
{
    private:
        
        int const x;
        int y;
        
    public:
        
        Project():x(5){}

        int NonConstant(int const x){
            this->y=x;
            return y;
        }
        
        int ConstData(const int x)
            return x;
        }
//      
//      const int ConstFunction(int x){
//              x++;
//              return x;
//      }
//        const void ConstBoth(void) const {
//          x++;
//      }
};


int main()
{
    Project obj;
    cout<<"NonConstant(1): "<<obj.NonConstant(1)<<endl;
    cout<<"ConstData(1): "<<obj.ConstData(1)<<endl;
    cout<<"ConstFunction(1): "<<obj.ConstFunction(1)<<endl;
//  obj.ConstBoth();
}
2 Answers

Is this what you wanted?
I had to rewrite your last function because it violated the principle of constant functions.

Edit: modified the functions according to Question #3.

#include<iostream>
using namespace std;

class Project
{
private:

    int const x;
    int y;

public:

    Project():x(5) {}

    // Non-constant data & function
    int NonConstant(int a)
    {
        this->y = a;
        return y;
    }

    // Constant data & Non-constant function
    int ConstData(const int a)
    {
        //a++; // You cannot modify const variables
        return a;
    }

    // Non-constant data & Constant function
    int ConstFunction(int a) const
    {
        a++;
        return a;
    }

    int ConstBoth(const int a) const
    {
        //a++; // You cannot modify const variables
        return a;
    }
};


int main()
{
    Project obj;
    cout<<"NonConstant(1): "<<obj.NonConstant(1)<<endl;
    cout<<"ConstData(1): "<<obj.ConstData(1)<<endl;
    cout<<"ConstFunction(1): "<<obj.ConstFunction(1)<<endl;
    cout<<"ConstBoth(1): "<<obj.ConstBoth(1)<<endl;
}

Let's say there is a struct with two members c and x:

struct S{
    // This is already quite unusual.
    // Nobody can change c after creation.
    // Because of constness.
       const int c = 42;   // Good useful number

    // x is part of the objects state.
    // x is important to protect for some reason or other.
    // Lets make it private:
    private:
       int x; 
    public:
    // Now how can others access x. 
    // In a non const way
    int& get_x_non();
    // Or in a const way 1 (A copy of out const):
    int get_x() const;
    // Or in a const way 2 (Our actual x as const, don't use with 1):
    const int& get_x() const;
    // The const after the function is part of the functions cv-qualifiers.
    // And makes a promise to the caller user that the object
    // doesn't change if get_x() is called.
    
    // So lets try to make a member function change c.
    void set_c(int new_c){
        // The compiler will not let this happen.
        c = new_c; 
    }
}

So:

void f(){
    S s;
    s.get_x_non() = 43; // Ok!
    s.get_x() = 44 // Can't happen
    int i = s.get_x(); // Always works.
    int j = s.c;  // Always works
    // neither i or j is const.
}

As you may notice, I can't really correlate these typical const scenarios to the questions in your assignment, that's why @Anoop's comment got a lot of cheering on.

As a consequence, we are going to have a hard time helping you with this particular problem. You need to ask whatever teaching staff you have available.

You can read more here: What's the difference between a const member function and a non-const member function?

Related