










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











class IntStack { public : IntStack( int max=100); ~IntStack(); void push( int ); int pop(); protected : int * stack; int top; public : int size(); }; Constructor has same name as class. Called when object is created. Destructor called ~ClassName; Called when object is deleted. Functions declared inside class are called member functions. They can access the data in the class. protected things can only be seen by subclasses; public things can be seen by everyone; private things can only be seen by this class.
class IntStack { public : IntStack( int max=100); ~IntStack(); void push( int ); int pop(); protected : int * stack; int top; public : int size(); }; 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; }; C++ Java
IntStack::InStack( int max=100) { stack = new int [max]; top = -1; } IntStack::~IntStack() { delete [] stack; } void IntStack::push( int k) { top++; stack[top] = k } Syntax “new TYPE[SIZE]” creates a new array of length SIZE containing objects of type TYPE. Member functions can access class variables without any special syntax. “delete [] X” frees the memory for the array pointed to by X. To free a single object, omit the “[]”.
IntStack S(10000); S.push(10); S.push(12); } // ~InStack automatically called { IntStack * S = new IntStack(10000); S->push(10); S->push(12); delete S; }
#include
int add1(int * x) { (x) += 1; } / C-style / int add1(int & x) { x += 1; } // C++-style int foo( const Image & pict) {/...*/}
struct Point { int x, y; Point( int xx, int yy) { x=xx; y=yy; } }; bool operator ==( const Point & A, const Point & B) { return A.x == B.x && A.y == B.y; } int main() { Point p1(10, 4); Point p2(-12, -100); Point p3(10, 4); if (p1 == p2) { /* FALSE / } if (p1 == p3) { / TRUE */ } }
- Doesn’t support^ int^ foo(a,b)^ int^ a,^ int^ b { /* ... */ }
- Function arguments can have default values: int foo( int a=0) { /* ... */ }.