Pointer

I've been asked to define Point, Player and Bullet.
I think i had defined player and bullet. But im stuck how do i (DEFINE) pointer.
where does it go?
how would i write it?

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

using namespace std;

class BulletClass{
public:
   BulletClass(string z){
    setColour(z);
   }
    void setColour(string x){
        Colour =x;
    }
    string getColour(){
    return Colour;
    }

private:
    string Colour;

};
class PlayerClass{
public:
    PlayerClass(int z){
    setHealth(z);
    }
    int setHealth(int x){
        Health = x;

    }
    int getHealth(){
        return Health;
    }

private:
    int Health;
};


int main()
{

    BulletClass bulletobject("Bullet black");
    std::cout <<bulletobject.getColour()<< endl;
    std::cout <<"Player ";
    PlayerClass playerobject(100);
    std::cout <<playerobject.getHealth()<<endl;
    return 0;
}
closed account (z05DSL3A)
cplusbeginner89 wrote:
I've been asked to define Point, Player and Bullet.
I think i had defined player and bullet. But im stuck how do i (DEFINE) pointer.
Point or pointer? you will need to be a little less inaccurate and give more details about what you are trying to do.
where does it go? how would i write it?

You have Player and Bullet, so you know how to write a class.

What attributes does Point have? How is it used? Do Player or Bullet use Point? You haven't told use any of that.

A typical Point class has x and y coordinates. Don't know if that fits your case or not. Beyond that, you're going to have to tell us more.


can i use a point to point to a class?
OR is it used to point to a function?
how do i write a point as code.
i currently have player health and bullet colour, would i use a point to point to both of those functions and call the points through couts?
Im sorry i am very crap at trying to explain stuff. Hopefully this helps a little.
can i use a point to point to a class?
1
2
3
4
5
class foo
{
};
//...
foo* ptr = new foo();

OR is it used to point to a function?
1
2
3
4
5
6
7
using foofuncptr = void (*)(int);

void foo(int i)
{
}
//...
foofuncptr* ptr = foo;
I've been asked to define Point, Player and Bullet.

Are you referring to a pointer or as class called "Point"?

You use the word "point". A point and a pointer are two different things.
Programming requires exactness. If you mean pointer, then you need to use that word. You can't use point and pointer interchangably.

If you truely mean pointer, there are tutorials on how to use pointers.
http://www.cplusplus.com/doc/tutorial/pointers/

An example of using pointers.
1
2
3
 
  Bullet  bullet;  // A bullet instance
  Bullet * pbullet = &bullet;  // a pointer to a bullet 

I am still new to c++ only been a 3 months or so of my course sorry.
I believe i mean pointer/
I have looked at that c++ tutoral and didnt really understand it.I work better with examples.
A pointer is simply a variable that contains the address of another item in memory.

Using the example I posted above, lets assume that Bullet is stored at address 100 and occupies 16 words of memory.
1
2
3
 
  100  Bullet  bullet; 
  116  Bullet * pbullet = &bullet; 

pbullet contains the address of bullet (100).

If I want to use the pointer to access bullet's methods, I have to use the -> operator.
 
  pbullet->setColour ("red");


closed account (j3Rz8vqX)
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
//"An" idea:
//Pointer variables point to addresses of real variables(intention: data).

//This is my integer variable, set to 0
int myInteger = 0;

//This is my Pointer variable, of type int, pointing to address Null; hopefully nothing's there ;D
int *myPointerToInt = 0;//This is how a pointer is declared!

//I am assigning my Pointer the address of myInteger (& = address/[de]reference)
myPointerToInt = &myInteger;

//I can manipulate my integer variable:
myInteger = 5;//By direct variable access.
*myPointerToInt = 7;//by pointer variable access
//(Hint: An (*) asterisk before pointers refers to their value, not address)

/*
Why should you use a pointer? Not sure, it is up to you.

Personally I use them to alias data and sometimes I'll include them to allow possible expansion of data.

They can be used as place holders for data that does not already exist.

You will definitely find folks using it in trees.
*/


Many more.... Times up.

Have a good day.

Tip: Read the tutorial, it'll explain uses with functions, objects and such; there is little reason to restate what's already stated; though I just sort of did that...
Topic archived. No new replies allowed.