Pages

100 Contemporary Living Room Designs

Thursday, August 23, 2012

STACK IMPLEMENTATION USING OPERATOR OVERLOADING IN C++


Write a C++ program to create a class called STACK using an array of integers. Implement the following operations by overloading the operators + and – 
i) s1=s1+element; where s1 is an object of the class STACK and element is aninteger to be pushed on the top of the stack.
ii) s1=s1-; where s1 is an object of the class STACK - operator pops the element.
Handle the STACK empty and STACK full conditions. Also display the contents of the stack after each operation, by overloading the operator <<


#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
const int SIZE=5; //Stack size
//class declaration
class stack
{
   int items[SIZE];
   int top;
   int full();
   int empty();
   public:
   stack()
   {
      top=-1;
   }
   stack operator--(int);
   friend stack operator+(stack s1,int elem);
   friend ostream &operator<<(ostream &os,stack &s1);
};
// checking for Stack overflow
int stack::full()
{
   if(top==SIZE-1)
      return 1;
   else
      return 0;
}
//Checking for stack under flow.
int stack::empty()
{
   if(top==-1)
      return 1;
   else
      return 0;
}
//function for element deletion from the stack

stack stack::operator--(int )
{
   if(empty())
   {
     cout<<"Stack underflow\n";
   }
   else
   {
       cout<<"\nThe element deleted is :"<<items[top];
       stack t;
       t.top=--top;
       for(int i=0;i<=top;i++)
           t.items[i]=items[i];
   }
   return *this;
}
ostream &operator<<(ostream &os,stack &s1)
{
   for(int i=s1.top;i>=0;i--)
     os<<s1.items[i]<<"\n";
   return os;
}
//function for element insertion on to the stack
stack operator+(stack s1,int elem)
{
   if(s1.full())
     cout<<"\nStack overflow\n";
   else
     s1.items[++(s1.top)]=elem;
   return s1;
}
/*Main function*/
void main()
{
   stack s1;
   int choice,elem;
   clrscr();
   for(;;)
   {
     cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n"
     <<"enter your choice:";
     cin>>choice;
     switch(choice)
     {
       case 1:
           cout<<"Enter the element to be inserted:";
           cin>>elem;
           s1=s1+elem;
           break;
       case 2:
           s1=s1--;
           break;
       case 3:
           cout <<"The contents of the stack are :\n"<<s1;
           break;
       case 4: exit(0);
       default: cout <<"Invalid choice\n";
       getch();
       exit(0);
     }
   }
}

output 


Tuesday, August 21, 2012

OPERATOR OVERLOADING - ADDITION, SUBTRACTION OF MATRICES USING OPERATOR OVERLOADING


Write a C++ program to create a class called MATRIX using a two dimensional array of integers.Implement the following operations by overloading the operator == which checks the compatibility of the two matrices to be added and subtracted. Perform the addition and subtraction by overloading the operators + and - respectively. Display the results by overloading operator <<.



#include<iostream.h>
#include<conio.h>
class matrix
{
  private:long m[5][5];
  int row;int col;
  public:void getdata();
  int operator ==(matrix);
  matrix operator+(matrix);
  matrix operator-(matrix);
  friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
  if(row==cm.row && col==cm.col)
  {
    return 1;
  }
  return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
  cout<<"enter the number of rows\n";
  cin>>row;
  cout<<"enter the number of columns\n";
  cin>>col;
  cout<<"enter the elements of the matrix\n";
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
       cin>>m[i][j];
    }
  }
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
  matrix temp;
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
      temp.m[i][j]=m[i][j]+am.m[i][j];
    }
    temp.row=row;
    temp.col=col;
  }
  return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
  matrix temp;
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
      temp.m[i][j]=m[i][j]-sm.m[i][j];
    }
    temp.row=row;
    temp.col=col;
  }
  return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
  for(int i=0;i<d.col;i++)
  {
    for(int j=0;j<d.col;j++)
    {
      fout<<d.m[i][j];
      cout<<" ";
    }
    cout<<endl;
  }
return fout;
}
/* main function */
void main()
{
  matrix m1,m2,m3,m4;
  clrscr();
  m1.getdata();
  m2.getdata();
  if(m1==m2)
  {
    m3=m1+m2;
    m4=m1-m2;
    cout<<"Addition of matrices\n";
    cout<<"the result is\n";
    cout<<m3;
    cout<<"subtraction of matrices\n";
    cout<<"The result is \n";
    cout<<m4;
  }
  else
  {
    cout<<"order of the input matrices is not identical\n";
  }
  getch();
}

