@Darkmaster
A setter doesn't work for me if I use a different object other than the object
that set the private var so I will try using a friend function.
@maeriden
The base class is inherited as public and I am aware the sets/gets can access
private member of a function through a public function but my problem comes
when I want to update(set) a private member var form outside the class in
which the private member variable resides and when I call a setter from outside
the class, the base class private member variable does not update...here is a
quick mock up of what I am getting at:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include "iostream"
using std::cout;
using std::endl;
class Yeehaw
{
int numInQuestion;
public:
Yeehaw()
{
numInQuestion = 50;
}
int GetNumInQuestion()
{
return numInQuestion;
}
void SetNumInQuestion(int x)
{
numInQuestion = x;
}
};
class PlaceHolederClass : public Yeehaw
{
public:
PlaceHolederClass()
{
}
};
int main()
{
Yeehaw cYeehaw;
PlaceHolederClass cPlaceHolder;
int num = 9;
///Get Number
cout<<"This is the original number: "<<cYeehaw.GetNumInQuestion()<<endl<<endl;
cout<<"\""<<num<<"\" will be put into the setter via the child class object"<<endl<<endl;
///Sets the Private Member Variable of the Base Class Via Child Class
cPlaceHolder.SetNumInQuestion(num);
cout<<"Number from base class after update: "<<cYeehaw.GetNumInQuestion()<<endl;
cout<<"Number from child class after update: "<<cPlaceHolder.GetNumInQuestion()<<endl;
return 0;
}
|
This is the original number: 50
"9" will be put into the setter via the child class object
Number from base class after update: 50
Number from chile class after update: 9 |
The number from the base class never updates and if I understand
correctly it is because I set the number with a different object but that
it just it. In my other program, my base setter sets a variable and I want
a function outside the class to be able to set that same variable and have
it updated in the child AND the base class and the only way I figured to do
it is pass the base class' object through to the child class' function...sorry for
that long explanation but I am just trying to be real clear. Will work with
friend function for now and see if they work...