c ++程序错误的结果

Sys*_*tem 2 c++

这是我为项目创建的代码.它的基本内容,但我一定忽略了一些东西,因为当我运行它时,无论我放在什么数字上,它给了我相同的答案:

radious given:288  
x=260444.
Run Code Online (Sandbox Code Playgroud)

这是为什么?

#include <iostream>
#include <stdlib.h>
#include <math.h>
#define pi 3.14

using std::cout;
using std::cin;
using std::endl;


class Circle 
{
private:          
    int Radious;
public:
    bool setRadious(int R);
    int getRadious(){return Radious;}
    double getx();
};


bool Circle::setRadious(int R)
{   
    bool RadiousSet = false;

    if (R > 0) //check validity of R
    {
        int Radious = R;
        RadiousSet = true;
    }     
    return RadiousSet;
}

//x = pi *R^2
double Circle::getx()
{
    return  pi*pow(Radious,2);
}

// -----------------------------

int main()
{
    int R=0;
    bool rslt;

    Circle myCircle;  


    cout<<"Give Radious: ";
    cin>>R;
    rslt = myCircle.setRadious(R);

    if(rslt == true) 
    {
        cout << "Radious given: " <<myCircle.getRadious();
        cout<<"x: "<<myCircle.getx()<<endl;
    }
    else
        cout<<"Radious must be greater than zero"<<endl;

    system("pause");
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

Mys*_*ial 7

改变这个:

if (R > 0) //check validity of R
{
    int Radious = R;
    RadiousSet = true;
} 
Run Code Online (Sandbox Code Playgroud)

对此:

if (R > 0) //check validity of R
{
    Radious = R;
    RadiousSet = true;
} 
Run Code Online (Sandbox Code Playgroud)

您将重新声明Radious为局部变量,它会遮挡您想要的变量.函数返回后,它的值会丢失.