The C++ class object, arrays, operator overloading, copy constructor and friends keyword program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Array type manipulation to demonstrate the C++ class object, operator overloading, copy constructor and the use of friend keyword in C++ programming
To show: How to create and use the C++ class object, operator overloading, copy constructor and friend keyword with array type in C++ programming
// C++ operators overloading, array, friend keyword and copy constructor code sample
#include <iostream>
using namespace std;
// C++ wrapper, the C header is <assert.h>
#include <cassert>
// a class declaration part
class TestArray
{
// these overloaded << and >> must be friend of TestArray class, so the overloaded << and >> of TestArray class are available
// for the ostream and istream classes. ostream and istream are C++ file I/O
//
// equal to operator(cout, object) function,
friend ostream &operator<<(ostream &,TestArray &);
// equal to operator(cin, object) function
friend istream &operator>>(istream &,TestArray &);
public:
// default constructor
TestArray(int = 4);
// copy constructor
TestArray(const TestArray &);
// destructor
~TestArray();
// return the size of the an array
int GetSize() const;
// overloaded the assignment operator
const TestArray &operator=(const TestArray &);
// overloaded the == operator
int operator==(const TestArray &) const;
// overloaded the != operator
int operator!=(const TestArray &) const;
// overloaded the [ ] operator
int &operator[ ](int);
private:
int *ptr;
int size;
};
// class implementation part
// default constructor for TestArray class
TestArray::TestArray(int ArraySize)
{
size = ArraySize;
ptr = new int[size];
assert(ptr != 0);
for(int i= 0;i<size;i++)
ptr[i] = 0;
}
// copy constructor for TestArray class
TestArray::TestArray(const TestArray &initial)
{
size = initial.size;
ptr = new int[size];
assert(ptr !=0);
for(int i = 0;i<size;i++)
ptr[i] = initial.ptr[i];
}
// destructor for TestArray class
TestArray::~TestArray()
{ delete[ ] ptr; }
// get the array's size
int TestArray::GetSize()const {return size;}
// overloaded the subscript/index operator
int &TestArray::operator[ ](int index)
{
assert((index >= 0) && (index < size));
return ptr[index];
}
// == comparison, return 1 if true, 0 if false
int TestArray::operator==(const TestArray &rightside) const
{
if (size != rightside.size)
return 0;
for(int i = 0; i< size; i++)
if(ptr[i] != rightside.ptr[i])
return 0;
return 1;
}
// != comparison, return 1 if true, 0 if false
int TestArray::operator!=(const TestArray &rightside) const
{
if (size != rightside.size)
return 1;
for(int i = 0; i<size;i++)
if (ptr[i] != rightside.ptr[i])
return 1;
return 0;
}
// overloaded assignment operator
const TestArray &TestArray::operator=(const TestArray &rightside)
{
if(&rightside != this)
{
delete [ ] ptr;
size = rightside.size;
ptr = new int[size];
assert(ptr !=0);
for(int i = 0; i< size; i++)
ptr[i] = rightside.ptr[i];
}
return *this;
}
// overloaded the >> operator
istream &operator>>(istream &input, TestArray &x)
{
for(int i = 0; i < x.size; i++)
input>>x.ptr[i];
return input;
}
// overloaded the << operator
ostream &operator<<(ostream &output, TestArray &x)
{
int i;
for(i = 0; i < x.size; i++)
{
output<<x.ptr[i]<<' ';
if((i+1)%10==0)
output<<endl;
}
if (i % 10 !=0)
output << endl;
return output;
}
// the main program
int main(void)
{
// One(3) same as One = 3 and Two will use the default constructor value, 4
TestArray One(3), Two;
cout<<"Array One's size is "<<One.GetSize()<<"\nWith initial data: "<<One;
cout<<"Array Two's size is "<<Two.GetSize()<<"\nWith initial data: "<<Two;
cout<<"\nNow, input 7 integers for the arrays:\n";
// read and store in arrays respectively
cin>>One>>Two;
cout<<"\nThen, the arrays content:\n"<<"One[ ]: "<<One<<"Two[ ]: "<<Two;
cout<<"\nNew array, copy of One's array:";
TestArray Three(One);
cout<<"\nThe new array, Three size is "<<Three.GetSize()<<"\nArray initial value: "<<Three;
cout<<"\nTesting: One == Three?\n";
if(One == Three)
cout<<"They are equal\n";
else
cout<<"They are not equal!";
cout<<"\nTesting: One != Two?\n";
if(One != Two)
cout<<"They are not equal\n\n";
cout<<"Assigning Two to One:\n";
One = Two;
cout<<"One: "<<One<<"Two: "<<Two;
cout<<"\nTesting: One == Two?\n";
if(One == Two)
cout<<"They are equal\n";
else
cout<<"They are not equal!\n";
return 0;
}
Output example:
Array One's size is 3
With initial data: 0 0 0
Array Two's size is 4
With initial data: 0 0 0 0
Now, input 7 integers for the arrays:
4 2 4 1 8 23 71
Then, the arrays content:
One[ ]: 4 2 4
Two[ ]: 1 8 23 71
New array, copy of One's array:
The new array, Three size is 3
Array initial value: 4 2 4
Testing: One == Three?
They are equal
Testing: One != Two?
They are not equal
Assigning Two to One:
One: 1 8 23 71
Two: 1 8 23 71
Testing: One == Two?
They are equal
Press any key to continue . . .