How Do I: Convert to Java
:confused:
Quote:
Hi buddy, do you know how to convert this C++ code into Java? I'm still fairly new to Java, hope you can guide me
Quote:
Microsoft Visual C++ 2010
#include "stdafx.h"
#include<iostream>
using namespace std;
template<typename T>
class myShape{
protected:
T height, width;
};
template<typename T>
class CSquare : public myShape<T>
{
public:
CSquare(int height, int width){
this->height = height;
this->width = width;
}
T area(){
return this->height * this->width;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CSquare<int> *s = new CSquare<int>(3,4);
cout<<s->area();
}
:)-:
Quote:
...by the way, if you familiar with C++, please help me
here to fix the Turbo C++ 4.5 code. Thanks friends
Re: How Do I: Convert to Java
Start out by converting the program to English. What does it do? Break it down into small steps- as small as possible. Then convert that to a Java program.
Re: How Do I: Convert to Java
Quote:
okay, so it will be like this right?
Code :
class myShape<T>{
protected T height, width;
}
class CSquare<T> extends myShape<T>
{
public CSquare(T height, T width){
this.height = height;
this.width = width;
}
T area(){
return this.height * this.width; // error: operator * cannot be applied to T,T
}
}
public class main{
public static void main (String[] args) {
//...
}
}
Quote:
but there are still have an error there..
return this.height * this.width;
Quote:
i get this error message:
operator * cannot be applied to T,T
Re: How Do I: Convert to Java
Seems pretty reasonable to me. You aren't guaranteeing that height or width are Numbers, so you can't multiply them.
Re: How Do I: Convert to Java
Quote:
hmm... how? add more statement in the main method?
Code :
public class main{
public static void main (String[] args) {
CSquare<Integer> s = new CSquare<Integer>(5,5);
System.out.println (s.area());
}
}
Quote:
if so, I still get the same error...
Re: How Do I: Convert to Java
No, because your CSquare class still has no idea what that type T is going to be. What types of values do you want to store as height and width?
Re: How Do I: Convert to Java
Nevermind, looks like I'm just wasting my time anyway.
This thread has been cross posted here:
http://www.java-forums.org/new-java/51912-how-do-i-convert-java.html
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
Re: How Do I: Convert to Java
C++ templates are far more flexible and can do things like what you want to do because it will generate specialized classes. Java, on the other hand, will not generate any specialized classes. Rather, generics is implemented with a technique called type erasure and exist to improve type safety (since you dont have to make as many casts). What this means is that you cannot use operators like * on generic types in Java because of how generics are implemented under the hood.
C++ templates
Java generics
EDIT:
In the case of your code... Java has no clue what the type of a generic type is at runtime. So when you multiply two generic variables it would have been compiled to multiplying two instances of Object, if the compiler did not realize that would be an error. That is why your code wont compile.
Re: How Do I: Convert to Java
okay, i get it! thanks for your time guys.. i'm sorry if i'd vexatious you.. i'm really really sorry...
Problem Solved!