int i = 10;
int * p = & i; // & gives address of object i
int & j = i; i = 50; // changes i directly *p = 60; // changes i indirectly j = 70; // changes i indirectly
int k = 20; p = & k; // p now points to k *p = 60; // changes k indirectly
j = k; // i now gets value of k
p = 0;
*p = 100; // will cause run-time error
int a[4]; int b[2]; int * p = a; *p = 10; p[0] = 20; p = b; *p = 30; p[0] = 40;
p[1] = 70; *(p+1) = 70; // does the same thing
char s1[] = "Hello"; char * s2 = "Hello"; // not the same thing as s1 char * s3 = s1; s3[0] = 'M'; // changes s1 to "Mello"
int main()
{
int size = getFromUser( "How Many?" );
int * a = new int[size];
for ( int i = 0; i < size; i++ )
a[i] = getFromUser( "Next Integer" );
for ( int i = 0; i < size; i++ )
cout << a[i] << endl;
delete[] a;
return 0;
}
class Vector
{
private:
int maxLength;
int * buf;
public:
Vector( int newLength )
: maxLength( newLength ), buf( new int[newLength] )
{
}
~Vector()
{
delete[] buf;
}
};
int main()
{
Vector v(10); // v gets constructed here
return 0; // v gets destructed here
}
class Vector
{
public:
Vector( Vector & v )
: maxLength( v.maxLength ), buf( new int[v.maxLength] )
{
for ( int i = 0; i < v.maxLength; i++ )
buf[i] = v.buf[i];
}
};
class Vector
{
private:
int maxLength;
int * buf;
bool inBounds( int i )
{
return i >= 0 && i < maxLength;
}
public:
int & operator [] ( int index )
{
// assert( inBounds( index ) );
return buf[index]; /// returns a reference to buf[index]
}
};
int main()
{
Vector v(10);
v[0] = 1; /// used as L-value
for ( int i = 1; i < v.length(); i++ )
v[i] = v[i-1] * 2;
cout << v[9] << endl; /// used as R-value
return 0;
}
class Vector
{
private:
int maxLength;
int * buf;
void resizeTo( int newSize )
{
delete[] buf;
buf = new int[newSize];
maxLength = newSize;
}
public:
Vector & operator = ( Vector & v )
{
if ( maxLength != v.maxLength )
resizeTo( v.maxLength );
for ( int i = 0; i < maxLength; i++ )
buf[i] = v.buf[i];
return *this;
}
};
class Vector
{
private:
int maxLength;
int * buf;
public:
bool operator == ( Vector & v )
{
if ( maxLength != v.maxLength )
return false;
for ( int i = 0; i < maxLength; i++ )
if ( buf[i] != v.buf[i] )
return false;
return true;
}
};
class IntList
{
private:
int maxLength; // the capacity of this IntList
int curLength; // the actual number of elements in this IntList
int * buf; // base of the array of integers in this IntList
const int DEFAULT_SIZE = 10;
bool indexInBounds( int i )
{
return i >= 0 && i < curLength;
}
void increaseArrayTo( int newSize )
{
int * oldBuf = buf;
buf = new int[newSize];
copy( buf, oldBuf, curLength );
maxLength = newSize;
/// curLength stays the same
delete[] oldBuf;
}
static void copy( int * to, int * from, int n )
{
for ( int i = 0; i < n; i++ )
to[i] = from[i];
}
int main()
{
IntList myInts( 10 );
for ( int i = 0; i < 7; i++ )
myInts.append( i + 1 );
for ( int i = 1; i < myInts.length(); i++ )
myInts[i] *= myInts[i-1];
IntList yourInts = myInts;
cout << "My ints are " << myInts << endl;
cout << "Your ints are " << yourInts << endl;
if ( myInts == yourInts )
cout << "and they're equal\n";
IntList otherInts;
if ( otherInts.length() == 0 )
cout << "otherInts is empty\n";
otherInts = yourInts;
cout << "Other ints are " << otherInts << endl;
otherInts = otherInts.reverse();
cout << "Other ints are " << otherInts << endl;
otherInts.remove( 720 );
cout << "Other ints are " << otherInts << endl;
if ( myInts != yourInts )
cout << "and they're not equal\n";
return 0;
}
My ints are 1 2 6 24 120 720 5040 Your ints are 1 2 6 24 120 720 5040 and they're equal otherInts is empty Other ints are 1 2 6 24 120 720 5040 Other ints are 5040 720 120 24 6 2 1 Other ints are 5040 1 120 24 6 2