You might also like:

output


Friday, August 17, 2012

OPERATOR OVERLOADING - ADDING TWO 'TIME' OBJECTS


Create a class called 'time' that has three integer data members for hours, minutes and seconds, define a member function to read the values, member operator function to add time, member function to display time in HH:MM:SS format. Write a main function to create two time objects, use operator function to add them and display the results in HH:MM:SS format.


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
  int h,m,s;
  public:
  void read();
  void print();
  time operator+(time t2);
};
void time::read()
{
  cout<<"\nEnter hour,minutes and seconds\n";
  cin>>h>>m>>s;
}
void time::print()
{
  cout<<"\nTime is-> "<<setfill('0')<<setw(2)<<h;
  cout<<":"<<setfill('0')<<setw(2)<<m;
  cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
time time::operator+(time t2)
{
  time t;
  t.h=h+t2.h;
  t.m=m+t2.m;
  t.s=s+t2.s;
  return t;
}
void main()
{
  clrscr();
  time t1,t2,t3;
  t1.read();
  t1.print();
  t2.read();
  t2.print();
  t3=t1+t2;
  cout<<"\nTime1+ Time2:\n";
  t3.print();
  getch();
}

output




ADDING 'TIME' OBJECTS AND DISPLAYING THE RESULT IN HH:MM:SS FORMAT USING MEMBER FUNCTION


Create a class called 'time' that has three integer data members for hours, minutes and seconds, define a member function to read the values, member function to add two time objects, member function to display time in HH:MM:SS format. Write a main function to create two time objects, add them and display the results in HH:MM:SS format.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
  int h,m,s;
  public:
  void read();
  void print();
  void add(time t2);
};
void time::read()
{
  cout<<"\nEnter hour,minutes and seconds\n";
  cin>>h>>m>>s;
}
void time::print()
{
  cout<<"\nTime is-> "<<setfill('0')<<setw(2)<<h;
  cout<<":"<<setfill('0')<<setw(2)<<m;
  cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
void time::add(time t2)
{
  h=h+t2.h;
  m=m+t2.m;
  s=s+t2.s;
}
void main()
{
  clrscr();
  time t1,t2;
  t1.read();
  t1.print();
  t2.read();
  t2.print();
  t1.add(t2);  // member function call.
  cout<<"\nTime1+ Time2:\n";
  t1.print();
  getch();
}

output
                                                                                                                                                                                                           

Wednesday, August 15, 2012

FUNCTION OVERLOADING - Program to create a class called complex

Write a C++ program to create a class called COMPLEX and implement the following overloading functions ADD that return a COMPLEX number .
I. ADD(a,s2) - where a is an integer(real part) and s2 is a complex    number.
II. ADD(s1,s2) - where s1 and s2 are complex objects.


#include<iostream.h>
#include<conio.h>
class complex
{
  int r,i;
  public:
  void read();
  void print();
  friend complex add(int a,complex c);
  friend complex add(complex c1,complex c2);
};
void complex::read()
{
  cout<<"Enter real and imaginary\n";
  cin>>r>>i;
}
void complex::print()
{
  cout<<r<<"+i"<<i<<endl;
}
complex add(int a,complex c)
{
  complex t;
  t.r=a+c.r;
  t.i=c.i;
  return t;
}
complex add(complex c1,complex c2)
{
  complex t;
  t.r=c1.r+c2.r;
  t.i=c1.i+c2.i;
  return t;

}
void main()
{
   int a=2;
   clrscr();
   complex s1,s2,s3;
   s1.read();
   cout<<"\ns1 : ";
   s1.print();
   s2=add(a,s1);
   cout<<"s2 : 2+s1\n";
   cout<<"   : ";
   s2.print();
   s3=add(s1,s2);
   cout<<"s3=s1+s2\n";
   cout<<"s1 : ";
   s1.print();
   cout<<"s2 : ";
   s2.print();
   cout<<"s3 : ";
   s3.print();
   getch();
}

output



Monday, August 13, 2012

PROGRAM TO PRINT A DIAMOND IN A BOX - 2

C++ program to print a diamond inside a box using asterisks.

#include<iostream.h>
#include<conio.h>
void main()
{
  int i,j,x,n=1,t=1,l;
  clrscr();
  cout<<"Enter limit\n";
  cin>>l;
  x=l/2;
  for(i=0;i<l;i++)
  {
    for(j=0;j<l;j++)
    {
      if(i>0&&i<(l-1))
      {
        if(j>=x&&n>0)
        {
          cout<<"*";
          n-=1;
        }
        else if(i==0||i==(l-1)||j==0||j==(l-1))
        {
          cout<<"*";
        }
        else
        {
          cout<<" ";
        }
      }
      else
      {
        if(i==0||i==(l-1))
        {
          cout<<"*";
        }
      }
    }
    if(i>0&&i<(l/2))
    {
      t=t+2;
      n=t;
      x-=1;
    }
    else
    {
      t=t-2;
      n=t;
      x+=1;
    }
    cout<<endl;
  }
  getch();
}

output

    You may also like:
   C++ program to print a diamond in a box - 1   

PROGRAM TO PRINT A DIAMOND IN A BOX - 1

C++ program to print a diamond inside a box using asterisks.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
  int i=0,j=0,num;
  clrscr();
  cout<<"Enter limit\n";
  cin>>num;
  cout<<endl;
  for(i=-(num+2);i<=num+2;i++)
  {
    for(j=-(num+2);j<=num+2;j++)
    {
      if(abs(i)+abs(j)<=num||j==-(num+2)
      ||j==(num+2)||i==-(num+2)||i==(num+2))
      {
        cout<<"*";
      }
      else
      {
        cout<<" ";
      }
    }
    cout<<endl;
  }
  getch();
}

