Home > Software design >  Need understanding of question regarding Const Function in C
Need understanding of question regarding Const Function in C

Time:07-09

i have attached question screenshot below, its regarding my understanding, im not asking anyone to do it for me, just want to understand what question is really trying to say, as im getting so many errors in code, if try to apply changes as shown in the figure. The Code is attached.

#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();
}
        

enter image description here

CodePudding user response:

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;
}

CodePudding user response:

Lets 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 Anoops comment got a got of cheering on.

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

You can read more here.

  •  Tags:  
  • c
  • Related