if i was to SET a object variable where would i do it, and could i use a function tooh?

so i want to change posx and posy using changepos (i will use setpos next time)

but when im declaring the object where i put the parameters for posx and posy and if i was to create a function...how would i get it to return th numbers in the same place

(there will be lots of randomly created objects wandering round a grid with behaviour...i figured i need to suss out how to use functions to manipulate object values and functions for behaviour...is that member functions?)

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
#include <iostream>
#include <string>

using namespace std;

class bunny
{
    public:
        int changepos ()
        {
            posx =0;
            posy =0;
        }
        int getpos ()
        {
            return posx,posy;
        }
    private:
    int posx;
    int posy;
};

int main()
{
bunny roger(//here?);
roger.changepos(// or here!?);  //OR ANYWHERE ELSE?
cout << roger.getpos()<<endl;
}


thanks guys you the best :)
Last edited on
If you want to set values when you are creating an object of the class then you should define a corresponding constructor that accepts two arguments.
Also you may not return to objects from a function as you wrote here

1
2
3
4
        int getpos ()
        {
            return posx,posy;
        }


The return statements of the function will return only posy because this expression posx,posy uses the comma operator.
Maybe it is better to return the values separately with each own function.
an how do i create a constructor that accepts two arguments...and how do i set them in main?

how can i get a function that would set them in main??
1
2
3
4
5
class bunny
{
    public:
        bunny( int x = 0, int y = 0 ) : posx( x ), posy( y ) {}
        ....


1
2
3
4
int main()
{
bunny roger( 10, 20);
...
oh so i sort of got it...and in main could i put a function in like

1
2
3
4
int main ()
{
bunny rodger (getrandomnum(),getrandomnum());
...


could i do that shiz?
Last edited on
what do i do if i want to change POSX and POSY values for the object later on...comps telling me theyre private...i could get rid of private and it would work fine but i heard i got to be careful
Last edited on
How can i declare a 2d array in class? i tried a few things

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

class bunny
{
    public:
        bunny (char x = ' ',char z='*') {}
       
        bunny  ( brdray[10] [10]) {={{z,z,z,z,z,z,z,z,z,z},{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,z,z,z,z,z,z,z,z,z} }}
        }

        bunny  (int x=5, int y=5): posx (x), posy (y) {} //declaring posx and posy

        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    char brdray;
    int posx;
    int posy;
    char bunicon;
    string name;
    char z;
    char x ;
    };





int getrand ();
void getboard ();
string namelist ();

//////////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
vector <bunny>bunnies;


}

//////////////////////////////////////////////////////////////////////////////
string namelist ()
{
string namearray [18] = {"bon","fur","fluff","foof"
,"bax","tig","uru","ira","esa",
"afe","ige","ifi","floof","tix","dar","vini","ter", "von"};

string none = namearray [getrand()%18];
string ntwo =namearray [getrand()*4%18];
string nthree=namearray [getrand()*7%18];
string fullname=none+ntwo+nthree;
return fullname;
}

void getboard ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }
}

int getrand ()
{
return 1+(rand()%6);
}

int getposx (int x)
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)

 {
  brdray [a] [b];
  if (brdray[a] [b]);

}
}
}


i get an error that says warning this will only work in extended gnus or sumk im not tooh sure where im going with all this either im just practising i guess
Last edited on
Topic archived. No new replies allowed.