![]() |
| | #1 |
| The Richness... Join Date: Jan 2006 Location: Ireland
Posts: 469
| Exam Question - Possible Mistake? 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 ();
};
Code: void translate (int amount)
{
m_nX += amount;
m_nY += amount;
}
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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #5 | |
| unleashed Join Date: Sep 2003
Posts: 693
| Quote:
__________________ source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense | |
| alphaoide is offline | |
| | #6 | |
| semi-colon generator Join Date: Sep 2005 Location: Chch, NZ
Posts: 597
| Quote:
Code: {
m_nX += amount;
m_nY += amount;
}
Code: {
CoOrd t;
t.m_nX += amount;
t.m_nY += amount;
}
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 | |
| | #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 | |
| | #8 | |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 1,032
| Quote:
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 | |
| | #9 |
| The superheterodyne. 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;
}
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 | |
| | #10 |
| The Richness... 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;
}
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 | |
| | #11 |
| Tropical Coder 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 | |
| | #12 |
| C++ Witch 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 | |
| | #13 |
| The superheterodyne. 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 | |
| | #14 | |
| Tropical Coder Join Date: Mar 2005 Location: Cayman Islands
Posts: 503
| Quote:
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |