Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

whats wrong with my code

bw125bw125 Posts: 10Member
//In class we are learning about private functions in classes and i do
//not understand why i cannot get a value for d

#include
#include
using namespace std;
#include

const double PI = 3.14159;

class Circle{
public:
double C, A, r;
double getC(double C);
double getA(double r, double A);

private:
double d;
double getd(double r);
};

double Circle::getd(double r){
d = r * 2;
return d;
}

double Circle::getC(double C){
C = d * PI;
return C;
}

double Circle::getA(double r, double A){
A = (r * r) * PI;
return A;
}

int main(int argc, char *argv[]){
Circle x;

double C = 0, A = 0, r;

cout << "Enter the radius: ";
cin >> r;
cout << "The cirumfrence is: " << x.getC(C);
cout << "
";
cout << "The area is: " << x.getA(r, A);
cout << "
";

system("PAUSE");
return EXIT_SUCCESS;
}



Comments

  • LundinLundin Posts: 3,711Member
    The point of making variables private is to prevent anyone from accidently altering them, as well as preventing people to meddle with code that they don't know how it works, or code that they don't even have access too. So you aren't supposed to be able to access "d" from main, you have to use a function like "getd" to get a copy of it.

    This is known as "encapsulation" and is considered very good programming practice.
Sign In or Register to comment.