Thread: using a class get function from main

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    using a class get function from main

    I have created a base class point and a derived class circle. I am to test all the functions in a driver program to make sure everything works then create another derived class cylinder. Before I create cylinder I need to make sure all of my class functions work. I have done that to the exclusion of my get functions. I can't figure out how to call that from main. I can find all kinds of examples on the prototype and definiton, but nothing on the use.

    Can anyone help me on this?
    pointClass.h
    Code:
    #ifndef H_point
    #define H_point
    
    #include <iostream>
    using namespace std;
    
    class pointType
    {
    public:
    	pointType();
    	//default constructor
    	pointType(int xCoord, int yCoord);
    	//constructor
    	
    	void setPoint(int xCoord, int yCoord);
    	//stores the values of xCoord into x, and yCoord into y
    	void setX(int xCoord);
    	//stores the value of xCoord into x and y remains unchanged
    	void setY(int yCoord);
    	//x remains unchanged and stores the value of yCoord into y
    	void getPoint(int & xCoord, int & yCoord);
    	//returns the value of x into xCoord and y into yCoord
    	void getX(int & xCoord);
    	//returns just the value of x into xCoord
    	void getY(int & xCoord);
    	//returns just the value of y into yCoord
    	void printPoint() const;
    	//prints (x, y)
    	void printX()const;
    	//prints just x
    	void printY()const;
    	//prints just y
    
    private:
    	int x;
    	int y;
    };
    
    #endif
    pointClass.cpp
    Code:
    #include "pointClass.h"
    
    pointType::pointType()
    {
    	setPoint(0, 0);
    }
    
    
    pointType::pointType(int xCoord, int yCoord)
    {
    	setPoint(xCoord, yCoord);
    }
    
    
    void pointType::setPoint(int xCoord, int yCoord)
    {
    	x = xCoord;
    	y = yCoord;
    }
    
    
    void pointType::setX(int xCoord)
    {
    	x = xCoord;
    }
    
    
    void pointType::setY(int yCoord)
    {
    	y = yCoord;
    }
    
    
    void pointType::getPoint(int & xCoord, int & yCoord)
    {
    	xCoord = x;
    	yCoord = y;
    }
    
    
    void pointType::getX(int & xCoord)
    {
    	xCoord = x;
    }
    
    
    void pointType::getY(int & yCoord)
    {
    	yCoord = y;
    }
    
    
    void pointType::printPoint() const
    {
    	cout << "(" << x << ", " << y << ")" << endl;
    }
    
    
    void pointType::printX() const
    {
    	cout << "The value of x is: " << x << endl;
    }
    
    
    void pointType::printY() const
    {
    	cout << "The value of y is: " << y << endl;
    }
    circleClass.h
    Code:
    #ifndef H_circle
    #define H_circle
    
    #include <iostream>
    //#include <string>
    #include <cmath>
    #include "pointClass.h"
    
    const double PI = 3.14159;
    
    class circleType: public pointType
    {
    public:
    	circleType();
    	circleType(int xCoord, int yCoord, int radius);
    
    	void setRadius(int radius);
    	void setPointRadius(int xCoord, int yCoord, int radius);
    	void getRadius(int & radius);
    	void getPointRadius(int & xCoord, int & yCoord, int & radius);
    
    	double calcArea();
    	double calcCircum();
    	string WhereAmI(int & xCoord, int & yCoord);
    
    	void printRadius() const;
    	void printArea();
    	void printCircum();
    	void printPointRadius() const;
    	
    
    private:
    	int r;
    };
    
    #endif
    circleClass.cpp
    Code:
    #include "pointClass.h"
    #include "circleClass.h"
    
    circleType::circleType()
    	: pointType()
    {
    	r = 0;
    }
    
    circleType::circleType(int xCoord, int yCoord, int radius)
    		: pointType(xCoord, yCoord)
    {
    	r = radius;
    }
    
    void circleType::setRadius(int radius)
    {
    	r = radius;
    }
    
    void circleType::setPointRadius(int xCoord, int yCoord, int radius)
    {
    	setPoint(xCoord, yCoord);
    	r = radius;
    }
    
    void circleType::getRadius(int & radius)
    {
    	radius = r;
    }
    
    void circleType::getPointRadius(int & xCoord, int & yCoord, int & radius)
    {
    	getPoint(xCoord, yCoord);
    	radius = r;
    }
    
    double circleType::calcArea()
    {
    	return (PI * pow(r, 2));
    }
    
    double circleType::calcCircum()
    {
    	return (2 * PI * r);
    }
    
    void circleType::printRadius() const
    {
    	cout << "The current value of radius is: " << r << endl;
    }
    
    void circleType::printArea()
    {
    	cout << "The area of a circle with a radius of " 
    		 << r << " is " << calcArea() << endl;
    }
    
    void circleType::printCircum()
    {
    	cout << "The circumference of a circle with a radius of "
    		 << r << " is " << calcCircum() << endl;
    }
    
    void circleType::printPointRadius() const
    {
    	pointType::printPoint();
    	cout << " and the radius of the circle is " << r << "." << endl;
    }
    
    string circleType::WhereAmI(int & xCoord, int & yCoord)
    {
    	getPoint( xCoord, yCoord);
    	if (xCoord > 0 && yCoord > 0)
    		return "My Center is in the first quadrant of the cartesian plane.";
    	else if (xCoord > 0 && yCoord < 0)
    		return "My Center is in the second quadrant of the cartesian plane.";
    	else if (xCoord < 0 && yCoord < 0)
    		return "My Center is in the third quadrant of the cartesian plane.";
    	else 
    		return "My Center is in the fourth quadrant of the cartesian plane.";
    }
    and finally my driver program: which is where my hang up seems to be.
    Code:
    #include "pointClass.h"
    #include "circleClass.h"
    
    //to test the pointClass prior to building the circleClass
    int main()
    {
    	//operation calls to check the point class
    	pointType cindyPoint;
    	
    	cout << "The x and y default values of cindyPoint are: ";
    	cindyPoint.printPoint();
    	cindyPoint.setX(19);
    	cindyPoint.printX();
    	cout << "The new x and y values of cindyPoint are: ";
    	cindyPoint.printPoint();
    	cindyPoint.setY(57);
    	cindyPoint.printY();
    	cout << "The x and new y values of cindyPoint are: ";
    	cindyPoint.printPoint();
    	cindyPoint.setPoint(20, 07);
    	cout << "The cindyPoint has been reset to x and y values of: ";
    	cindyPoint.printPoint();
    
    	pointType badPoint;
    	cout << "A new point in town - badPoint - with x and y default values of: ";
    	badPoint.printPoint();
    	badPoint.setX(32);
    	badPoint.setY(23);
    	badPoint.printX();
    	badPoint.printY();
    	cout << "The new x and y values of badPoint are: ";
    	badPoint.printPoint();
    	badPoint.setPoint(-62, -78);
    	cout << "And again, the x and y values of badPoint are: ";
    	badPoint.printPoint();
    
    
    	//operation calls to check the circle class
    
    	circleType cindyCircle;
        
    	cout << "cindyPoint has a new friend in town - cindyCircle.";
    	cout << "\ncindyCircle has default x and y values of: ";
    	cindyCircle.printPointRadius();
    	cindyCircle.setPointRadius(3, 3, 3);
    	cout << "cindyCircle has reset her values to: ";
    	cindyCircle.printPointRadius();
    	cout << "cindyCircle called in some calculating friends to find a size." << endl;
    	cindyCircle.calcArea();
    	cindyCircle.printArea();
    	cindyCircle.calcCircum();
    	cindyCircle.printCircum();
    	cindyCircle.WhereAmI();
    	//cindyCircle.WhereAmI(-3, 3);
    	//cindyCircle.WhereAmI(-3, -3);
    	//cindyCircle.WhereAmI(3, -3);
    }
    Thanks for any input.
    Last edited by clegs; 11-03-2007 at 11:50 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    include "pointClass.h"
    Could the typo error above be the source of your problems? It should be #include, not just include.

    Incidentally, constants defined using #define are conventionally named all in uppercase. So, instead of using H_point and H_circle for the inclusion guards, use H_POINT and H_CIRCLE respectively.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    No. I have the # symbol infront of my include, I just didn't get it when I made the copy to paste.

    The problem is in using the getter function in main. I don't know how to call it propoerly with the right parameter list. This is what I tried for circle.
    ` cindyCircle.WhereAmI();
    //cindyCircle.WhereAmI(-3, 3);
    //cindyCircle.WhereAmI(-3, -3);
    //cindyCircle.WhereAmI(3, -3);

    I deleted what I had for point before creating circle, because I couldn't get them to work either.
    I have emailed my instructor days ago and haven't heard anything from him. So I am on my own to figure it all out.

    When I copy the header and implementation files into my project for the cylinder everything is ok outside of my external reference to main. So the class and functions are built right, it is just using them in main.

    Thanks for the additional info on the #define. The text I am using didn't use the all caps, but the research I've done on this issue uses the syntax you described.
    Last edited by clegs; 11-03-2007 at 11:58 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, I see. The problem is that you are passing arguments by reference when you actually do not need to pass them by reference.
    Code:
    string WhereAmI(int & xCoord, int & yCoord);
    should be:
    Code:
    string WhereAmI(int xCoord, int yCoord);
    If you pass by value or by const reference you can use literals such as -3 and 3 as arguments. Otherwise, you have to pass variables, which in this case does not make sense.

    EDIT:
    Looking more closely, I see that this is still not correct. It looks like you should declare WhereAmI as:
    Code:
    string WhereAmI() const;
    xCoord and yCoord should then purely be local variables in WhereAmI(). You should define the member function as const since it does not change the observable state of the object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    What about if I want to use the values already stored in x & y in my point class?

  6. #6
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I wanted to use the values set in x and y, but when I couldn't figure that out I just tried using values I passed in on the call.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It depends on how you want to use them. In this case it is a matter of using the point class' getX(), getY(), getPoint(), which should all be const member functions too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I just modified my function definition to:
    Code:
    void circleType::WhereAmI(int xCoord, int yCoord)
    {
    	if (xCoord > 0 && yCoord > 0)
    		cout<< "My Center is in the first quadrant of the cartesian plane." << endl;
    	else if (xCoord > 0 && yCoord < 0)
    		cout << "My Center is in the second quadrant of the cartesian plane." << endl;
    	else if (xCoord < 0 && yCoord < 0)
    		cout << "My Center is in the third quadrant of the cartesian plane." << endl;
    	else 
    		cout << "My Center is in the fourth quadrant of the cartesian plane." << endl;
    }
    and the portion of main to:
    Code:
    	cindyCircle.WhereAmI(3, 3);
    	cindyCircle.WhereAmI(-3, 3);
    	cindyCircle.WhereAmI(-3, -3);
    	cindyCircle.WhereAmI(3, -3);
    which uses the arguments correctly.

    when I included the

    getPoint(xCoord, yCoord);

    in my function definiton it used the values that were set in the point variables which is what I want it to do, but then the arguments in the function call in main were ignored. How do I set up the argument list in the function call "cindyCircle.WhereAmI()" to use the values that exist in x and y of the point class.

    when I use the getPoint() in my WhereAmI finction definition and don't include a signature, the compiler doen't like it. I've tried that.
    Last edited by clegs; 11-04-2007 at 12:26 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does this mean?
    Code:
    cindyCircle.WhereAmI(3, 3);
    So, cindyCircle could be at a location, but by asking it where it is, its location suddenly changes? That does not make sense. If so, you would not be asking it its location, you would be telling it what is its location, so WhereAmI() would be a poor choice of name.

    I think you really want something like this:
    Code:
    void circleType::WhereAmI()
    {
        int xCoord, int yCoord;
        getPoint(xCoord, yCoord);
        if (xCoord > 0 && yCoord > 0)
            cout<< "My Center is in the first quadrant of the cartesian plane." << endl;
        else if (xCoord > 0 && yCoord < 0)
            cout << "My Center is in the second quadrant of the cartesian plane." << endl;
        else if (xCoord < 0 && yCoord < 0)
            cout << "My Center is in the third quadrant of the cartesian plane." << endl;
        else 
            cout << "My Center is in the fourth quadrant of the cartesian plane." << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    When I use this definition my compiler complains, loud:
    Code:
    void circleType::WhereAmI() const
    {
    	getPoint(xCoord, yCoord);
    	if (xCoord > 0 && yCoord > 0)
    		cout<< "My Center is in the first quadrant of the cartesian plane." << endl;
    	else if (xCoord > 0 && yCoord < 0)
    		cout << "My Center is in the second quadrant of the cartesian plane." << endl;
    	else if (xCoord < 0 && yCoord < 0)
    		cout << "My Center is in the third quadrant of the cartesian plane." << endl;
    	else 
    		cout << "My Center is in the fourth quadrant of the cartesian plane." << endl;
    }
    I get multiple errors but they all stem from xCoord and yCoord are undeclared identifiers.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    That is it! That is what I wanted to happen. I want to set and reset the points; then call WhereAmI() and find out what quadrant the point is in.
    Thanks!

    In general is that how the getter function is used - within another class function definition? Then to use it you have to declare the variables before getting the data within the function that the get is used? I have been trying for 4 days to figure out how to use the get inside the WhereAmI() and never thought of declaring the variables before using the getPoint(). There isn't any examples that I could find on how to use and test the get() to make sure it too was working. I figured out how to test all the other functions, but not that one.
    Last edited by clegs; 11-04-2007 at 12:41 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is it! That is what I wanted to happen. I want to set and reset the points; then call WhereAmI() and find out what quadrant the point is in.
    That is not good, at least if you want it to be a member function. A function should do one thing and do it well.

    From what I gather, your circle objects are determined by two factors: the radius and the centre point. As such, your Circle is not a Point. Rather, your Circle has a radius and has a centre Point. Consequently, you should not use inheritance but use composition instead.

    With that in mind, you could have a setter/getter for radius, and a setter/getter for Point. Instead of using pass by reference for the getters so as to have output parameters, just return values:
    Code:
    int circleType::getRadius() const
    {
        return r;
    }
    
    Point circleType::getPoint() const
    {
        return point; // point is a member variable of type Point.
    }
    With the principle of "do one thing and do it well", you would not provide a getter that gets both radius and point. For the Point class, you would not provide a getter that gets both x and y, though you might provide a convenience function that sets both x and y, but this convenience function could be a free function instead.

    So, to accomplish what you want for WhereAmI(), you may end up writing a free function:
    Code:
    string moveCircle(CircleType& circle, int xCoord, int yCoord)
    {
        circle.setPoint(Point(xCoord, yCoord)); // Move the circle.
        // Return string describing new location of circle.
        if (xCoord > 0 && yCoord > 0)
            return "My Center is in the first quadrant of the cartesian plane.";
        else if (xCoord > 0 && yCoord < 0)
            return "My Center is in the second quadrant of the cartesian plane.";
        else if (xCoord < 0 && yCoord < 0)
            return "My Center is in the third quadrant of the cartesian plane.";
        else 
            return "My Center is in the fourth quadrant of the cartesian plane.";
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Actually I didn't work 4 days on getting get to work in WHereAmI() I was trying to test the get function, period, nny way I could, and could not figure it out. Using it in WhereAmI was just the best effort after trying to use just cindyPoint.getPoint() or .getPointRadius() in main without any success.

    Thanks! Your information is great, what I need. When I was creating this code, I realized part way in that I should be using composition because circle has a point, but is not a point, which is what you just said. So, I wrote my instructor regarding this issue and again, he never got back to me. SO I continued along the same path because I was almost done.

    Now I have to create a cylinder class from these two classes. With that I will use composition and not inheritance because of the same "has a" not "is a" relationship.

    Thank you so much. I have so much reading to do, so many programs to write (30) and so little time (end date 12-18) to finish this class, and seemingly so little help from the instructor.

    You have helped a great deal laserlight. I am printing this thread out for reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM