Click to See Complete Forum and Search --> : 110 percent novice
hexaplus
November 28th, 2004, 05:03 PM
I have written a good deal of code in java and now must port a program to C++.
If I have a method that returns an object in java equivalently I will return a pointer in c++?
example:
//java
public treeNode getNode(){
return rootNode;
}
//C++
treeNode* getNode(){
return *rootNode;
}
//please pick apart any and all syntactical errors I have made. Thanks!!
wien
November 28th, 2004, 06:47 PM
If I have a method that returns an object in java equivalently I will return a pointer in c++?Not neccesarily. In C++ you have the choice to return objects by either value (like ints and floats in Java) or by reference (through pointers/references). Which one you choose depends completely on what you are trying to achieve. Generally speaking you should only use pointers if there is a particular reason why returning an actual object won't work. (Like I suspect it is in your case.)
One thing though... Trying to directly port a piece of code from Java to C++, is almost always a bad idea. Although very similar in syntax, there are a lot of differences between the two languages, most of which greatly impact the way you implement different concepts. Java programmers like yourself do for example have a tendency to be looking for the Object class, and often try to make one themselves if they want a function to take any kind of object. A much better solution would be to take advantage of C++' template facillity, something most of them overlook completely.
My point is, don't get too caught up in the "Java-way", and try to understand how C++ works instead. Just a friendly piece of advice. :)//please pick apart any and all syntactical errors I have made. Thanks!!To tell if you have done any syntactical blunders, we will need to see some more code. It all depends on how rootNode is declared.
hexaplus
November 28th, 2004, 10:58 PM
mm rootNode is a non-static variable in an instance of treeNode. I guess a better name for the example would be leftNode as in a linked list binary tree.
Paul McKenzie
November 29th, 2004, 12:41 AM
Java programmers like yourself do for example have a tendency to be looking for the Object class, and often try to make one themselves if they want a function to take any kind of object. A much better solution would be to take advantage of C++' template facillity, something most of them overlook completely.Not only that, Java programmers who are trying to port without learning C++ properly believe that "new" in Java is the same as "new" in C++. A sure-fire way to know that a Java programmer has (poorly) written a C++ program is the overuse of "new" all over the place in the program. The program is then full of memory leaks, other run-time programs, and is near impossible to maintain.
The "do it myself object class" and the overusage of "new"
http://www.codeguru.com/forum/showthread.php?t=269528&highlight=object+java
http://www.codeguru.com/forum/showthread.php?t=271051&highlight=object+java
are the two most obvious mistakes Java programmers make when writing a C++ program.
My point is, don't get too caught up in the "Java-way", and try to understand how C++ works instead. Just a friendly piece of advice.More than friendly advice, it is mandatory that the C++ language is learned correctly, independent of Java.
Regards,
Paul McKenzie
Paul McKenzie
November 29th, 2004, 12:42 AM
I have written a good deal of code in java and now must port a program to C++.
If I have a method that returns an object in java equivalently I will return a pointer in c++?
No, not necessarily.
But in general, it is not a good idea to translate from any language to C++ "line-by-line" -- it doesn't work that way. You must actually sit down and learn the language, and then accomplish what you want to do in terms of the C++ language, not in terms of Java, Cobol, Fortran, Basic, or any other language you may have used before.
Regards,
Paul McKenzie
cilu
November 29th, 2004, 02:04 AM
If I have a method that returns an object in java equivalently I will return a pointer in c++?
//C++
treeNode* getNode(){
return *rootNode;
}
You can do this as well:
treeNode getNode(){
return rootNode;
}
But in this case treeNode must have an overloaded = operator:
class treeNode
{
treeNode(); // default cst
treeNode(const treeNode& node){
// copy node into this
}
const treeNode& operator=(const treeNode& rval){
// copy rval into this
return *this;
}
};
However, returning a pointer or a reference is faster than returning an object, because no copy of objects is involved.
Andreas Masur
November 29th, 2004, 02:35 AM
But in this case treeNode must have an overloaded = operator:
Well...actually you only need to supply an overloaded assignment operator, if the class deals with dynamically allocated memory...otherwise simply use the default one provided by the C++ implementation.
Furthermore, one should always define all three constructors/operators in case of dynamically allocated memory...
wien
November 29th, 2004, 06:13 AM
mm rootNode is a non-static variable in an instance of treeNode. I guess a better name for the example would be leftNode as in a linked list binary tree.I meant, what type is rootNode? Is it a treeNode, treeNode*, treeNode**, foo*, what?
Again, if you show us more code, we can help you out a lot better by suggesting solutions to things you might be doing wrong.
Kevin McFarlane
November 29th, 2004, 06:22 AM
No, not necessarily.
But in general, it is not a good idea to translate from any language to C++ "line-by-line" -- it doesn't work that way. You must actually sit down and learn the language, and then accomplish what you want to do in terms of the C++ language, not in terms of Java, Cobol, Fortran, Basic, or any other language you may have used before.
Regards,
Paul McKenzie
Same applies when translating from C++ to any other language. You need to learn the idioms of the other language. Though, typically, going from these other languages to C++ is the more difficult transition.
hexaplus
November 29th, 2004, 02:42 PM
sorry more code it is:
/** ALP.h
*
*/
class ALP
{
public:
treeNode * root;//first node in tree
treeNode * mom;//temp var for roundUmUp method
treeNode * curNode;//holds current tree position i
treeNode * garbage;//temp variable that holds cur
string ansQuest;//the yes or no answer to distingQ
string distingQ;//holds a quesiton
string ansAnimal;//the animal the user chose that was not in the tree
string guessedAnimal;//animal that curNodes returns from getAnimal method
bool notDone; //variable that is responsible for exiting startdown loop
bool cameFrom;// which child curnode
int main()
{
}
string talk()//performs input from keyboard
{
}
private:
string play;//the y/n/q var
int state; //holds int that is returned by treeNode.askQuest
string userGarbage;//trash var for i/0
/**
*Traverses tree starting at staic location root
*
*
* @param none
* @return void
*/
void startDown()
{
}
/**
* Modifies tree so that:
* curNode is replaced by new non leaf node, garbage.
* curNode is made a child of the new leaf node.
* another child(leaf) is created from users response.
*
* @param none
* @return void
*/
void roundUmUp()
{
}
//--------------------//
/**
Creates abstract data types, treeNodes has all necessary methods to modify tree
*
* @version Version 1.0
*/
class treeNode
{
public:
/**
* An empty constructor for all question nodes
*
* @param none
* @return treeNode
*/
treeNode ()
{
}
/**
* A constructor for all end nodes, that adds an end flag on creation.
*
* @param int
* @return treeNode
*/
treeNode (int blah)
{
}
/**
* Returns the left child of this.treeNode.
*
* @param none
* @return treeNode
*/
treeNode getLeft()
{
}
/**
* Method used to ask the nodes resident question.
* Returns an int under the following convention:
* 0 The answer to question was yes and the node is not at the end of the tree.
* 1 The answer to question was no and the node is not at the end of the tree.
* 2 The answer to question was yes and the node is a leaf.
* >=3 The answer was no and the node it s leaf.
* @param none
* @return int
*/
int askQuest()
{
}
/**
* returns the qeustion of this.treeNode for debuging purposes
*
* @param none
* @return String
*/
string getQuest(){
}
/**
* Sets this.treeNode's question to the parameter string.
*
* @param String
* @return void
*/
public void setQuest(String q)
{
}
/**
* Sets this.treeNode's left child to the parameter.
*
* @param treeNode
* @return treeNode
*/
treeNode setLeft( treeNode leftVal )
{
}
/**
* Returns this.treeNode's right child.
*
* @param none
* @return treeNode
*/
public treeNode getRight( )
{
}
/**
* Sets this.treeNode's right child to parameter.
*
* @param treeNode
* @return treeNode
*/
treeNode setRight( treeNode rightVal )
{
}
/**
* Returns this.treeNode's anima, if no animal is present returns to spaces.
*
* @param none
* @return String
*/
string getAnimal( )//returns ansAnimal
{
}
string setAnimal(string ani)// non static modifyer method
{
}
private:
treeNode left; // Reference to the left child
treeNode right; // Reference to the right child
boolean end;// variable that denotes if node is leaf
string quest;// question set by setQuest
string animal;// animal set by setAnimal method auto sets quest var
string ans;// temp var for answer
} // interface treeNode
wien
November 29th, 2004, 03:11 PM
Heh.. Complete with JavaDoc and everything. :)
First off. When posting code in the forums, use the code tags. The code is close to impossible to read now.
Secondly. This code doesn't even begin to compile. I don't even know were to begin commenting on it. :confused: It looks very much like you have simply moved your code over from Java, and made changes from there. That is not the way to go... really...
I suggest you find some nice C++ tutorials on the web, and start out programming a few "Hello World"ish programs to get a feel for how C++ works, and work your way up from there. Since you say you have written a lot of code in Java, you shouldn't have too much trouble getting the hang of it fairly quickly.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.