3 Replies - 269 Views - Last Post: 09 January 2013 - 12:52 PM Rate Topic: -----

#1 Arcand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 13

Class and Enum Interactions

Posted 08 January 2013 - 12:07 PM

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:

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

This post has been edited by macosxnerd101: 08 January 2013 - 12:45 PM
Reason for edit:: Please use a more descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Class and Enum Interactions

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,572
  • Joined: 19-February 09

Re: Class and Enum Interactions

Posted 08 January 2013 - 01:55 PM

The TOOL class inherits from OBJECT_ID. This means that TOOL inherits the data and functions from OBJECT_ID.

class TOOL : public OBJECT_ID



Inheritance between classes



The constructor is correct for inheritance, notice OBJECT_ID is a class name.

32	    TOOL() : OBJECT_ID(OT_TOOL)




If TOOL was composed with an OBJECT_ID member variable then the initializer list would have an name of an object (variable).

32	    TOOL() : m_object_id(OT_TOOL)




Using inheritance, you could create a function in OBJECT_ID to display the id type.

  void display_type()
  {
    switch(type_of_object)
    {
       case ...
    }
  }



You can then call display_type within TOOL, or from an object of TOOL.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2954
  • View blog
  • Posts: 10,176
  • Joined: 08-August 08

Re: Class and Enum Interactions

Posted 08 January 2013 - 02:10 PM

First, all caps is usually used for constants, not class names.

You don't even need to use inheritance, although that can be useful. One class can use another as a type. Here's a simple example.
#include <iostream>
#include <vector>
#include <string>

class customer {
	double discount, annual;
	bool free_ticket;
	
public:
	customer(int disc) {
		double adiscount[2] = {0.1, 0.2};
		if (disc < 2) {
			discount = adiscount[disc];
		}
	}
	
	double get_disc() {
		return discount;
	}
};

class example {

public:
	std::vector <customer> cust;
	
	example() {
		cust.push_back(customer(0));
		cust.push_back(customer(1));
	}
};

int main(int argc, const char * argv[])
{
	example test;
	
	std::cout << test.cust[0].get_disc() << " " << test.cust[1].get_disc() << std::endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Arcand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-January 13

Re: Class and Enum Interactions

Posted 09 January 2013 - 12:52 PM

Thanks! i didn't know that classes acknowledge each other with out being told to do so! ahhh the joys of being a newb. I forgot about how fun it was in my nerdie leet-ness. =+)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1