Saturday 11 January 2014

A program in C++ to create a Class Octal to perform decimal to octal conversion and addition of an octal and an interger by overloading ‘+’ and ‘=’.

#include<iostream.h>
#include<conio.h>
class octal
{private:int ov;
 public:octal()
            {ov=0;}
            void getdata();
            void octcon(int);
            octal operator=(int a);
            int operator+(int a);
            void showdata(int iv,int x)
            {cout<<"\n the decimal number:";
             cout<<iv;
             cout<<"\n the sum:"<<ov;
             getch();
            }
            void showdata(int iv)
            {cout<<"\n the octal number:";
             cout<<ov;
             cout<<"\n the decimal number:";
             cout<<iv;
             getch();
            }
}n1,n2;
void octal :: getdata()
{int x=0,i=0,temp,c=0;
 cout<<"\n for addition \n";
 while(x!=1)
 {temp=0;i=0;c=0;
  cout<<"\n enter octal number to be added:";
  cin>>ov;
  temp=ov;
  while(i==0)
  {if((temp%10)>7)
   {c++;}
   temp=temp/10;
   if(temp==0)
   {i=1;}
  }
  if(c!=0)
  {cout<<"\n invalid value enter again \n";
  }
  if(c==0)
  {x=1;}
 }
}
int octal :: operator+(int v)
{int c=0,l,k=1,temp;
 temp=ov;
 while(temp!=0)
 {l=temp%10;
  l=l*k;
  c=c+l;
  temp=temp/10;
  k=k*8;
 }
 temp=c+v;
 return temp;
}
octal octal:: operator=(int iv)
{int r=0,i=0,j,l,k,temp;
 temp=iv;
 while(temp!=0)
 {k=1;
  for(j=0;j<i;j++)
  {k=k*10;}
  l=temp%8;
  temp=temp/8;
  r=k*l+r;
  i++;
 }
 ov=r;
}
void octal :: octcon(int iv)
{int r=0,i=0,j,l,k,temp;
 temp=iv;
 while(temp!=0)
 {k=1;
  for(j=0;j<i;j++)
  {k=k*10;}
  l=temp%8;
  temp=temp/8;
  r=k*l+r;
  i++;
 }
 ov=r;
}
void main()
{int c=0,k=0,v;
 clrscr();
 while(k==0)
 {clrscr();
  cout<<"\n enter the choice of operation to be performed \n1-conversion of decimal to octal \n2-addition of an octal and an integer \n3-exit\n \n enter choice:";
  cin>>c;
  switch(c)
  {case 1:cout<<"\n enter the integer value to be converted:";
              cin>>v;
              n1.octcon(v);
              n1.showdata(v);
              break;
   case 2:n1.getdata();
              cout<<"\n enter the integer value to be added:";
              cin>>v;
              n2=n1+v;
              n2.showdata(v,0);
              break;
   case 3:k=1;
              cout<<"\n thanks for using the program";
              break;
   default:cout<<"\n you have entered wrong choice try again";
               getch();
  }
 }
 getch();

}

No comments:

Post a Comment