Ok, first let's take a look at your class. Normally, the raw data members of the class should be declared private or protected. So, in your case the two int members x and w should be declared private:
1 2 3 4 5 6 7 8 9 10 11
|
// px.h
#ifndef PX_H
#define PX_H
class px
{
public:
private:
int x, w;
};
#endif
|
Now, building from there, you will declare your public members. These will include your constructor and any member functions that you want to expose to users of your class. Adding that in would make it now look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// px.h
#ifndef PX_H
#define PX_H
class px
{
public:
px(int);
px& move(int);
private:
int x, w;
};
#endif
|
Now, this presents a problem with your original requirements. You need access to the private member 'w'. So, the correct way to handle this is to create 'accessor' methods. These are simply set and get methods. Now, your class will look like this (I have also added a virtual destructor just in case you might want to inherit from this class later):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// px.h
#ifndef PX_H
#define PX_H
class px
{
public:
px(int);
virtual ~px();
px& move(int);
int getW() const;
px& setW(int);
private:
int x, w;
};
#endif
|
Instead of having to create an instance of 'px' and then calling the 'set' method for variable 'w' to get a value into it, you should add that as one of the parameters of your constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// px.h
#ifndef PX_H
#define PX_H
class px
{
public:
px(int, int);
virtual ~px();
px& move(int);
int getW() const;
px& setW(int);
private:
int x, w;
};
#endif
|
^^This will be what the completed class definition looks like and this is in a file named 'px.h'.
Ok, you might be asking what some of that stuff is in the code I have provided. The whole point of creating classes and such is for usefulness in a program. One of the conventions that should be followed for EVERY class is splitting the definition from the implementation. You define the class in a header file and implement the class in a cpp source file. To this point, we have only dealt with the header file.
The way you will 'link' everything together is like this:
px.cpp:
1 2 3 4
|
#include "px.h"
// implementation code here
|
main.cpp:
1 2 3 4 5 6 7 8 9
|
// any includes that you need
#include "px.h"
int main(int argc, const char* argv[])
{
// program code
return 0;
}
|
I'll help get you started with the implementation file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// px.cpp
#include "px.h"
px::px(int x_param, int w_param)
{
}
px::~px()
{
// don't need to do anything here, just for inheritance purposes
}
px& px::move(int move_val)
{
}
int px::getW() const
{
}
px& px::setW(int w_val)
{
}
|
Go ahead and take a shot at completing the implementation file given the class I have helped you get started on. If you have specific questions about how to do something or what something here means, then ask. Please be specific.