Skip to main content

IntArray Class Definition:: It can hold an array of Integer with Diffrent Size

A Class named 'intarray' which can store an integer array of different size.
This Class contains::
O Constructor with deafault argument value
At the time of Object creation if array size is missing then An array of size 0 will be created.
O Copy Constructor
This Constructor is used to create a new Object from an existing Object.
O Overloaded Operator =
This operator is overloaded to assign an intarray object into another object, simply as two integer variables can be made.
O Overloading Operator [ ]
As array element can be reffered by its position declaring in Subscript operator ( [ ] ), here every element of an object array can be reffered by [ ] operator.
O Overloading Operator >>
This is an input operator. Here we can take input to a object simply by this line cin >> a;. Here a is an intarray object.
O Overloading Operator <<
This is an output operator. Here we can get the output of an intarray object by this line cout << a;. Here a is an intarray object.
O Destructor
It will free-up the memory space allocated for an object.
#include<iostream.h>

class intarray{
  // Data Members
 int *a;     //Integer pointer to hold the address of integer array
 long int size;    //Array size
 public:
 //Member Functions
 intarray(int s=0) //Constructor with default value
 {
  size=s;
  if(s) a=new int[s]; else a=NULL;
 }

 intarray(intarray &t); // Declaration of Copy Constructor

 int& operator[](long int i) //[ ] operator overloading function definition
 {
  if(i>=0) return a[i]; else return a[size+1];
 }

 intarray operator = (intarray &t); // = operator overloding function declaration

 friend istream& operator >> ( istream &in, intarray &t);  //Opeartor overloading for input Array elements
 friend ostream& operator << ( ostream &out, intarray &t); //Operator overloading for Display Array Content

 ~intarray(void) //Destructor Definition
 {
  delete [] a;
 }
}

intarray::intarray(intarray &t) //Definition of Copy Constructor
{
 size=t.size;
 if(size)
 {
  a=new int[size];
  for(long i=0; i<size;i++)
 a[i]=t.a[i];
 }
 else
  a=NULL;
}

intarray intarray::operator = ( intarray &t)  //Definition of = Operator Overloading function
{
 if(size)
 delete [] a;

 size=t.size;
 if(size)
 {
  a=new int[size];
  for(long i=0; i<size;i++)
 a[i]=t.a[i];
 }
 else
  a=NULL;
 return *this;
}

istream& operator >> ( istream &in, intarray &t)  //Definition of >> Operator Overloading function
{
 for(long i=0; i<t.size;i++)
  in>>t.a[i];
 return in;
}

ostream& operator << ( ostream &out, intarray &t)  //Definition of << Operator Overloading function
{
 if(t.size)
 out<< t.a[0];
 for(long i=1;i<t.size;i++)
  out<<'|'<< t.a[i];
 out<< endl;
 return out;
}

void main()  //main() function starts from here
{
 intarray a(5);
 cout<<"Enter the array element::\n";
 cin>>a;
 cout<<"The Array contains:: \n"<< a;

 intarray b(a);
 cout<<"Here is the Copy of First Array::\n"<< b;

 cout<<"3rd element of the Array is:: "<< a[2];
 a[2]=7;
 cout<<"\nAfter modifying the 3rd element to 7::\n"<< a;
}

Comments

Popular posts from this blog

টুক করে বকখালি

বকখালি অবস্থান:  দেশ: ভারতবর্ষ রাজ্য: পশ্চিমবঙ্গ জেলা: দক্ষিণ ২৪ পরগনা কি কি দেখার ?                 বিশাল সমুদ্র..... আরে, Bay of Bengal.... পুরো দীঘার ছোট edition... বিস্তৃত সমুদ্র সৈকত..... বেশ ঘন ম্যানগ্রোভ অরণ্য... আর যেটা খুব ভালো লাগে সেটা হল.... এখানে দীঘার মত এত ভীরভাট্টা নেই.... বেশ নিরিবিলি..... যারা একাকী একটা দিন নিভৃতে কাটাতে চান তাঁদের জন্য ব্যাপক জায়গা। (বা দুজনে নিভৃতে কাটাতে চান....)                  একটা Crocodile Park আছে ওখানে.....পশ্চিমবঙ্গ বন দপ্তর দ্বারা নির্মিত..... Bus Stand এর কাছেই। ভালই সাজানো গোছানো.....২ টাকা করে Entry Fee.... ওর মধ্যে একটা পুকুর আছে, যার ঘাট বাঁধানো । কিছুক্ষণ ওখানে বসতেও খুব ভালই লাগে..... বেশ গাছের ছায়া.... মৃদু মন্দ শীতল হাওয়া..... উফ: প্রাণটা যেন জুড়িয়ে যায়..... যেন মনে হয় প্রকৃতির কোলে মাথা রেখে একটু শান্তিতে ঘুমিয়ে নিই.... একটা Tube Well ও আছে.....

Majay Byatha