Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:06:30+00:00 2026-06-15T04:06:30+00:00

I am currently working on Chapter 7 in the book Starting Out With C++

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:06:32+00:00Added an answer on June 15, 2026 at 4:06 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working through a jQuery book and am currently on the chapter of table
I`m currently working out the design for simple graphic editor, who support trivial operations
I am working on SportsStore example on chapter 4 from the following book and
I'm currently working my way through this book: http://www1.idc.ac.il/tecs/ I'm currently on a section
I am going through Zed Shaw's Python Book. I am currently working on the
I'm working through the railstutorial.org book, chapter 9. When running the test suite, I
Working in F# has lead me to learn about Haskell. I'm currently on chapter
I am working on Michael Hartl Rails tutorial (currently chapter 7). When I try
I have followed book until chapter 5 finished and it's working OK in my
Currently working with converting SQLException error messages into messages that are more useful for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.