Hi,
coming from Java, my experience with the Classes in C++ is quite limited.
Thats why I am having trouble converting the following (simple!) Java-Program.
Most examples with linked lists I found on the web describe how to implement the LinkedList class itself. But my problem is different: I want to use such a Class (I have a LinkedList class available on my system which is presumably OK).
I hope somebody can help me translate the Java-Code to C++-Code.
Cheers!
Code:
package main;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
BoxList<String> sl = new BoxList<String>(
new ArrayList<ABox<String>>());
sl.getList().add(
new BoxPair2<String>(new BoxPair2<String>(
new BoxBasic<String>("s1"),
new BoxBasic<String>("s2")), new BoxPair2<String>(
new BoxBasic<String>("s3"),
new BoxBasic<String>("s4"))));
System.out.println(sl.toString()); // (((s1,s2),(s3,s4)))
sl.getList().set(0, new BoxBasic<String>("s5"));
System.out.println(sl.toString()); // (s5)
}
}
Code:
package main;
public abstract class ABox<E> {
}
Code:
package main;
public class BoxBasic<E> extends ABox<E> {
private E s;
public BoxBasic(E s) {
this.s = s;
}
public E getS() {
return s;
}
public void setS(E s) {
this.s = s;
}
@Override
public String toString() {
return s.toString();
}
}
Code:
package main;
import java.util.List;
public class BoxList<E> extends ABox<E> {
private List<ABox<E>> list;
public BoxList(List<ABox<E>> list) {
this.list = list;
}
public List<ABox<E>> getList() {
return list;
}
public void setList(List<ABox<E>> list) {
this.list = list;
}
@Override
public String toString() {
String res = "";
for (ABox<E> a : list)
res += a.toString() + ",";
if (list.isEmpty())
return "(" + res + ")";
else
return "(" + res.substring(0, res.length() - 1) + ")";
}
}
Code:
package main;
public class BoxPair1<E> extends ABox<E> {
private ABox<E> S;
private int val;
public BoxPair1(ABox<E> S, int val) {
this.S = S;
this.val = val;
}
public ABox<E> getS() {
return S;
}
public int getVal() {
return val;
}
public void setS(ABox<E> s) {
S = s;
}
public void setVal(int val) {
this.val = val;
}
@Override
public String toString() {
return "(" + S.toString() + "," + val + ")";
}
}
Code:
package main;
public class BoxPair2<E> extends ABox<E> {
private ABox<E> S1;
private ABox<E> S2;
public BoxPair2(ABox<E> S1, ABox<E> S2) {
this.S1 = S1;
this.S2 = S2;
}
public ABox<E> getS1() {
return S1;
}
public ABox<E> getS2() {
return S2;
}
public void setS1(ABox<E> s1) {
S1 = s1;
}
public void setS2(ABox<E> s2) {
S2 = s2;
}
@Override
public String toString() {
return "(" + S1.toString() + "," + S2.toString() + ")";
}
}