Sign in to follow this  

C++ class variables

Recommended Posts

I'm new to C++ and a bit confused on how to use public variables.

 

I create a class:

class board{
    public:
    int n = 0;
};

Then I manipulate it in main:

int main() {
    board xy;
    xy.n = 10;
    cout << anotherFunction();
    return 0;
}

It would seem that only in main does n = 10; What I want to do is update it in the class so another function can use it. Is that possible? And if so how can I accomplish that?

int anotherFunction(){
    board xy;
    return xy.n; //need it to equal 10 now not 0 sense main updated it 
}

Hope this makes sense. Thanks.

Edited by Adehm

Share this post


Link to post
Share on other sites

I can do it with global variables.

namespace ObjectGlobalVars {
   //Put all of your global variables here
     int n = 0;
}
using namespace std;
int anotherFunction(){
return ObjectGlobalVars::n;
}
int main() {
ObjectGlobalVars::n = 10;
cout << anotherFunction();
return 0;
}
Edited by Adehm

Share this post


Link to post
Share on other sites

Problem with pointers or references as I understand it; is passing a multi dimensional variable. From what I understand is I can't pass that all through one variable, though I could be wrong. I was working on this: http://www.hackerrank.com/challenges/battleship1p and just wanted to be able to call functions to return a bool based on other data. Then instead of returning two pieces of data, which I don't know how to do, I could just set global variables from that function. Here is what I did to solve this problem; though I got a bad score because I don't know a better way of searching the board besides sequential until I find a target.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
namespace board {
   //Put all of your global variables here
     int n = 0;
    int xy[10][10]; //integer data repersenting board 0 = miss or dead/1 = undiscovered/2 = hit 
    int hX = 0;
    int hY = 0;
}
using namespace std;
//-------------------------------------------------------------------------------------------------------
bool hit(int x, int y){
            //search for another hit/ y-1,y+1,x-1,x+1
    if(y - 1 > -1 && board::xy[x][y-1] == 2){ //found
            board::hX = x;
            board::hY = y-1;
            return true;
    }else if(y + 1 < board::n && board::xy[x][y+1] == 2){ //found
            board::hX = x;
            board::hY = y+1;
            return true;
    }else if(x - 1 > -1 && board::xy[x-1][y] == 2){ //found
            board::hX = x-1;
            board::hY = y;
            return true;
    }else if(x + 1 < board::n && board::xy[x+1][y] == 2){ //found
            board::hX = x+1;
            board::hY = y;
            return true;
    }
    return false;
}
//-------------------------------------------------------------------------------------------------------
bool attack(int x, int y){
            //search for another hit/ y-1,y+1,x-1,x+1
    if(y - 1 > -1 && board::xy[x][y-1] == 1){ //found
            board::hX = x;
            board::hY = y-1;
            return true;
    }else if(y + 1 < board::n && board::xy[x][y+1] == 1){ //found
            board::hX = x;
            board::hY = y+1;
            return true;
    }else if(x - 1 > -1 && board::xy[x-1][y] == 1){ //found
            board::hX = x-1;
            board::hY = y;
            return true;
    }else if(x + 1 < board::n && board::xy[x+1][y] == 1){ //found
            board::hX = x+1;
            board::hY = y;
            return true;
    }
    return false;
}
//-------------------------------------------------------------------------------------------------------
int main() {
    cin >> board::n;  
    string col;
    int hX = -1;
    int hY = -1;
    for(int y = 0;y<board::n;y++){
        cin >> col;
        int x  = 0;
        for(char& c : col) {
            switch(c){    
                case 'm' || 'd':
                    board::xy[x][y] = 0;
                    break;
                case '-':
                    board::xy[x][y] = 1;
                    break;
                case 'h':
                    board::xy[x][y] = 2;
                    //store hit
                    hX = x;
                    hY = y;
                    break;
            }
            x=x+1;
        }
    }
    //board now stored in xy
    //if hX a positive number then search around it
    if(hX > -1){
        //search for another hit
        if(hit(hX,hY)){ //another hit found
            int dirX = 0;
            int dirY = 0;
            //direction known from hX - xy.hX or similier
            //find direction
            //cerr << hX << " <> " << board::hX << endl;
            if(hX > board::hX){ //x-1
                dirX = -1;
            }else if(hX < board::hX){ //x+1
                dirX = 1;
            }else if(hY < board::hY){ //y-1
                dirY = -1;    
            }else if(hY > board::hY){ //y+1
                dirY = 1;   
            }
            //check direction until = 1
            for(int i = 1;i<5;i++){
                if(board::xy[hX+dirX*i][hY+dirY*i] == 1){ //found next attack
                    hX = hX+dirX*i;
                    hY = hY+dirY*i;
                    break;                
                } 
            }
            
        }else{ //try around it until hit
            if(attack(hX,hY)){
                hX = board::hX;
                hY = board::hY;
            }
        }      
    }else{ //find a 1 to attack
        for(int y = 0;y<board::n;y++){
            for(int x = 0;x<board::n;x++){
                if(board::xy[x][y] == 1){ //attack
                    cout << y << " " << x;
                    return 0;
                }
            }
        }
    }
    cout << hY << " " << hX;
    return 0;
}

Share this post


Link to post
Share on other sites

Scope- A block that holds a "context" of variables and code.

{

// First scope

{

 // Second scope

}

 

// Second cope is gone, returning to first scope.

}

Secondly, The two objects in main and in your functions refer to other ISNTANCES of the class board.

board xy; //is an instance of board.

You can refer to the same instance by using a reference or a pointer (which gives you the original location of your instance).

 

int func(board* xy)

{ xy->n=10;}

board xy;

func(&xy); // You pass the LOCATION of xy and not the instance. It copies the memory address instead of the value itself. 

if(xy.n==10)

{ cout << "I did it"<<endl; } 

Share this post


Link to post
Share on other sites

Cool beans, didn't know how to pass a struct by reference like that. I'll try replacing my code tomorrow to do it that way so I can ensure I understand how; only really my second day using c++ so didn't really know how to do anything. Thanks for the examples.

 

In case anyone else is wondering how to pass a struct by reference here is a working example.This makes it extremely easy to refer to a group of variables with a single pass.

struct board {
     int n = 0;
};
void af(board& b){
    cerr << b.n << endl; //print n proving it was updated in main
    b.n = 2; //update n
}
int main() {
    board b; //instance of struct board
    b.n = 1; //update n
    af(b); //pass instance of structure by reference to another function
    cerr << b.n; //print n proving it was updated from af()
    return 0;
}
Edited by Adehm

Share this post


Link to post
Share on other sites

If you're working with fixed size arrays in modern C++, I suggest using std::array over that bracket syntax. It makes arrays act like every other object in C++, This mostly has stylistic advantages, plus it allows you to pass and return arrays by value, if ever needed.

Share this post


Link to post
Share on other sites

Thanks for the info King Mir, very helpful; also lead me to some syntax showing me how to initialize the array in the declare which i'm very glad to know. 

void af(std::array<int, 3>& n){
    cout << n[1] << endl; //print second value
    n[1] = 3; //change second value 
}
int main(){
    std::array<int, 3> n{0,0,0}; //declare a std::array and initialize
    n[1] = 2; //update second value
    af(n); //pass it by reference
    cout << n[1]; //print updated value
    return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this