0

I am currently working on Chapter 7 in the book "Starting Out With C++ Early Objects" by the Pearson printing company.

I am unable to understand the function of the variable 'r' in this class declaration:

class Circle
{  private:
       double radius;

   public:
       void setRadius(double r)
       {  radius = r; }

  double getArea()
      {  return 3.14 * pow(radius, 2); }
};

Why can't I just write the 'radius' variable like this?

class Circle
{  private:
       double radius;

  double getArea()
      {  return 3.14 * pow(radius, 2); }
};

I don't understand the function of the

   public:
       void setRadius(double r)
       {  radius = r; }

Statement.

Robert
  • 123
  • 4
  • 1
    How can you set the value of radius if you don't have setRadius? – Sidharth Mudgal Nov 22 '12 at 07:23
  • Because it's private. Try to access it and change it, instead of using `setRadius`, and you'll see, that the code will not compile. – Kiril Kirov Nov 22 '12 at 07:23
  • You first need to understand function arguments.`setRadius` is a function that takes one argument named `r` of type `double`. Do you have trouble understanding what a function argument is? – Nikos C. Nov 22 '12 at 07:29

4 Answers4

1

The technical reason is "because radius is private, hence inaccessible from outside the class". So a change to radius must be some how managed by a public member function like SetRadius.

If the question now becomes "why designers did it that way, and did not simple make radius public?", well ... this is a never ending debate about how a proper object-oriented design should be an what has to be public and what not inside an object.

Traditional OOP school tends to make all data "private" and access or modify them through a variety of function to enforce what they call "encapsulation", and to have life easier in case the interface need to be extended to support eventual callbacks or events.

In this trivial simple case, well... all looks like a waste of energy (and without proper compiler optimization IS a waste of energy! In true physical sense). But may be they needed a uniform interface with something else.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
1

As the functional behaviour of private is explained in other answers, directly accesing a private member outside the class will give you a compile-time error.

If you are asking why do we use setter functions and make some members private is a matter of design. For example; if you need the radius to be always positive number, you can write the set function as;

void setRadius(double r)
{  
    if(radius >= 0)
        radius = r;
    else
        radius = 0;
}

Thus, you will have control over the values of the member when they are tried to be modified outside the class.

bmkorkut
  • 616
  • 5
  • 17
0

The radius is private. Without that function, you would not be able to set the radius from outside of the class. In order for the class to be useful, you would most likely want to be able to create objects of the type Circle and set their radius. Thus, you need some type of function in order to set that radius.

The easiest and most reasonable way to solve this is to supply a public member function inside the class Circle itself.

This can most easily be done using a setter, such as what you have shown. This allows you to set, and later change, the radius.

void SetRadius(float r)
{
    radius = r;
} 

You could also supply an extra argument to the constructor to ensure that a Circle always has its radius initialized with a user-supplied value, or at least set a default value in the declaration of radius (in C++11).

Agentlien
  • 4,996
  • 1
  • 16
  • 27
  • OH! I see what that is for now! I understood the function of keeping the data private, but for some reason I wasn't understanding that the program could not access the memory inside the 'radius' variable without passing the data from the public variable 'r'. – Robert Nov 22 '12 at 07:51
  • I was having trouble with the `radius = r;` statement. When I look at that statement I see something like `someVariable = 4;` or `someVariable = anotherVariable;` and I didn't understand how you could pass an empty variable to the 'radius' function. Thank you for the explanation! – Robert Nov 22 '12 at 07:57
0

The concept of public private that no one can access the private variables just the class methods, and you can only access the public methods from your main function so ,the function setRadius is responsible to set the private variable of the radius

public:
   void setRadius(double r)
   {  radius = r; }  

because the radius is a private variable so you have to create a public function inside the class to set that variable so you can set the radius when you create Circle object by :

this will work to set the radius in the main

Circle * test = new Circle;
test.setRadius(7);  

but if you tried to set the radius directly by :

Circle * test = new Circle;
test.radius = 7;

it will crash the program Cannot access class private method

Omar Freewan
  • 2,678
  • 4
  • 25
  • 49