3

The assignment is to create a constant function getArea() that returns the area of a rectangle.

double getArea() {
    return width * height;
}

Is this it?

Do I put const after the parameters?

I don't really understand what I'm being asked.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
aeipownu
  • 107
  • 3
  • 17
  • Check this: http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c – Rahul Tripathi May 07 '15 at 05:19
  • Please go through this discussion . It will answer your question http://stackoverflow.com/questions/3141087/what-is-meant-with-const-at-end-of-function-declaration – neeta stanley May 07 '15 at 05:21

4 Answers4

6

In C++, a function becomes const when const keyword is used in function’s declaration. The idea of const functions is not allow them to modify the object on which they are called.

double getArea() const    {
    return width * height;
}

const after the (empty) argument list in the function declarations. indicates that getArea() function do not modify the state of a object i.e. data member of a object.

Please refer this link for more explanation: http://www.codeproject.com/Articles/389602/Constant-Member-Functions-Are-Not-Always-Constant

Reena Upadhyay
  • 1,977
  • 20
  • 35
3
double getArea() const 
   {
     return width * height;
   }

const keyword is used when the user doesnt want to modify or change the values of that variable or function.

Bullionist
  • 2,070
  • 3
  • 25
  • 42
2

Just like the built-in data types (int, double, char, etc…), class objects can be made const by using the const keyword. All const variables must be initialized at time of creation.

Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the constness of the object. This includes both changing member variables directly (if they are public), or calling member functions that sets the value of member variables.

const class objects can only call const member functions. A const member function is a member function that guarantees it will not change any class variables or call any non-const member functions.

SOURCE : leancpp.com

ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • This answer doesn't explain what to do to make the function const, but it does provide some background that addresses the question's *"I don't really understand what I'm being asked."* - nice! – Tony Delroy May 07 '15 at 06:56
2

Consider:

class ClassType
{
int x;
public:
ClassType(int _x) : x(_x) {}

int GetX() { return x;}
};

const ClassType object(20);
object.GetX();

This will fail to compile, since the object is const (readonly). But GetX is not marked const (readonly). length(), size(), IsOpen() are example of methods that may be called from a read only object. Hence the class should facilitate such (const) functions for const object.

You generally don't create const object, but you do pass them like:

void foo(const ClassType& obj);

Therefore, the GetX function should be:

int GetX() const { return x;}

const make a promise or a contract that this function doesn't modify this object.

Ajay
  • 18,086
  • 12
  • 59
  • 105