3

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();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
mrc
  • 43
  • 6
  • 2
    `int const x` and `const int x` are exactly the same. – Some programmer dude Jul 08 '22 at 12:39
  • 11
    Whoever wrote that question could've done a lot better in writing that question. – Jason Jul 08 '22 at 12:39
  • 1
    Also, returning a `const` value has no real meaning. The caller could still assign the returned value to a non-const variable. And returning `const void` means even less, since nothing is even returned. – Some programmer dude Jul 08 '22 at 12:40
  • 1
    @AnoopRana absolutely man. – mrc Jul 08 '22 at 12:41
  • And a `const` qualified function (like `void ConstBoth() const`) can't modify member variables. Not that it matters in your specific case, as `const` member variables can't be modified anyway. – Some programmer dude Jul 08 '22 at 12:41
  • @Someprogrammerdude yes brother i know that, const member variables, cant be modified and const object variables can be be modified using mutable keyword. – mrc Jul 08 '22 at 12:44
  • just want people opinions and solutions – mrc Jul 08 '22 at 12:45
  • "want to understand what question is really trying to say": Looks like you'll have to ask the instructor or assistant, etc., because I have to guess at a lot of parts, and have no clue in particular what the "Function Type" column is about. And it sounds like the other commenters also don't know. – aschepler Jul 08 '22 at 12:49
  • My guess would be the "data to be modified" column refers to the parameter being (non-)`const` while the "function type" column refers to it being a (non-)`const` member function. Although it is strange that the first column explicitly shows the parameter as non-const every time – perivesta Jul 08 '22 at 12:57
  • @perivesta class Project { private: int const x; int y; public: Project():x(5){} int NonConstant(int x){ return x; } int ConstData(int const x){ return x; } const int ConstFunction(int x){ return x; } const ConstBoth(const int x){ return x; } }; int main() { Project obj; cout<<"NonConstant(1): "< – mrc Jul 08 '22 at 13:03
  • Also note that the constness of the arguments means the compiler can't use move semantic. This questions seems to be from a time before there was move semantic in C++ as it's awfully incomplete in modern c++. There are a lot more cases to consider today. – Goswin von Brederlow Jul 08 '22 at 13:11

2 Answers2

0

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;
}
Adrian
  • 468
  • 2
  • 6
  • 15
  • 2
    Let's hope its not what OP needs because `const int ConstFunction(int x)` is nonsensical amongst many other things. – Captain Giraffe Jul 08 '22 at 12:54
  • @CaptainGiraffe I agree that the return value should be just int in all cases, but it seems that he/she just wants to experiment. – Adrian Jul 08 '22 at 12:57
  • I'd imagine `ConstFunction` is supposed to be `int ConstFunction(int x) const {`, to denote a function which does not modify the current object nor its fields. – Rogue Jul 08 '22 at 12:59
0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67