Help me figure out a solution...

This isn't a homework assignment but I need some help. After banging my head against my desk for thirty minutes I THOUGHT I had a solution but I can't seem to get it to work. Here is the exercise:

Modify the Vector class header and implementation files (Listings 11.13 and 11.14)
so that the magnitude and angle are no longer stored as data components. Instead,
they should be calculated on demand when the magval() and angval() methods
are called.You should leave the public interface unchanged (the same public methods
with the same arguments) but alter the private section, including some of the
private method and the method implementations.Test the modified version with Listing 11.15, which should be left unchanged because the public interface of the
Vector class is unchanged


and here is my header file :

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
namespace VECTOR
{
    class Vector
    {
        public:
            enum Mode{RECT,POL};//rect for rectangular, pol for polar
        private:
            double x;
            double y;
            double mag;
            double ang;
            Mode mode;
            //private methods for setting values
            void set_mag();
            void set_ang();
            void set_x();
            void set_y();
        public:
            Vector();
            Vector(double n1, double n2, Mode form = RECT);
            void reset(double n1, double n2, Mode form = RECT);
            ~Vector();
            double xval () const { return x; } // report x value
            double yval () const { return y; } // report y value
            double magval() const { return mag; }
            double angval() const { return ang; }
            void polar_mode();
            void rect_mode();
            //operator overloading
            Vector operator+(const Vector & b) const;
            Vector operator-(const Vector & b) const;
            Vector operator-() const;
            Vector operator*(double n) const;
            //friends
            friend Vector operator*(double n, const Vector & a);
            friend std::ostream & operator<<(std::ostream & os, const Vector & v);
    };
} 


My proposed solution is to change the private methods : set_ang() and set_mag() to type double functions get_mag() and get_ang().

Then I would create two pointers that point to those functions called mag and ang - so when the public code calls magval or angval it returns the pointed to get_mag() and get_ang().

Is this the appropriate way to go about this code? Would you recommend a different route? I get type mismatches when I create the pointers to those functions - should this solution work? If so then I probably have syntax errors while creating my pointers to functions... they just are so hard to wrap my head around(syntactically).

I am trying not to modify public code, again (I could just return the values with magval() or angval() but I'm trying to do this with the private section only)

Thank you for reading my question, I hope someone out there has the knowledge to help me with this!
It seems like what you're doing is an exercise in changing the implementation of a class without changing the interface of a class.

If you change magval() to call a function, you haven't changed the interface. It's impossible to do what you're describing; if mag were a function pointer, you'd be returning a function pointer.

Again, as long as the interface remains the same (the signatures of public member functions) client code doesn't need to change. The definition (the bodies) of those functions can change without affecting the interface.
blahhhhhhhhhhhhhhhhhh are you kidding me? Thanks for clarifying... I guess I do know how to do this then, for some reason I felt that modifying the public code would change the interface.. but now that I step back and look at it again, the interface (what the client sees) remains the same, a call to magval() would remain the same yet I changed the way it works behind the scenes (simply by returning sqrt(x*x+y*y)) each time the function is called and not depending on private variables.

Thank you so much cire, I can rest easy now! (Not that I'm resting... I still have a few more exercises before I can.)
Topic archived. No new replies allowed.