The following assignment refers to an existing "circle" class which I am including in condensed form, leaving in only the functions that have a direct bearing on the problem.
The assignment:
Quote:
Write a C++ class for an abstract data type "color" with a public enumeration type colorType. that has the color values shown in (Listing 10. . Your abstract data type should have an attribute for storing a single value of type colorType and member functions for reading and writing a color value as well as setting and accessing it. The function writeColor should display as a string the value stored in the value attribute of a type "color" object. Modify class "circle" and the driver function in (Listing 10.9) to include and use this class. You'll need to remove the declaration for color in class circle. Test your modified driver function with the new "color" and "circle" classes.
|
Question - how do you modify the circle class to include the color class? The book doesn't say anything about that yet. I've added #include color.h to the circle.cpp file and have messed around with trying to call a circle class member function but can't get it to work. Am I misreading the assignment? Is it worded incorrectly? Can you call a member function from another class from within a class?
I hope this makes sense to someone.....
circle.h
/*
* circle.h
*
*/
#ifndef CIRCLE_H
#define CIRCLE_H
class circle
{
public:
enum color {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor};
//Member functions
//constructor
circle();
//Set color
void setColor(color);
//Display attributes
void displayCircle() const;
//Accessor functions
color getColor() const;
private:
//Data members
int x;
int y;
float radius;
color cColor;
float area;
float perimeter;
};
#endif
circle.cpp
/*
* circle.cpp
*
*/
#include "circle.h"
#include <iostream>
#include <string>
using namespace std;
const float pi = 3.14159;
//Member functions
//Constructor
circle::circle()
{
x = 0;
y = 0;
radius = 0;
cColor = nocolor;
area = 0.0;
perimeter = 0.0;
}
//Set color
void circle::setColor(color c)
{
cColor = c;
}
//Display attributes
void circle::displayCircle() const
{
cout << "x-coordinate is " << x << endl;
cout << "y-coordinate is " << y << endl;
cout << "radius is " << radius << endl;
cout << "color is " << int(cColor) << endl;
cout << "area is " << area << endl;
cout << "perimeter is " << perimeter << endl;
}
//Accessor functions
circle::color circle::getColor() const
{
return cColor;
}
main
#include "circle.h"
#include <iostream>
using namespace std;
int main () {
circle myCircle;
//Set circle attributes
myCircle.setColor(circle::magenta);
//Display the circle attributes
cout << "The circle attributes follow:" << endl;
myCircle.displayCircle();
return 0;
}
color.h
/*
* color.h
*
*/
#ifndef COLOR_H
#define COLOR_H
class color
{
public:
enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, noColor};
color();
color(colorType);
void readColor();
void writeColor() const;
void setColor(colorType);
color getColor() const;
private:
colorType aColor;
};
#endif
color.cpp
/*
* color.cpp
*
*/
#include "color.h"
#include <iostream>
using namespace std;
//Constructors
color::color()
{
aColor = noColor;
}
color::color(colorType colorArg)
{
aColor = colorArg;
}
void color::readColor()
{
string cString;
cout << "Enter a color. Your choices are:" << endl
<< "black, blue, green, cyan, red, magenta, brown, light gray, or no color. ";
cin >> cString;
if (cString == "black") aColor = black;
else if (cString == "blue") aColor = blue;
else if (cString == "green") aColor = green;
else if (cString == "cyan") aColor = cyan;
else if (cString == "red") aColor = red;
else if (cString == "magenta") aColor = magenta;
else if (cString == "brown") aColor = brown;
else if (cString == "light gray") aColor = lightgray;
else aColor = noColor;
}
void color::writeColor() const
{
cout << "The color is ";
switch(aColor)
{
case black: cout << "black" << endl; break;
case blue: cout << "blue" << endl; break;
case green: cout << "green" << endl; break;
case cyan: cout << "cyan" << endl; break;
case red: cout << "red" << endl; break;
case magenta: cout << "magenta" << endl; break;
case brown: cout << "brown" << endl; break;
case lightgray: cout << "light gray" << endl; break;
case noColor: cout << "not assigned" << endl; break;
}
}
void color::setColor(colorType colorArg)
{
aColor = colorArg;
}
color::color color::getColor() const
{
return aColor;
}
MODIFIED circle.h
/*
* circle.h
*
*/
#ifndef CIRCLE_H
#define CIRCLE_H
class circle
{
public:
//Member functions
//constructor
circle();
//Set color
//void setColor(color);
//Display attributes
void displayCircle() const;
//Accessor functions
//color getColor() const;
private:
//Data members
int x;
int y;
float radius;
//color cColor;
float area;
float perimeter;
};
#endif
MODIFIED circle.cpp
/*
* circle.cpp
*
*/
#include "circle.h"
#include "color.h"
#include <iostream>
#include <string>
using namespace std;
const float pi = 3.14159;
//Member functions
//Constructor
circle::circle()
{
x = 0;
y = 0;
radius = 0;
//cColor = nocolor;
area = 0.0;
perimeter = 0.0;
}
//Set color
//void circle::setColor(color c)
//{
//cColor = c;
//}
//Display attributes
void circle::displayCircle() const
{
cout << "x-coordinate is " << x << endl;
cout << "y-coordinate is " << y << endl;
cout << "radius is " << radius << endl;
//cout << "color is " << int(cColor) << endl;
cout << "area is " << area << endl;
cout << "perimeter is " << perimeter << endl;
}
//Accessor functions
//circle::color circle::getColor() const
//{
// return cColor;
//}