change the objects inside functions - c++

I have a function which has two parameters, both objects. I change these objects inside the function and I need to see the changes afterwards. But pointers does not work. Any idea?
void foo(apple &a,apple &b)
{
//change a and b
}
main()
{
apple a,b;
foo(a,b);
//a and b are the same as befor calling foo `
}
thanks.

Do you mean changing the methods of the classes you're passing? You'll need to use '->' if that's what you mean.
class apple {
public:
int weight;
}
void foo(apple *a,apple *b) {
a->weight = b->weight;
}
main() {
apple a,b;
foo(&a,&b);
}

Related

I am getting a garbage value for variable a. I have to input 2 values and print their sum using 2 classes- base and derived

This is a program for single inheritance in C++. I have to derive a class using the protected keyword.
In the first class A, I have declared 2 functions to input and store the value of an integer variable a.
After I have created a derived class, I want to print the values I have inputted for variables a and b and then display their sum on the screen. I got the value of b correctly, but I am getting a garbage value for a. Can someone please help me out and tell me where I went wrong?
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
protected:
int a;
public:
void input_val_a(int);
int ret_a();
};
class B:protected A
{
int b,c;
public:
void input_val_b(int);
void get_a();
void sum();
void display_value();
};
void A::input_val_a(int y)
{
a=y;
}
int A::ret_a()
{
return a;
}
void B::input_val_b(int x)
{
b=x;
}
void B::sum()
{
c=ret_a()+b;
}
void B::get_a()
{
int a=ret_a();
}
void B::display_value()
{
cout<<"Value of a="<<ret_a()<<endl;
cout<<"Value of b="<<b<<endl;
cout<<"Value of c="<<c<<endl;
}
int main()
{
int a,b;
B obj;
A obj1;
cout<<"Enter the value of a"<<endl;
cin>>a;
cout<<"Enter the value of b"<<endl;
cin>>b;
obj.input_val_b(b);
obj1.input_val_a(a);
obj.get_a();
obj.sum();
obj.display_value();
getch();
return 0;
}
When you create two instances of objects, unless they share the same memory location, they both are two separate objects. Which means that once you do,
B obj;
A obj1;
These two are completely separate object. That means that if you assign the value of a to obj1, the variable a in obj will not change. And since your not initializing your variables, you will most certainly end up with junk.
How can I fix this?
Its simple, either you have to set the values of a and b to both obj and obj1, or else remove one of them, presumably obj1 (because you only call it once after creating).
Another valid option is to mark a as a static variable. This means that B::a and A::a points to the same variable. Updating it from one object, updates the value of a for all the other objects.

Getting same class instance as attribute from another class

I would like to obtain an class instance from another class.
I have the following classes, with class A having B object as a private member.
class A
{
private:
B my_B;
public:
B getBInstance (void)
{
return this->my_B;
}
}
class B
{
private:
int my_attr;
public:
B ()
{
this -> my_attr = 0; //Initial value for my_attr
}
void setMyAttr (int attr)
{
this->my_attr = attr;
}
int getMyAttr (void)
{
return this->my_attr;
}
}
I want my_B to be exclusive to an object that is instantiated from class A.
When I perform the following, I am not able to change the contents of my_attr, because it accesses a different B instance everytime I call A::getBInstance().
A new_A;
new_A.getBInstance().setMyAttr(50);
printf ("%d\n", new_A.getBInstance().getMyAttr()); //Prints 0
But, if I do the following, then I get the correct output:
A new_A;
new_B = new_A.getBInstance();
new_B.setMyAttr (50);
printf ("%d\n", newB.getMyAttr()); //Prints 50
I apologize if the answer is so obvious. I suspect efforts regarding making things static or singleton pattern might help, yet I need some guidance, as I'm pretty new to this kind of implementations.
Thanks in advance,
You're returning a copy of my_B. If you want to change the one stored in A, return a reference:
B &getBInstance (void)
{
return this->my_B;
}

C++ calling a vector of classes' method in another class

class C {
public: void c_set(int x){ a = x; }
private: int a;
}
;
class U {
public: void load();
c_loader(int i, int x){ c[i].c_set(x); };
private: vector<C> c(20);
}
;
void U::load() {
int x;
cin >> x >> i;
c_loader(i, x)
}
I'm really confused with this one. I need to call a member function in another one but my problem is that the inside class is a vector of that classes. My code is supposed to work but the result is segfault. Presume that the function cget has definition.
The question is a bit unclear but try this to prevent segfault.
class C {
public: void cget(int a);
private: int a;
};
class U {
public: void load();
vector<C> c; // Note: c is made public in order to add elements from main
};
void U::load(unsigned x, int a) {
if (x < c.size()) // Check the size of c _before_ access
{
c[x].cget(a);
}
}
void main()
{
U u;
C c;
u.c.push_back(c);
u.load(0, 3); // Will end up calling cget
u.load(1, 3); // Will just return without calling cget
}
EDIT:
Just want to mention that the code in the question has changed a lot sinse my answer. That explains why my code looks quite different ;-)
In any case, the answer is still: Check the size of c before accessing it.

