Classes with Enum Data Members

#include <iostream.h>
/***************************************************
 File: enum_class.C
 This program demonstrates how enum data members
 are processed and assigned values
***************************************************/

class robot
{
public:
	enum direction{NORTH, WEST, SOUTH, EAST};
	robot() : x(0), y(0), orientation(NORTH) {}
	void turn(direction  );
	void display();
private:
	direction orientation;
	int x, y;
};

void robot::turn(direction dir)
{
	orientation = dir;
}

void robot::display()
{
	cout << "Position" << endl;
	cout << "X: " << x << endl;
	cout << "Y: " << y << endl;
	cout << "Orientation: " << orientation << endl;
};

int main()
{
	robot bob;

	bob.display();
	bob.turn(robot::WEST);
	bob.display();
	return(0);
}
		
Output:
Position
X: 0
Y: 0
Orientation: 0
Position
X: 0
Y: 0
Orientation: 1



Back to Previous Page

Document:
Local Date:
Last Modified On: