GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2006, 18:54
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Am I reading this assignment correctly?


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
CPP / C++ / C Code:
/*
 *  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
CPP / C++ / C Code:
/*
 *  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
CPP / C++ / C Code:
#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
CPP / C++ / C Code:
/*
 *  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
CPP / C++ / C Code:
/*
 *  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
CPP / C++ / C Code:
/*
 *  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
CPP / C++ / C Code:
/*
 *  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;
//}
  #2  
Old 09-May-2006, 06:50
davis
 
Posts: n/a

Re: Am I reading this assignment correctly?


I think that what it is asking you to do is to add to your "circle" class a member of type "color."

I think that the very basic implementation shown in the (abbreviated) code below is what the assignment is asking from you, albeit with obvious separation into declaration files and implementation files.

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;

class Color
{
public:
    enum colorType { RED, GREEN, BLUE };
    Color( colorType color ) : m_color( color ){}
    void writeColor()
    {
	string str = "";
	switch( m_color )
	{
	    case RED:
		str = "RED";
		break;
	    case GREEN:
		str = "GREEN";
		break;
	    case BLUE:
		str = "BLUE";
		break;
	    default:
		str = "Unspecified Color!";
		break;
	}
	cout << str << endl;
    }
private:
    int m_color;
};

class Circle
{
public:
    Circle() : m_color( Color::BLUE ) {}
    Circle( Color::colorType color ) : m_color( color ) {}
    void writeProps()
    {
	m_color.writeColor();
    }
private:
    Color m_color;
};

int main()
{
    Circle c1;  // default ctor
    c1.writeProps();

    Circle c2( Color::GREEN );  // args ctor
    c2.writeProps();

    return 0;
}

Output:

BLUE
GREEN


:davis:
  #3  
Old 09-May-2006, 09:39
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Am I reading this assignment correctly?


OK, so you read the assignment the same way, and it is possible to implement. The only problem for me is that the book hasn't covered the implementation at all!! This is why I really hate this textbook sometimes.....

Thanks Davis!
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Not sure I understand this assignment.... earachefl C++ Forum 7 05-May-2006 16:12
Reading in from a tab delimited text file? selent C Programming Language 3 13-Apr-2006 05:52
Need Help reading HEX data from a sensor Moooey C Programming Language 4 09-Mar-2005 13:46
Circular Linked Queue Copy Constructor and Assignment Operator Not Working? wc3promet C++ Forum 0 17-Oct-2004 07:55
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.