For this problem, you will design and implement 2 classes and then write a drive
ID: 3548182 • Letter: F
Question
For this problem, you will design and implement 2 classes and then write a driver function to test these classes. The first will be a C++ class for an abstract data type color with a public enumeration type colorType that has the color values shown in Listing 10.8. Your 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 (see Figure 7.5). Modify class circle and the driver function in Listing 10.9 to include and use this class. You
Explanation / Answer
color.h 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
//color.h //Color class definition #ifndef COLOR_H #define COLOR_H
class color { public: enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor}; //Member functions //read color in void readColor (color); //write color out void writeColor (color); //accessor functions color getColor() const; private: //data members }; color.cpp
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
// File color.cpp // Color class implementation #include "color.h" #include <iostream> using namespace std; // Member Functions... // Set color void color::readColor(color c) { cColor = c; } void color::writeColor() const { cout << "color is " << int(cColor) << endl; } // accessor functions circle::color circle::getColor() const { return cColor; } circle.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// File circle.h // Circle class definition #include "color.h" #ifndef CIRCLE_H #define CIRCLE_H class circle { public: // Member Functions // constructor circle(int); // Set center coordinates void setCoord(int, int); // Set radius void setRadius(int); // Set color void readColor(color); // Compute the area float computeArea(); // Compute the perimeter float computePerimeter(); // Display attributes void displayCircle() const; // accessor functions int getX() const; int getY() const; int getRadius() const; color getColor() const; private: // Data members (attributes) int x; int y; int radius; color cColor; }; #endif // CIRCLE_H circle.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// File circle.cpp // Circle class implementation #include "circle.h" #include "color.h" #include <iostream> using namespace std; const float pi = 3.14159; // Member Functions... // constructor circle::circle(int r) { x = 0; y = 0; radius = r; cColor = nocolor; } // Set center position void circle::setCoord(int xArg, int yArg) { x = xArg; y = yArg; } // Set radius void circle::setRadius(int r) { radius = r; } // Set color void circle::readColor(color c) { cColor = c; } // Compute the area float circle::computeArea() { return pi * radius * radius; } // Compute the perimeter float circle::computePerimeter() { return 2 * pi * radius; } // 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; } // accessor functions circle::color circle::getColor() const { return cColor; } // Insert definitions for rest of accessor functions here. // ... int circle::getX() const { return x; } int circle::getY() const { return y; } int circle::getRadius() const { return radius; } makefile
1
2
3
4
5
6
7
8
9
10
11
circletest: circle.o circletest.o g++ circletest.o circle.o -o circle circle.o: circle.cpp circle.h color.cpp color.h g++ -c circle.cpp circletest.o: circletest.cpp circle.h color.h g++ -c circletest.cpp clean rm circletest circletest.o circle.o
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
//color.h //Color class definition #ifndef COLOR_H #define COLOR_H
class color { public: enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor}; //Member functions //read color in void readColor (color); //write color out void writeColor (color); //accessor functions color getColor() const; private: //data members };
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
// File color.cpp // Color class implementation #include "color.h" #include <iostream> using namespace std; // Member Functions... // Set color void color::readColor(color c) { cColor = c; } void color::writeColor() const { cout << "color is " << int(cColor) << endl; } // accessor functions circle::color circle::getColor() const { return cColor; } 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// File circle.h // Circle class definition #include "color.h" #ifndef CIRCLE_H #define CIRCLE_H class circle { public: // Member Functions // constructor circle(int); // Set center coordinates void setCoord(int, int); // Set radius void setRadius(int); // Set color void readColor(color); // Compute the area float computeArea(); // Compute the perimeter float computePerimeter(); // Display attributes void displayCircle() const; // accessor functions int getX() const; int getY() const; int getRadius() const; color getColor() const; private: // Data members (attributes) int x; int y; int radius; color cColor; }; #endif // CIRCLE_H 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// File circle.cpp // Circle class implementation #include "circle.h" #include "color.h" #include <iostream> using namespace std; const float pi = 3.14159; // Member Functions... // constructor circle::circle(int r) { x = 0; y = 0; radius = r; cColor = nocolor; } // Set center position void circle::setCoord(int xArg, int yArg) { x = xArg; y = yArg; } // Set radius void circle::setRadius(int r) { radius = r; } // Set color void circle::readColor(color c) { cColor = c; } // Compute the area float circle::computeArea() { return pi * radius * radius; } // Compute the perimeter float circle::computePerimeter() { return 2 * pi * radius; } // 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; } // accessor functions circle::color circle::getColor() const { return cColor; } // Insert definitions for rest of accessor functions here. // ... int circle::getX() const { return x; } int circle::getY() const { return y; } int circle::getRadius() const { return radius; } 1
2
3
4
5
6
7
8
9
10
11
circletest: circle.o circletest.o g++ circletest.o circle.o -o circle circle.o: circle.cpp circle.h color.cpp color.h g++ -c circle.cpp circletest.o: circletest.cpp circle.h color.h g++ -c circletest.cpp clean rm circletest circletest.o circle.o
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
//color.h //Color class definition #ifndef COLOR_H #define COLOR_H
class color { public: enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor}; //Member functions //read color in void readColor (color); //write color out void writeColor (color); //accessor functions color getColor() const; private: //data members }; Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.