2 Replies - 1003 Views - Last Post: 09 December 2012 - 06:10 AM Rate Topic: -----

#1 jeckel7234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 12

How does this look? Class for abstract data class type

Posted 08 December 2012 - 08:11 PM

I'm working on a project, and this is part of what I'm doing for it, can someone take a look over this before I submit



Problem
Create a C++ class for an abstract data type color with a public enumeration type colorType that has the color values (black, blue, green, cyan, red, magenta, brown, lightgray, nocolor) The abstract data type should have an attribute for storing a single value of type colortype and member functions for reading (readColor) and writing (writeColor) a color value as well as setting and accessing it. The function readColor should read a color as a string and store the corresponding color value in the value attribute of a type color object. The function writeColor should display as a string the value stored in the value attribute of a type color object.

class color
{
enum colorType { black, blue, green, cyan, red, magenta, brown, lightgray, nocolor };

colorType c;
int n;
void readColor()
{
cout<<"Enter the color code";
cout<<"\n 0 for"<<" black"<<"\n 1 for"<<" blue"<<"\n 2 for"<<" green"<<"\n 3 for"<<" cyan"<<"\n 4 for"<<" red"<<"\n 5 for"<<" magenta"<<"\n 6 for"<<" brown"<<"\n 7 for"<<" lightgray"<<"\n 8 for"<<" nocolor";
cin>>n;
}

void writeColor(int a)
{
switch(a)
{
case black : cout<<"the color is black";
break;
case blue : cout<<"the color is blue";
break;
case green : cout<<"the color is green";
break;
case cyan : cout<<"the color is cyan";
break;
case red : cout<<"the color is red";
break;
case magenta : cout<<"the color is magenta";
break;
case brown : cout<<"the color is brown";
break;
case lightgray : cout<<"the color is lightgray";
break;
case nocolor : cout<<"the color is nocolor";
break;
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How does this look? Class for abstract data class type

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,507
  • Joined: 19-February 09

Re: How does this look? Class for abstract data class type

Posted 08 December 2012 - 09:06 PM

I think that readColor should be passed a string, and writeColor should not be passed a value.

void readColor(string &str);



The class should end with a semicolon.

class color
{
 // ...
}; // <-- semicolon added




It would be useful to write a main function and test the class.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: How does this look? Class for abstract data class type

Posted 09 December 2012 - 06:10 AM

I like to take your requirements and lay them along side the code:
// Create a C++ class for an abstract data type color with a public
class color {
public:  // a public
	// enumeration type colorType
	enum colorType { black, blue, green, cyan, red, magenta, brown, lightgray, nocolor };

	// should have an attribute for storing a single value of type colortype
	// should this be public?  Probably not, but given the rest of the description, probably
	// c is a craptastic name
	// colorType c;
	colorType value;
	
	// int n;  no

	// member functions for reading (readColor) and writing (writeColor) 
	
	// readColor should read a color as a string
	void readColor(const std::string &);
	// writeColor should display as a string the value stored in the value attribute of a type color object.
	void writeColor() const;
};



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1