TUGAS PERTEMUAN 8
Tugas 8A: Buatlah ringkasan tentang Array of class dan Pointer of class
Tugas 8B: Buatlah analisis program kasus dengan Array of Class berikut ini:
#include <iostream.h>#include <stdlib.h>
#include <iomanip.h>
enum bool {false, true};
class Array
{ friend ostream &operator<( ostream &, const Array & );
friend istream &operator>( istream &, Array & );
public:
Array( int = 10 );
Array( const Array & );
~Array()
{ delete [] ptr; }
int getSize() const;
const Array &operator=( const Array & );
bool operator==( const Array & ) const;
bool operator!=( const Array &right ) const
{ return ! ( *this == right ); // invokes Array::operator==
}
int &operator[]( int );
const int &operator[]( int ) const;
private:
int size;
int *ptr;
};
Array::Array( int arraySize )
{
size = ( arraySize > 0 ? arraySize : 10 );
ptr = new int[ size ];
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0;
}
Array::Array( const Array &arrayToCopy ) : size( arrayToCopy.size )
{ ptr = new int[ size ];
for ( int i = 0; i < size; i++ )
ptr[ i ] = arrayToCopy.ptr[ i ];
}
int Array::getSize() const
{ return size;}
const Array &Array::operator=( const Array &right )
{ if ( &right != this )
{ if ( size != right.size )
{ delete [] ptr;
size = right.size;
ptr = new int[ size ];
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ];
}
return *this;
}
bool Array::operator==( const Array &right ) const
{ if ( size != right.size )
return false;
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;
return true;
}
int &Array::operator[]( int subscript )
{ if ( subscript < 0 || subscript >= size )
{ cout < "\nError: Subscript " < subscript
< " out of range" < endl;
exit( 1 );
}
return ptr[ subscript ];
}
const int &Array::operator[]( int subscript ) const
{ if ( subscript < 0 || subscript >= size ) {
cout < "\nError: Subscript " < subscript
< " out of range" < endl;
exit( 1 );
}
return ptr[ subscript ];
}
istream &operator>( istream &input, Array &a )
{ for ( int i = 0; i < a.size; i++ )
input > a.ptr[ i ];
return input;
}
ostream &operator<( ostream &output, const Array &a )
{ int i;
for ( i = 0; i < a.size; i++ ) {
output < setw( 12 ) < a.ptr[ i ];
if ( ( i + 1 ) % 4 == 0 )
output < endl;
}
if ( i % 4 != 0 )
output < endl;
return output;
}
void main()
{ Array integers1( 7 );
Array integers2;
cout < "Size of array integers1 is "
< integers1.getSize()
< "\nArray after initialization:\n" < integers1;
cout < "\nSize of array integers2 is "
< integers2.getSize()
< "\nArray after initialization:\n" < integers2;
cout < "\nInput 17 integers:\n";
cin > integers1 > integers2;
cout < "\nAfter input, the arrays contain:\n"
< "integers1:\n" < integers1
< "integers2:\n" < integers2;
cout < "\nEvaluating: integers1 != integers2\n";
if ( integers1 != integers2 )
cout < "integers1 and integers2 are not equal\n";
Array integers3( integers1 );
cout < "\nSize of array integers3 is "
< integers3.getSize()
< "\nArray after initialization:\n" < integers3;
cout < "\nAssigning integers2 to integers1:\n";
integers1 = integers2;
cout < "integers1:\n" < integers1
< "integers2:\n" < integers2;
cout < "\nEvaluating: integers1 == integers2\n";
if ( integers1 == integers2 )
cout < "integers1 and integers2 are equal\n";
cout < "\nintegers1[5] is " < integers1[ 5 ];
cout < "\n\nAssigning 1000 to integers1[5]\n";
integers1[ 5 ] = 1000;
cout < "integers1:\n" < integers1;
cout < "\nAttempt to assign 1000 to integers1[15]" < endl;
integers1[ 15 ] = 1000; // ERROR: out of range
}
Tugas 8C: Carilah output dari program Pointer to Object berikut ini:
#include<iostream.h>class Pointer
{ public:int data1, data2;
int value;
int id;
void foo1() {}
void foo2() {}
};
void SetValue(Pointer& object)
{int Pointer::*ip = &Pointer::value;
object.*ip = 3;
}
void SetId(int *member)
{*member = 7; }
void main()
{ int Pointer::*ip1 = &Pointer::data1;
int Pointer::*ip2 = &Pointer::data2;
Pointer obj1;
//access 2 public data members via reference
obj1.*ip1 = 5;
obj1.*ip2 = 10;
//access data member via SetValue() method
SetValue(obj1);
//create 2 pointers to object member
int *pointer1 = &obj1.value;
int *pointer2 = &obj1.id;
//pass the pointers to SetValue()
SetId(pointer1);
SetId(pointer2);
}