Reputation: 55
I have two classes, for example class A and B. B is encapsulation in A under private;
class A
{
private:
int x;
int y;
B b;
public:
void set(int , int, int, int, int);
void setX(int);
void setY(int);
string toString();
};
void A::set(int high, int low, int middle)
{
B(high, low, middle);
setX(x);
setY(y);
}
void A:: setX(int x)
{
this -> x = x;
}
void A:: setY(int y)
{
this -> y = y;
}
string A::toString()
{
string str;
ostringstream convert;
convert << getlow();
str = convert.str();
return str;
}
class B
{
private:
int low;
int middle;
int hight;
public:
B();
B(int, int, int);
int getLow();
int getMiddle();
int getHigh();
};
in another class with my int main
int main ()
{
int test1,test2,test3,test4,test5;
// with lots of codes
A a;
a.set (test1,test2,test3,test4,test5);
}
When i get some values from int main, i pass in the 3 values into set, which initialize object B. when i use the getB function, all i get is values of 0, or it doesn't appear at all. (I have a constructor that takes in arguments and set all int to 0). Can someone enlighten me? And please do not tell me not to use this. I'm new to C++ so guide me along.
Upvotes: 0
Views: 59
Reputation: 3812
in your set you don't initialize b member variable:
A::set(int high, int low, int middle)
{
B(high, low, middle);
}
Upvotes: 1
Reputation: 310910
Change the definition of class A at least the following way
class A
{
private:
B b;
int x;
int y;
public:
void set(int, int, int);
int getB();
};
void A::set(int high, int low, int middle)
{
b = B(high, low, middle);
}
int A::getB()
{
return b.getLow();
}
Upvotes: 1