Working with classes

So, im relatively new to programming, and I have a tutor that has given me an assignment based on our last lesson but I have run in to a problem with something that I dont understand based on the following code:

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
enum OBJECT_TYPE // Creation of a data type
{
	// Individual data types
	OT_ROOM,
	OT_TOOL,
	OT_PUZZLE,
	OT_NOUN,
	OT_VERB,
	OT_MAX_OBJECT_TYPE
};

class OBJECT_ID // Class to call when using an object from OBJECT_TYPE
{
public: // Seen and used by any outside source
	OBJECT_ID() // Default constructor - Called if nothing else can be
	{
		type_of_object = OT_MAX_OBJECT_TYPE; // Makes type_of_object (as in private) the value of OT_MAX_OBJECT_TYPE, or the max and illigitimate value
	}
	OBJECT_ID(OBJECT_TYPE intype) // Primary constructor - called when requested with a defining value from the enum
	{
		type_of_object = intype; // Assigns type_of_object the value sent to the class when it was called
		
	}
	~OBJECT_ID(); // Destroys the object
private: // seen only by the class itself
	OBJECT_TYPE type_of_object; // Declares an enum value called type_of_object
};

class TOOL : public OBJECT_ID // A class that interacts with the OBJECT_ID class as its parent
{
public: // Seen and used by any outside source
	TOOL() : OBJECT_ID(OT_TOOL) // Default constructor called by OBJECT_ID with a value of OT_TOOL
	{

	}
	~TOOL();
private: // seen by only the class itself 


What im TRYING to do is have it be possible for OBJECT_ID to identify for what enum value its been called for and run the appropriate class as such, but, I just realised (the next day after the lesson... lol) that I never asked how one class can point to running code from another?

 
	TOOL() : OBJECT_ID(OT_TOOL) // Default constructor called by OBJECT_ID with a value of OT_TOOL 


as far as I know this line does what I commented it as, and if im not wrong then there needs to be another line of code to make this one happen?

thanks for the help!
AJ
Last edited on
Perhaps you could accept a pointer input in a member funtion that dynamically allocates a class (of the given type) for you and returns the pointer pointing to the new class of the type specified. Make sure you don't leave a hanging pointer though.
Last edited on
You want OBJECT_ID to identify the enum when it acts as a base class for TOOL? Or when you construct just the OBJECT_ID class?
Passing a value to the constructor of a parent class in the constructor list will construct that class with those parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class A
{
public:
	A(int i): I(i){};
private:
	int I;
};

class B: public A
{
public:
	B(int i): A(i){};
}

int main()
{
B b(10); //I in A will take on the value 10.
}
I'm not exactly sure of what you are asking, but I think you may be thinking of these class definitions like functions. OBJECT_ID is a class definition; like int, or long. It is an object type. TOOL is also an object type which contains OBJECT_ID. When you create an instance of TOOL (like creating an int variable), it passes the enum value OT_TOOL (equal to 1) to the base class OBJECT_ID and it is stored as the property type_of_object. So if you have code like this:
1
2
TOOL Mytool;
printf("Mytool type is %d", Mytool.type_of_object);


You create an instance of TOOL and it knows what type it is.
As it is, this is all that Mytool can do.
Topic archived. No new replies allowed.