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 7044663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:26:11+00:00 2026-05-28T02:26:11+00:00

It always outputs ‘0’ for the area. I can’t figure how to work in

  • 0

It always outputs ‘0’ for the area. I can’t figure how to work in the int r to calculate the area.

// Define a class and use it to test out some math stuff
#include <iostream>
#include <cmath>
using namespace std;

class Circle {
        public:
                // function that calculates the area of a circle
                float circle_area(int r) {
                        area = 3.14 * (r*r);
                        return area;
                } // end function circle_area
                void display_msg() {
                        cout << "Area: " << circle_area(r) << endl;
                } // end function display_msg
        private:
                float area;
                int r;
}; // end class Circle

int main(void) {
        int r;

        Circle circle_calc; // create a Circle object named circle_calc

        cout << "Radius of circle: ";
        cin >> r;
        circle_calc.display_msg();
}
  • 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-05-28T02:26:12+00:00Added an answer on May 28, 2026 at 2:26 am

    The member variable Circle::r is never set, the local variable r in main() is set via cin but never used.

    Something like this may be want you want:

    class Circle {
    public:
        Circle(int a_radius) : r(a_radius) {}
        // function that calculates the area of a circle
        float circle_area(int r) {
                area = 3.14 * (r*r);
                return area;
        } // end function circle_area
        void display_msg() {
                cout << "Area: " << circle_area(r) << endl;
        } // end function display_msg
    private:
        float area;
        int r;
    }; // end class Circle
    
    int main(void) {
        int r;
    
        cout << "Radius of circle: ";
        cin >> r;
    
        Circle circle_calc(r);
    
        circle_calc.display_msg();
    }
    

    The variable int r declared in main() is not related to the member variable r in class Circle, even though they have the same name: they are two different variables.

    EDIT:

    Further refinement of class Circle:

    class Circle {
        public:
            // Constructor.
            Circle(int a_radius) : r(a_radius) {}
    
            // function that calculates the area of a circle
            float circle_area() { return 3.14 * r * r; }
    
            void display_msg() {
                cout << "Area: " << circle_area() << endl;
            }
        private:
            int r;
    }; // end class Circle
    

    Changes:

    • area does not need to be a member variable (as stated by Fred Larson)
    • r is not required to be passed to circle_area() member function as r is a member variable, so circle_area() has access to it
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a small problem I can't seem to figure out. I've got the
I'd like to use a variable like $GET[name] that always outputs a MySQL-safe version
I can output a locale sensitive time format using strftime('%X') , but this always
Can anyone tell me why I always have this strange output after running this
I always thought that you could use OR in a LIKE statment to query
For some reason the following C# Console program always outputs: 32 False wtf=0 What
I'm working with a function that outputs a string that is always formatted Month
Whenever I use Html.ActionLink it always Html encodes my display string. For instance I
The following code outputs a random number each second: int main () { srand(time(NULL));
Is it possible to get PowerShell to always output / instead of \ ?

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.