CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2012
    Posts
    4

    Implementing classes and creating a driver function?

    Hi everyone, I'm taking my first class in c++, and I'm am having a few issues with this program, they stem from passing the color.h and .cpp into another .h and .cpp, and frankly getting how those two things really fit together.

    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’ll need to remove the declaration for color in class circle. Test your modified driver function with the new color and circle classes.

    The second class will be to design and implement a rectangle class similar to class circle. Be sure to incorporate the new color class you have written and tested in the first part of the programming exercise. Write a client program that asks the user to enter a shape name (circle or rectangle) and then asks the user for the necessary data for an object of that class. The program should create the object and display all its attributes.

    The circle class .h and .cpp files as well as the original driver function will be supplied. You are to provide the .h and .cpp files for the new color class and the modified driver function as well as the .h and .cpp files for the rectangle class and the client program that uses all three classes.


    What I have so far, and I'm pretty lost;

    color.h

    Code:
    Code:
    //color.h
    //Color class definition
    
    #include "stdafx.h"
    #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 cColor;
    };
    #endif



    color.cpp
    Code:
    Code:
    // File color.cpp
    // Color class implementation
    
    #include "stdafx.h"
    #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

    Code:
    Code:
    // File circle.h
    // Circle class definition
    
    #include "stdafx.h"
    #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

    Code:
    Code:
    // File circle.cpp
    // Circle class implementation
    
    #include "stdafx.h"
    #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

    Code:
    Code:
    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


    Thanks for any help. I'm pretty lost for this one

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Implementing classes and creating a driver function?

    You should only include the precompiled header file "stdafx.h" in cpp files, not in your header files.
    Header guards should include the include directives in your header files.
    Code:
    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 cColor;
    };
    Note the member variable highlighted in red. An instance of 'color' contains an instance of 'color' as a member, which ...
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Mar 2013
    Posts
    5

    Re: Implementing classes and creating a driver function?

    If you're looking for some more supplementary information on implementing functions, I made a board on that topic, check it out and tell me if you like it. http://www.verious.com/board/AKumar/...function-in-c/

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Implementing classes and creating a driver function?

    Quote Originally Posted by ak223 View Post
    If you're looking for some more supplementary information on implementing functions, I made a board on that topic, check it out and tell me if you like it.
    Spamming your own site here with every post is not cool.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Implementing classes and creating a driver function?

    Code:
    // accessor functions
    circle::color circle::getColor() const
    {
       return cColor;
    }
    Your colour class should have no knowledge of and no reference to your circle class.

    Your abstract data type should have an attribute for storing a single value of type colortype
    Code:
    	private:
    	//data members
    	color cColor;
    As previously stated, you can't have an instance of a class within its own class. What you need here is a variable of type colorType to hold the colour value.

    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 user needs to be be prompted to enter a colour as a string from the keyboard (eg red, blue etc as text). This string needs to be checked to make sure its valid and the corresponding value from colorType stored in the class private variable.

    The function writeColor should display as a string the value stored in the value attribute of a type color object
    This needs to convert the value stored in the class private variable into a string and displayed.

    Your getColor needs to return the value stored in the private variable as type colorType, whilst setColor should take as parameter a type of colorType, check that it is a valid value and then store it in the private variable.

    I suggest that you get the color class working first before you start on the circle/rectangle class. Write a main program that tests the color class by using its methods.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured