syntax question: member after colon in implementation

Hi,
I have a quick question.
In this example class, why is the member "radius (r)" listed after a colon in the function implementation?

class Circle
{
  private:
  float radius;
  public:
  //constructor
  Circle(float r);
};
//implementation
//constructor
Circle::Circle(float r): radius(r)
{}

*******
i'm a bit confused by this syntax. what does ": radius(r)" do after the constructor function?
Thanks in advance.
daniel416
LVL 3
daniel416Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
That section of the code is called the initialize list, and is used to initialize members or parrent before entering the body of the constructor
Jaime OlivaresSoftware ArchitectCommented:
this form of declaration will initialize 'radius' member with 'r' value, when constructor is called, previous to all contents (if any) of constructor code.
AxterCommented:
It's usually better to initialize your members via the initialize list, because then you're only setting the value once.

In other words:
Circle::Circle(float r): radius(r)
{}
The above code will set radius to the value of r

Circle::Circle(float r)
{
  radius = r;
}

The above code will first set radius to the value of zero, and then set it to the value of r.

This is what happens in most debug version of the code, however, most release versions will not set the value of radius to any default value.

But if you're using custom classes, they will usually have some type of default value.
If you don't use the initialize list, the custom class will set itself to the default value first, and then set itself to the value you reassign in your constructor.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

arun_vipCommented:
In your example you can write the constuctor in both the ways. Both the output will be same.
Circle::Circle(float r)
{
  radius = r;
}

or
Circle::Circle(float r): radius(r)
{}

If we use any reference member or constant member then you should use only through constructor list.
ex.
class A
{
  private :
const int i;
 public:
A():i(100)
{
}
}

in the above example if you use
A(){
i= 100;
}
then the compiler will give an error.
itsmeandnobodyelseCommented:
The main reason of using an initialize list ist to be able to use base class constructor and constructors of class members other than default constructor.

If we have

class Base
{
protected:
       string id;
public:
       Base(const string& name) : name(id) {}
};

class Derived : public Base
{
       Base secondBase;
public:
       Derived(const string& first, const string& second);
};

the following implementation of Derived constructor fails:

Derived::Derived(const string& first, const string& second)
{
      Base::id = first;                  
      secondBase.id = second;  
}

The problem is, that Base has no default constructor. That's why a Derived object that actually 'is a' Base object as well, needs to initialize its base class part prior to entering the body of the constructor function cause Derived must be constructed fully at that time. The only way out (beside of providing a default constructor) is to use the initialize list:

Derived::Derived(const string& first, const string& second)
     :  Base::id(first)
     ,  secondBase(second)  
{
}

If doing that you also can make the Base::id member private cause you don't need to have acces from the derived class.

Regards, Alex


daniel416Author Commented:
thanks everyone for your answers! very informative
sadly i don't have enough points to give to everyone. i hope you'll understand.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.