在java中转换C ++代码 [英] Convert C++ code in java

查看:66
本文介绍了在java中转换C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有下一个代码,我想用Java编写它



I have the next code in C++ and I want to write it in Java

#include <iostream>
#include <cstring>
using namespace std;

class MyClass
{
private:
    char *firstName;
    char *secondName;
public:
    MyClass(const char *firstName, const char *secondName)
    {
        if (this->firstName != NULL)
            this->firstName = NULL;
        this->firstName = new char[strlen(firstName)+1];
        strcpy(this->firstName, firstName);

        if (this->secondName != NULL)
            this->secondName = NULL;
        this->secondName = new char[strlen(secondName)+1];
        strcpy(this->secondName, secondName);
    }
    ~MyClass()
    {
        if (firstName != NULL)
            delete firstName;
        firstName = NULL;

        if (secondName != NULL)
            delete secondName;
        secondName = NULL;
    }
    char *getFirstName()
    {
        return firstName;
    }
    char *getSecondName()
    {
        return secondName;
    }
    friend ostream& operator<< (ostream& out, MyClass &obj);
    friend istream& operator>> (istream& in, MyClass &obj);
};

ostream &operator<< (ostream& out, MyClass &obj)
{
    out << "\n First Name: " << obj.firstName << endl;
    out << "\n Second Name: " << obj.secondName << endl;

    return out;
}

istream& operator>> (istream& in, MyClass &obj)
{
    char aux[20];
    if (obj.firstName != NULL)
        delete[] obj.firstName;
    cout << "\n First Name: ";
    in >> aux;
    obj.firstName = new char[strlen(aux)+1];
    strcpy(obj.firstName, aux);

    if (obj.secondName != NULL)
        delete[] obj.secondName;
    cout << "\n Second Name: ";
    in >> aux;
    obj.secondName = new char[strlen(aux)+1];
    strcpy(obj.secondName, aux);

    return in;
}

int main()
{
    MyClass obj("Dragu", "Stelian");
    cout << "\n First Name: " << obj.getFirstName() << endl;
    cout << "\n Second Name: " << obj.getSecondName() << endl;
    cin >> obj;
    cout << obj;
    return 0;
}





我试图用Java编写上面的代码





I tried to write the code above in Java

package myclass;
import java.util.Scanner;

class FirstClass
{
    private char firstName[];
    private char secondName[];
    public FirstClass(char firstName[], char secondName[])
    {
        this.firstName = firstName;
        this.secondName = secondName;
    }

    public char getFirstName()
    {
        return firstName;
    }

    public char getSecondName()
    {
        return secondName;
    }
};

public class MyClass
{
    public static void main(String[] args)
    {
        FirstClass obj = new FirstClass("Dragu", "Stelian");
        Scanner input = new Scanner(System.in);
        System.out.print("Enter First Name: ");
        obj.firstName = input.nextLine();
        
        System.out.print("Enter Second Name: ");
        obj.secondName = input.nextLine();
        
        System.out.println("Name is: " +obj.firstName + " " +obj.secondName);
    }
    
}





我尝试过:



现在,Java中有一些方面。

1. Java中没有指针,所以我使用的是char变量。 />
2. Java中没有析构函数。

3. getFirstName(),getSecondName()函数返回或应返回char数组。

因此,当我尝试返回一个char数组时出现错误。

4.在C ++中从键盘输入数据并显示它们我正在使用重载流操作符。

在Java中没有运算符重载。

我认为如果我使用字符串数据会更容易。



请帮帮我!



What I have tried:

Now, there are some aspects in Java.
1. There are no pointers in Java, so I am using char variables.
2. There is no destructor in Java.
3. The getFirstName(), getSecondName() functions returns or should return an array of char.
So, I get an error when I try to return an array of char.
4. In C++ to enter data from the keyboard and display them I am using overloading the stream operators.
In Java there is no overloading of operators.
I think it's easier if I use string data.

Please help me!

推荐答案

