What you should know about C++

What you should know about C++


What you should know about C++
















Comparison to Java

C++                                                            

class IntStack {           
public:
IntStack(int max=100);
~IntStack();
void push(int);
int pop();
protected:
int * stack;
int top;
public:
int size();
};

JAVA

class IntStack {
public IntStack(int max) {
stack = new int[max];
top = -1;
}
protected void finalize() {
// nothing to do here
}
public void push(int k) {
stack[++top] = k;
}
// ...
protected int [] stack;
protected int top;
};


new and delete operators
• new operator similar to Java
• new and delete in C++ return explicit pointers.
• Java: int[] A = new int[3];
• C++: int * A = new int[3];
• C++: Node * n = new Node;
• In Java, there’s garbage collection. In C++, have to delete
explicitly:
• C++: delete [] A;
• C++: delete myStack



Classes – Example use
• Stored as a local variable:
{
IntStack S(10000);
S.push(10);
S.push(12);
} // ~InStack automatically called

• Stored on heap:
{
IntStack * S = new IntStack(10000);
S->push(10);
S->push(12);
delete S;
}.........
























What you should know about C++






0 commentaires: