I'm trying to write a class to figure out the parameters for a rectangle or arch window. I need some help with the class. I'm having some difficulty calling variables defined in one method to another method in the same class.
You need to declare the data as a member of the function. For example:
Foo.h
1 2 3 4 5 6 7 8 9 10
class Foo
{
private:
int MyInt; //Declaring this data as part of the function allows you to access it in any of the classes functions.
float MyFloat;
public:
int GetInt(); //Both GetInt
void AddOneToInt(); //And AddOne would be able to access the same variable "MyInt".
float GetFloat();
};
So now in your .cpp file:
Foo.cpp
1 2 3 4 5 6 7 8 9 10 11
#include "Foo.h"
int Foo::GetInt()
{
return MyInt; //You can just call the variable by the name that it was given in the class definition.
}
void Foo::AddOneToInt()
{
++MyInt; //Here we are manipulating the exact same variable. If GetInt was called after AddOne was //called the returned value would be one bigger.
}
Just remember that if you put data in the public section of a classes members it can be accessed just like in a struct:
If MyInt was public this:
1 2 3 4 5 6
int main()
{
Foo Test;
Test.MyInt = 0; //Would be perfectly legal, and now a call to GetInt would return 0.
return 0;
}
EDIT:
So for your code you would want something like:
1 2 3 4 5 6 7 8 9 10
class Rect
{
private:
int width,height;
double pi;
//Any other private data or functions.
public:
//All your public functions, for example to access the private members.
};
Just keep in mind that if your class isn't too complicated it can just bloats the class to have special accessing functions for every variable. Just make them public.