output

  You may also like:
   C++ program to print a diamond inside a box  using asterisks.
   















Sunday, August 12, 2012

PROGRAM TO READ THE NAME AND MARKS OF N STUDENTS AND FIND THE TOPPER

C++ program to accept the details of n students and find the class topper using friend function. Array of objects must be passed to the friend function.

#include<iostream.h>
#include<conio.h>
class student
{
  int total;
  char name[30];
  public:
  void read()
  {
    cout<<"Enter name\n";
    cin>>name;
    cout<<"Enter total mark\n";
    cin>>total;
  }
  friend void find(student *st,int n);
};
void find(student *st,int n)
{
  int i;
  student s;
  student *s1=new student;
  s1=st;
  s=s1[0];
  for(i=0;i<n;i++)
  {
    if(s1[i].total>s.total)
    {
      s=s1[i];
    }
  }
  cout<<"Topper details\n";
  cout<<"Name  : "<<s.name<<endl;
  cout<<"Marks : "<<s.total<<endl;
}
void main()
{
  int i,l;
  clrscr();
  student s1[10];
  cout<<"Enter the number of students\n";
  cin>>l;
  for(i=0;i<l;i++)
  {
    s1[i].read();
  }
  find(s1,l);
  getch();
}

output




FRIEND FUNCTION

Program to create a class car and a class truck, each containing , as private variable, the speed of the vehicle. Initialize the name and speed using constructors. Create one object each for class car and truck. Use a friend function to compare the speeds of the two objects.


#include<iostream.h>
#include<conio.h>
#include<string.h>
class truck;
class car
{
  int speed;
  char name[25];
  public:
  car(char x[],int y)
  {
    strcpy(name,x);
    speed=y;
  }
  friend void find(car c1,truck t1);
};
class truck
{
  int speed;
  char name[25];
  public:
  truck(char x[],int y)
  {
    strcpy(name,x);
    speed=y;
  }
  friend void find(car c1,truck t1);
};
void find(car c1,truck t1)
{
  if(c1.speed>t1.speed)
  {
    cout<<"\n"<<c1.name<<" is faster\n";
  }
  else
  {
    cout<<"\n"<<t1.name<<" is faster\n";
  }
}
void main()
{
  int i;
  clrscr();
  car c1("BMW",200);
  truck t1("TATA",185);
  find(c1,t1);
  getch();
}

output


Friday, August 10, 2012

MANIPULATOR FUNCTIONS - SETW() AND SETFILL()


C++ Program  to describe the working of setw() and setfill() functions.



#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
  clrscr();
  cout<<setw(5)<<5<<endl;
  cout<<setfill('0')<<setw(5)<<5<<endl;
  cout<<setw(5)<<setfill('0')<<5<<endl;
  cout<<setfill('0')<<setw(5)<<55<<endl;
  cout<<setfill('0')<<setw(5)<<555<<endl;
  getch();
}

output 

 

setw(length); - sets the width of the field assigned for the output.
setfill(char); - used to fill spaces when results have to be padded to the field width
Both functions are defined in the header iomanip.