Exam Question - Possible Mistake?

This is a discussion on Exam Question - Possible Mistake? within the C++ Programming forums, part of the General Programming Boards category; This may seem a pretty basic question but it has been bugging me for the last couple of days. This ...

  1. #1
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469

    Exam Question - Possible Mistake?

    This may seem a pretty basic question but it has been bugging me
    for the last couple of days. This question was on my end of year
    C++ exam and was worth 4%. While I'm not too worried about
    my grade, others in my class may be, and if the lecturer made a
    mistake, it's policy that we get the marks for a correct answer if
    we attempted it. Here goes:

    Code:
    class CoOrd
    {
    private:
        int m_nX;
        int m_nY;
    
    public:
        CoOrd ();
        CoOrd (int nX, int nY);
    
        void someOp ();
    };
    Given the following global function,

    Code:
    void translate (int amount)
    {
        m_nX += amount;
        m_nY += amount;
    }
    that adds a set value - amount - to the m_nX and m_nY member
    variables of the CoOrd class defined above, what changes would
    have to be made to the CoOrd class definition for this function to
    work. You cannot change the translate function's implementation.

    Thats the question, and the answer i guessed was to make
    translate a friend of the class, thinking that it may be legal to
    call the function off a class instance, but following the exam i
    tried it out and it didn't work. The only alternative i can think
    of would be to make translate a member of the class, but as the
    question specifies, translate is global - wouldn't making it a
    member in essence change its implementation?

    Obviously translate cannot compile on its own as m_nX and m_nY
    are undeclared locally, so is the question actually wrong or am I
    really missing some other way of getting it to work? Thanks in
    advance, I really appreciate any advice on this.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    make m_nX and m_nY public static objects of the class.

    [edit]that doesn't work either - at least with Dev-C++ compiler[/edit]
    Last edited by Ancient Dragon; 05-07-2006 at 04:36 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    My guess is that making it a member would be the answer. Technically, that doesn't change the implementation of the function, since the code in the body of the function would remain unchanged. It is somewhat of a strange question, IMO.

    >> make m_nX and m_nY public static objects of the class.
    Even if those variables were public static, you'd have to change the implementation by using CoOrd::m_nX and CoOrd::m_nY.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You can't make it a member becouse that would mean you must call it as a member like: myCoOrd.translate(5).

    But then again, calling translate() by itself does not make sense without an object. Translate must somehow know which instance of CoOrd to change.

    You could, in addition to making it a friend function, make m_nX and m_nY static, and create an instance of CoOrd inside translate(). That would not change the implementation of translate, but would make all CoOrd objects identical.
    Last edited by King Mir; 05-07-2006 at 09:24 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Richie T
    ... so is the question actually wrong or am I
    really missing some other way of getting it to work? Thanks in
    advance, I really appreciate any advice on this.
    Just get the answer from your lecturer and go from there.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by King Mir
    You can't make it a member becouse that would mean you must call it as a member like: myCoOrd.translate(5).

    But then again, calling translate() by itself does not make sense without an object. Translate must somehow know which instance of CoOrd to change.

    You could, in addition to making it a friend function, make m_nX and m_nY static, and create an instance of CoOrd inside translate(). That would not change the implementation of translate, but would make all CoOrd objects identical.
    initial code
    Code:
    {
        m_nX += amount;
        m_nY += amount;
    }
    your suggestion
    Code:
    {
        CoOrd t;
        t.m_nX += amount;
        t.m_nY += amount;
    }
    how does that not change the implementation? and why to you need to create a CoOrd object and make m_nX and m_nY static? that makes no sense.

    RichieT, I have serious doubts about you lecturers level of C++ knowledge. First off, it does seem like a bad question. Second, anyone that still uses hungarian notation is EVIL!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    It does not change The way translate is used when called. But I guess you're right, that's probably not what your professor ment. I'm confusig terms here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by ChaosEngine
    Second, anyone that still uses hungarian notation is EVIL!


    But seriously, why?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> First off, it does seem like a bad question. Second, anyone that still uses hungarian notation is EVIL!

    Haha, I never liked it myself. And I agree, dodgy question alright. Seems like a lot of effort for identifying variables ... but I guess it was drilled into him somehow.

    >> You cannot change the translate function's implementation
    Does that mean you can't change the function heading to :

    Code:
    void CoOrd::translate( int amount )
    {
        m_nX += amount;
        m_nY += amount;
    }
    ? I mentioned a couple of the above recommendations in the exam, and what I mentioned here, but the question is:

    Does changing the function's header/heading change the function's implementation?
    Last edited by twomers; 05-08-2006 at 01:39 AM.

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>RichieT, I have serious doubts about you lecturers level of C++
    knowledge

    Well perhaps serious is a bit extreme, he's made a few mistakes
    like telling us that the following is illegal in C++ without a typedef:

    Code:
    struct thing
    {
       int x, y, z;
    };
    
    int main (void)
    {
        thing instance;
    }
    He's mixing up C with C++ there. He also uses void main, which
    i don't like, but other than that it's been standard code

    What it comes down to i suppose is the wording of the question:

    >>Does changing the function's header/heading change the
    function's implementation?

    I think we might be able to argue that technically, making a
    function a class member does change its implementation.

    >>Just get the answer from your lecturer and go from there

    Yes i think we will, i just wanted to see if there was a solution,
    and it appears that there is none unless the word global is
    removed from the question. I just wanted some ammunition
    of sorts before i went further, since there are many programmers
    here more experienced than me.

    Thanks for for all the suggestions - legends as always
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Here's your solution with translate untouched.
    *Edit* a better solution without statics and using a global instance of the class.
    **RE-Edit** now able to use any instance

    Code:
    #include <iostream>
    using namespace std;
    class CoOrd
    {
        friend void translate(int x);
        friend int main();
    private:
         int m_nX;
         int m_nY;
    
    public:
        CoOrd ():m_nX(0),m_nY(0){};
        CoOrd (int nX, int nY);
        static CoOrd* gpCoOrd;
    
        void someOp ();
    };
    CoOrd* CoOrd::gpCoOrd = 0;
    
    #define m_nX CoOrd::gpCoOrd->m_nX
    #define m_nY CoOrd::gpCoOrd->m_nY
    
    
    void translate (int amount)
    {
        m_nX += amount;
        m_nY += amount;
    }
    
    #undef m_nX
    #undef m_nY
    
    int main()
    {
        CoOrd anyInstance;
        anyInstance.gpCoOrd = &anyInstance;
    
        translate(10);
    
    
        cout << anyInstance.m_nX << endl;
        cout << anyInstance.m_nY << endl;
    }
    Last edited by Darryl; 05-08-2006 at 08:48 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Interesting solution, but then that is an abuse of the preprocessor. Then it begs the question: hasnt the implementation of translate() been changed by the preprocessor?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> hasnt the implementation of translate() been changed by the preprocessor?

    Circumstantially, yes it has, but emotionally, I don't think so. Interesting solution though!

  14. #14
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by twomers
    >> hasnt the implementation of translate() been changed by the preprocessor?

    Circumstantially, yes it has, but emotionally, I don't think so. Interesting solution though!
    Actually I think not, as the op stated, mn_X and mn_Y is undefined in translate, therefore must be defined outside of translate and that is all I am doing, giving them meaning.

    The only other way I can maybe think of without preprocessor trick is to somehow make them references to static member. I tried that but could get quite get it right.
    Last edited by Darryl; 05-08-2006 at 08:57 AM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Does changing the function's header/heading change the function's implementation?

    IMO, no. It changes the interface that it belongs to and that it used to belong to, but the implementation is still the same. It changes what it uses (the meaning of the variables, in this case), but it doesn't change how they are used. Implementation is how something is implemented, not the data it is implemented with or the method in which it can be called.

    >> anyone that still uses hungarian notation is EVIL!

    I still use this at my work, primarily because of the large mass of existing code already uses it, so it would be more confusing to change styles intermittently. I have to admit, while I don't use it in my own small projects, I have gotten used to it in the big application. Intellisense and other IDE niceties that are supposed to help with variable type information don't always work in such a massive beast of code, and I have gotten good at quickly changing the variable name if I change the type, which is one of the burdens of hungarian notation. I'm not saying it's a great thing, but I can see why some people like it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help for my exam. easy question
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-15-2008, 03:09 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  3. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 09:33 PM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 09:29 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21