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

2's Complement of 2Byte Number

This program will work out the 2's Complement of a 2 Byte Number. As Microprocessor can perform 8 bits at a time in Accumulator, so we follow the foloowing Algo Fetch the 2 Byte Number in a Register Pair Bring Least Significant Byte of the Number from last Register into A (Accumulator) Make Complement of A and add 1 Store back the Updated LSByte into its source Register Bring MSByte of the Number from another Register into A Make Complement of A and add 1 if carry was genetrated at the time of LSByte opeartion Store back the Updated MSByte into its source Register Store the Result to a Memory location Suppose, Argument Number is stored in Location 1000H and 1001H Memory Location and store the value in 2000H and 2001H Memory Locations. Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 2A 00 10 START: LHLD 1000H ;Load the HL Register Pair with the content of Memory Location 1000H and 1001H 3053 7D MOV A, L ;Move the Content of L to A ( Accumulator ) 3054 2F

Flip-Flop Conversion

Flip-Flop is a memory element. You can say a basic element. It is a circuit that can hold a binary state (0 or 1 as low or high volts) until an input to change that state. We have many flip-flops as SR Flip-Flop (Set Reset) D Flip-Flop (Data) JK Flip-Flop T Flip-Flop (Toggle) Those Flip-Flops are available in market in IC Package Form. Now You have a circuit that uses 1 JK and 1 D Flip-Flop and 3 NOT Gates. So you have to buy three ICs- one for JK, one for D and other for NOT Gate. Ultimately you will see that a flip flop in IC of JK, a flip-flop in D IC and 3 NOT gates are not used. Now, if you convert a JK flip flop into D flip flop then you didn’t have to pay for the D flip flop IC. So, your circuit will be made by 2 ICs in place of 3 and in cheap cost. This article is for that reason. How the Flip-Flops are converted from one to other. Procedure: 1. Write Qt and Qt+1 Table with 4 combination of 0 and 1. 2. Write columns for inputs of destination Flop-Fl op. 3. Write the ce

How to set Set-Top Box to Resume automatically to last Channel after switch on?

How to set customised automatic Resume Channel to last viewed in Set-Top Box?  Introduction In the era of Digitally fast growing life WiFi enabled smart devices and also voice controlled smart devices are trying to be companion of every human being. But, all human do not change their habit at the same time. It may be cause of mental attachment or financial limits or unwillingness or though as unnecessary. What ever the reason may be, there exists the old technology, like IR(Infrared), RF(Radio Frequency), etc. So, this article for them who are using "Un-smart" TV like me in this time of Smart World. I was searching for a solution to set my Set-top Box of Videocon DTH (controlled by a RF Remote) to resume the last viewed channel when I power on my TV anytime. Suppose, I was watching NAT-Geo and switch off my (Un-smart) TV, even switch off the plug power at board. Now, after some time / on the next day, when I switch on my TV it will directly tune to the last viewed channel, i.