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();
}
The member variable
Circle::ris never set, the local variablerinmain()is set viacinbut never used.Something like this may be want you want:
The variable
int rdeclared inmain()is not related to the member variablerin classCircle, even though they have the same name: they are two different variables.EDIT:
Further refinement of class
Circle:Changes:
areadoes not need to be a member variable (as stated by Fred Larson)ris not required to be passed tocircle_area()member function asris a member variable, socircle_area()has access to it