-8
Rectangle(Point2D center, double w, double h, double r, double g, double b):center(center)
    {
        ...
    }

What does the :center(center) part of this code do?

jotik
  • 17,044
  • 13
  • 58
  • 123
Programmer
  • 421
  • 1
  • 7
  • 21

2 Answers2

1

: initializes center member with center parameter.

In most case (not always!) your code

Rectangle(Point2D center, double w, double h, double r, double g, double b):center(center)
{
    ....
}

could be replaced with

Rectangle(Point2D center, double w, double h, double r, double g, double b)
{
    this->center = center;
    ....
}

You can read more about initialization lists in C++ here

Avt
  • 16,927
  • 4
  • 52
  • 72
0

It's used to initialize the element . Since C++ is object oriented everything is treated as object even datatypes. so when you use initialization list the object are initialized when the object is created for it.

This is considered to better(faster) way then assigning to the variable directly.

As definition and declaration happens in one go.

KARTHIK BHAT
  • 1,410
  • 13
  • 23