C++ Pointer to Object as Class member

I'm trying to get two different classes to interact with eachother, for that I have in one class a pointer to an object of an other class, which is specified in the constructor.
Interaction works so far, I can change the paramters of the pointed-to object and I can see the changes, as I'm printing it on a terminal. BUT when I try to get a parameter from this object and try to print it to the terminal through the class which points to it I only get a zero value for an Int from which I know, cause of debug outputs, that it isn't zero, if called directly.
I will give you an example of the code:
Class A:
class Spieler
{
private:
int score;
Schlaeger *schlaeger;
int adc_wert;
int channel;
public:
Spieler(int x, Schlaeger &schl, int adc_wert_c=0, int channel_c=0 )
{
score=x;
schlaeger=&schl;
adc_wert=adc_wert_c;
channel=channel_c;
}
//....
void set_schl(Schlaeger &schl){ schlaeger=&schl;}
int getPosY(){ schlaeger->getSchlaeger_pos_y();}
int getPosX(){ schlaeger->getSchlaeger_pos_x();}
void setPosY(int y){ schlaeger->set_pos_y(y);}
void schlaeger_zeichen(){
schlaeger->schlaeger_zeichen();
}
void schlaeger_bewegen(){
schlaeger->schlaeger_bewegen(getADC());
}
//...
};
Class B:
class Schlaeger
{
private:
int schlaeger_pos_x;
int schlaeger_hoehe;
int schlaeger_pos_y;
public:
Schlaeger(int x=0, int h=5, int pos_y=15)
{
schlaeger_pos_x=x;
schlaeger_hoehe=h;
schlaeger_pos_y=pos_y;
}
int getSchlaeger_pos_x()
{
return schlaeger_pos_x;
}
int getSchlaeger_pos_y()
{
return schlaeger_pos_y;
}
int getSchlaeger_hoehe()
{
return schlaeger_hoehe;
}
void set_pos_y(int new_y)
{
schlaeger_pos_y=new_y;
}
};
The calls to the changing methods work, I can see the changes and I can see it in a debug output.
You're not returning the value in the getter
int getPosY(){ schlaeger->getSchlaeger_pos_y();}
should be
int getPosY(){ return schlaeger->getSchlaeger_pos_y();}

Accessing/filling a vector from a different class?

I am confused on how to fill a vector with values from a different class.
Can anyone give me a coded example how this is done. :)
Class A
{
//vector is here
}
Class B
{
//add values to the vector here
}
main()
{
//access the vector here, and print out the values
}
I appreciate the help <3
It seems like a quick lesson in access levels and encapsulation is in order.
My guess, based on the questions touch is that you're looking for the following code.
int main()
{
ClassA[] as = {new ClassA(), new ClassA(), ... }
ClassB[] bs = {new ClassB(), new ClassB(), ... }
}
But I'm shooting in the dark, a bit. :)
You should make your question more specific, edit it and post what you've tried to do. If you mean to do something respecting oop-rules, the play looks like this:
#include<iostream>
#include<vector>
class A{
public:
void fill_up_the_vector() { v=std::vector<int>(3); v[0]=0; v[1]=1; v[2]=4; }
void add( a.add(i); ) { v.push_back(i); }
void display_last() const { std::cout<<v[v.size()-1]; }
private:
std::vector<int> v;
};
class B{
public:
B(){ a.fill_up_the_vector(); } // B just *instructs* A to fill up its vector.
void add_value(int i) { a.add(i); }
void display() const { a.display_last(); }
private:
A a;
};
int main()
{
B b;
b.add_value(9);
b.display(); // reads v through A.
}
Note that this example above is a bit different from what you've asked. I posted it since I think you sould keep in mind that according to OOP rules
you don't want to access values in A directly,
B should have a member with type A if you plan to access a value in A,
you're supposed to access a value in A through B if you have filled it up from B.
The other way to go is not OOP:
struct A{
std::vector<int> v;
};
struct B{
static void fill_A(A& a) const { a.v = std::vector<int>(3); a.v[0]=0; a.v[1]=1; a.v[2]=4; }
};
int main()
{
A a;
B::fill_A(a);
a.v.push_back(9);
std::cout << a.v[a.v.size()-1];
}
but this code is as horrible as it gets.