Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anyone who knows C++

 
Paul Count
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a C++ code. I don't know the exactly meaning. Can anyone explain this in Java? Thanks a lot!

#include <stdio.h>

class keyAPI
{
public:
keyAPI(char *);
int LeftKey() {return leftKey;}
int RightKey() {return rightKey;}
private:
int leftkey;
int rightkey;
};

void main()
{
keyAPI keyclass("1");
printf("Left Key is %d\n\n", keyclass.LeftKey());
printf("Right Key is %d\n\n", keyclass.RightKey());
}

keyAPI::keyAPI(char * a)
{
if (a == "2")
{
leftKey = 1;
rightKey = 3;
}
else{
leftKey = 4;
rightKey = 6;
}
}
}
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give it a shot, though I haven't used C++ since school. If I make a mistake, someone please correct me.


[ May 13, 2004: Message edited by: Jason Fox ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason got it pretty much correct except for a couple of typos in "// typos? should be leftkey, rightkey?" -- it should say "...should be leftKey, rightKey?"
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That must be why I keep missing all those 'what does this code do?' questions. And they say JBuilder doesn't make you lazy. Oh, and one clarification. I believe that the first keyAPI(char *) is the prototype of the constructor. Am I right?
[ May 14, 2004: Message edited by: Jason Fox ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Fox:
I believe that the first keyAPI(char *) is the prototype of the constructor. Am I right?


Yep, you're right.

You missed the chance to make fun of main()'s signature. "void main()" isn't correct C++ (most compilers are generous about this, however.) Only "int" is a legal return type for main.
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're right, of course. But I've written enough sloppy C code to have violated that rule a few times myself (void main, indeed!). BTW, I don't know how anyone else feels, but I really don't like C++ at all. Too much of it feels hacked.
PS - why is this in the J2ME forum? Just curious.
[ May 14, 2004: Message edited by: Jason Fox ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Fox:
BTW, I don't know how anyone else feels, but I really don't like C++ at all. Too much of it feels hacked.
Ditto. Pointers and memory allocation gives me the creeps... (yeah, I know that comment is most often countered with "I guess you're a newbie, then")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic