Pages

100 Contemporary Living Room Designs

Sunday, September 16, 2012

C++ PROGRAM TO CREATE A CLASS OCTAL.

Write a C++ program to create a class called OCTAL which has the characteristics of an octal number. Implement the following operations by writing an appropriate constructor and an overloaded operator +.
i) OCTAL h = x; where x is an integer.
ii) int y = h + k; where h is an OCTAL object and k is an integer.



Display the OCTAL result by overloading the operator <<. Also display the values of h and y.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
class OCTAL
  {
    int oct[15],count;
    public:
       OCTAL(int x);
       int operator +(int);
       friend ostream &operator<< (ostream &,OCTAL &);

  };
OCTAL ::OCTAL(int x)
   {
     int i=0,rem,a[15];
     while(x!=0)
       {
     rem=x%8;
     x=x/8;
     a[i++]=rem;
       }
       count=i;
       int n=count-1;
    for(i=0;i<count;i++)
      {
        oct[i]=a[n];
        n--;
      }
   }

int OCTAL::operator+(int k)
   {
      int x=0,i;
      int j=count-1;
    for(i=0;i<count;i++)
      {
        x=x+oct[j]*pow(8,i);
        j--;
      }
      return(x+k);
   }

ostream & operator<<(ostream &print,OCTAL &o)
   {
    for(int i=0;i<o.count;i++)

        print<<o.oct[i];
      return(print);
    }
void main()
   {
     int x,k,y=0;
     clrscr();
     cout<<"\nEnter the integer value in decimal: ";
     cin>>x;
     OCTAL h=OCTAL(x);
     cout<<"The corresponding octal value "

         <<for ("<<x <<" )is: "<<h;
     cout<<"\n\nEnter the integer to be added to octal value:";
     cin>>k;
     y=x+k;
     cout<<"\n "<<h<<" (Octal)+"<<k<<" (Decimal)="

         <<y<<" (Decimal)";
     getch();
   }

OUTPUT



1 comment:

  1. Learning from knowledgeable instructors can make a significant difference. Their ability to explain complex topics and provide real-world examples is invaluable.click here for more information.

    ReplyDelete