首先,你应该写'明智' C ++ 代码。

First, you should write 'sensible' C++ code.
#include <iostream>
#include <string>
using namespace std;

class Person
{
  string firstname, secondname;
public:
  Person( const char * firstname, const char * secondname ) : firstname(firstname), secondname(secondname)
  {
  }
  string getFirstName() const { return firstname; }
  string getSecondName() const { return secondname; }

  friend istream& operator>> (istream& in, Person & p);
};

ostream& operator<< (ostream& out, const Person & p)
{
  out << "\n First Name: " << p.getFirstName() << "\n";
  out << "\n Second Name: " << p.getSecondName() << endl;
  return out;
}

istream& operator>> (istream& in, Person & p)
{
  in >> p.firstname >> p.secondname;
  return in;
}

int main()
{
  Person dragu("Dragu", "Stelian");
  cout << "\n First Name: " << dragu.getFirstName() << endl;
  cout << "\n Second Name: " << dragu.getSecondName() << endl;

  cout << "\nPlease enter person't first and second name: ";
  cin >> dragu;
  cout << dragu;
}





然后你应该考虑将 C ++ 代码移到 Java 被视为危害人类罪。



Then you should consider that moving C++ code to Java is regarded as a crime against humanity.


你从哪里得到那个(坏的)C ++代码,你甚至编译了两个版本?



更好的版本将使用 std :: string 而不是char数组。然后可以使用Java String 类将其转换为Java。因此,在Java类中将您的成员更改为该类型。



因为您要设置现有对象的字符串且成员是私有的,所以您还必须提供 setFirstName() setLastName()函数(在C ++和Java版本中)或使字符串成员公开(这将是坏的样式)。





在C ++中使用 std :: string version避免自己处理缓冲区并减少源代码:不需要分配和删除,默认创建的析构函数将调用字符串成员的析构函数,这样就不需要实现析构函数了。虽然有时可能有理由这样做,但通常没有必要使用字符串。保持简单。



使用自己的C ++内存分配时的一些注意事项:


  • 没有必要在构造函数中检查 NULL 指针。在设置之前,这些值是未定义的。

  • 无需检查 NULL 指针删除

  • 最后不需要将指针设置为 NULL 在析构函数中。

From where did you got that (bad) C++ code and did you even compiled both versions?

A much better version would use std::string instead of char arrays. Then it can be converted to Java using the Java String class. So change your members to that type in your Java class.

Because you are setting strings of existing objects and the members are private, you must also provide setFirstName() and setLastName() functions (in the C++ and Java version) or make the string members public (which would be bad style).


Using std::string in the C++ version avoids handling the buffers yourself and reduces the source code: No allocation and deleting is required and the default created destructor will call the destructors of the string members so that there is no need to implement a destructor. While there might be reasons to do such sometimes, it is usually not necessary with strings. Just keep it simple.

Some notes when using your own C++ memory allocation:

  • There is no need to check for NULL pointers in the constructor. The values are undefined there until set.
  • There is no need to check for NULL pointers with delete.
  • There is no need to set the pointers finally to NULL in the destructor.
// Allocated string versions
const char *getFirstName() const { return firstName; }
// std::string version
const char *getFirstName() const { return firstName.c_str(); }



Java是一种垃圾收集语言,因此不需要析构函数或释放对象。虽然可以创建自己的基于字符的对象来保存字符串,但它比使用C ++更无用(出于同样的原因而且在Java中相当罕见)。



流操作不能简单地转换为Java。但我认为你的解决方案适合我没有提供解决方案。



但你可以创建一个打印出值的函数。

[/ EDIT]


Java is a garbage collected language so that there is no need for a destructor or releasing of objects. While it would be possible to create your own char based objects to hold strings it is even more useless than with C++ (for the same reasons and it is rather uncommon with Java).

The stream operations can't be simply converted to Java. But I think your solution fits so that I did not provide a solution.

But you might create a function that prints the values out.
[/EDIT]


这篇关于在java中转换C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