class Car
{
public:
Car(string name, const int wheels = 4){}

private:
string name;
};

void main()
{
Car("Ford",3); // Why can I put in a 3?
}

Edited 6 Years Ago by pac-man: n/a

4 is a default value. If you had written line 12 like this: Car("Ford"); it would still be valid as the argument list would default the second argument to 4 rather than the 3 you actually used. const means that whatever is done within the constructor it cannot change the value of wheels it is given. If the constructor is given sometinig other than 4 then that will be only value of wheels it is allowed to used. If the constructor isn't passed an explicit value then in will default to a constant value of 4.

In addition to that, even if you initialize a constant variable in your constructor, the value to be used is the value passed in when an object of Car is instantiated. That's why 3 is accepted as the value for wheels.
However, if you try to compile this code:

#include<iostream>
using namespace std;
class Car{
 public:
 Car(string name,const int wheels):wheels(4){

 cout<<"Wheels:"<<wheels<<endl;
 }
 private:
 string name;
 const int wheels;
 };
 int main()
{
Car th("Ford",3); // Why can I put in a 3?
th.wheels=45;
return 0;
}

it would give you 2 errors; Car::wheels cannot be accessed outside the class, and that you cannot modify a const object. That is when const is useful!!
The value of wheels cannot be modified when tried to, because it has been declared as a constant variable in Car.

Edited 6 Years Ago by tkud: n/a

hi there and thanks for the reply..

that makes sense, so if I create a Car object with wheels value of 2...

then try to change the wheel number from 2 to 6, that should not be permitted?

To my knowledge, only if wheels is a member variable declared with keyword const. That is, it is the declaraion the member variable called wheels as const that prevents it from being changed somehow after construction. If keyword const is left off of the declaration of wheels in post #3 line 11, then wheels could be changed to some other value by some other function. However, the argument called wheels passed to the constructor as an argurment is not the same variable as the member variable called wheels, and I would strongly discourage use of the same name for separate variables. To my knowledge the keyword const associated with an argument means that the constructor can't change the value of the argument that has been passed in. That is

class Car:
{
  int wheels;
  public:
  Car(string Name, const int w = 3)
  {
     w += 1; //this would be an error
    wheels = w + 1;  //this should be okay since it doesn't involve a change in w
};

It is my understanding that wheels in the above case could be changed after construction even though w could not be changed in the constructor.

I vaguely remember that const member variable must be initialized in the initialization list like this:

const int wheels;
public:
Car(string s, const int w = 4) : wheels(w){};

In this case w can't be can't be changed by the constructor and wheels can't be changed by any function after construction. The value of wheels will be 4 by default and the value of w under any other condition.

What may not be possible is this:

const int wheels;
Car(string s, const int w = 4)
{
   wheels = w;
}

However, at the moment my head is spinning and I haven't tested all of the above scenarios to see if my memory is in error. Any corrections or clarifications by another poster is welcome.

Thanks a lot for the detailed response. It makes sense that you can still use the const variable so long as you're not changing its value

:-)

By the way, the syntax used in

Car(string s, const int w = 4) : wheels(w){};

reminds me of when applying inheritance!

This article has been dead for over six months. Start a new discussion instead.