C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2006, 03:22 PM   #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
Richie T is offline   Reply With Quote
Old 05-07-2006, 04:31 PM   #2
Registered User
 
Join Date: Aug 2005
Posts: 1,265
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.
Ancient Dragon is offline   Reply With Quote
Old 05-07-2006, 04:33 PM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,251
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.
Daved is offline   Reply With Quote
Old 05-07-2006, 09:20 PM   #4
Registered User
 
Join Date: Apr 2006
Posts: 1,312
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.
__________________
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.

Last edited by King Mir; 05-07-2006 at 09:24 PM.
King Mir is offline   Reply With Quote
Old 05-07-2006, 09:27 PM   #5
unleashed
 
alphaoide's Avatar
 
Join Date: Sep 2003
Posts: 693
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
alphaoide is offline   Reply With Quote
Old 05-07-2006, 09:41 PM   #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?
ChaosEngine is offline   Reply With Quote
Old 05-07-2006, 09:56 PM   #7
Registered User
 
Join Date: Apr 2006
Posts: 1,312
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.
King Mir is offline   Reply With Quote
Old 05-08-2006, 12:53 AM   #8
Dae
Deprecated
 
Dae's Avatar
 
Join Date: Oct 2004
Location: Canada
Posts: 1,032
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
Dae is offline   Reply With Quote
Old 05-08-2006, 01:32 AM   #9
The superheterodyne.
 
twomers's Avatar
 
Join Date: Dec 2005
Location: Ireland
Posts: 2,215
>> 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?
__________________
I blag!

Last edited by twomers; 05-08-2006 at 01:39 AM.
twomers is offline   Reply With Quote
Old 05-08-2006, 05:06 AM   #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
Richie T is offline   Reply With Quote
Old 05-08-2006, 07:59 AM   #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;
}
__________________
SWinC - Simple Windows Class

Last edited by Darryl; 05-08-2006 at 08:48 AM.
Darryl is offline   Reply With Quote
Old 05-08-2006, 08:34 AM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,319
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-08-2006, 08:41 AM   #13
The superheterodyne.
 
twomers's Avatar
 
Join Date: Dec 2005
Location: Ireland
Posts: 2,215
>> hasnt the implementation of translate() been changed by the preprocessor?

Circumstantially, yes it has, but emotionally, I don't think so. Interesting solution though!
__________________
I blag!
twomers is offline   Reply With Quote
Old 05-08-2006, 08:53 AM   #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.
__________________
SWinC - Simple Windows Class

Last edited by Darryl; 05-08-2006 at 08:57 AM.
Darryl is offline   Reply With Quote
Old 05-08-2006, 11:41 AM   #15
Registered User
 
Join Date: Jan 2005
Posts: 7,251
>> 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.
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help for my exam. easy question joker_tony C Programming 4 04-15-2008 03:09 PM
opengl DC question SAMSAM Game Programming 6 02-26-2003 09:22 PM
sort problem for an exam rjeff1804 C Programming 10 02-12-2003 10:33 PM
another exam question rjeff1804 C Programming 4 02-12-2003 10:29 PM
The AP Exam..... RoD A Brief History of Cprogramming.com 32 02-10-2003 09:46 PM


All times are GMT -6. The time now is 06:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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