Subscribe to Invariance_Under_Translation

## Decimal to Binary Conversion utilizing OOP

Here is the same program as my last entry, only this time utilizing OOP. It is decently commented throughout, if you have any questions or critiques feel free to post or PM. Enjoy and happy coding!

```//Decimal to Binary Conversion
#include<iostream>

using namespace std;

#define MAX 8     //Use preprocessor instruction to define MAX

class IPConv
{					//Our private variables
int count;			//count is put in place for error checking
int decToBin[MAX];	        //Stores our preset values to compare input against
int binStor[MAX];	        //Stores our binary equivelant
int val1;			//variable to store user input
public:

IPConv(int);		//Constructor, takes user input as arguement
~IPConv();		//Deconstructor
void convertIP();	//method for converting decimal number to binary octet
void displayIP();	//method to display binary octet

};

IPConv::IPConv(int i)
{
cout<<"Inside Constructor"<<endl;    //Notice when you get this output? Why would that be?
cout<<endl;
count = 0;	//Initialize count to 0
val1 = i;	//set val1 equal to user input

}

IPConv::~IPConv()
{
cout<<endl;
cout<<"Destructing"<<endl; //Notice when you get this output?
cout<<endl;
}

void IPConv::convertIP()		   //method for converting decimal to binary
{
int decToBin[] = {128, 64, 32, 16, 8, 4, 2, 1};   //An array to hold our values we check user input against

if ( count < 4 )         //an IP address is made up of four octets, so a little error checking to
{		         //ensure we stay within that scope.
count++;	 //Increment count for first conversion
for (int b = 0; b < MAX; b++ )	 //Iterates through array decToBin, checking
{				 //user input against arrays values

if ( val1 >= decToBin[b] )	//Set as greater then or equal to, better accuracy
{
int temp = decToBin[b];   //temporary variable to store array value
val1 = val1 - temp;       //set val1 to subtract itself from temp if  greater then current array value.
binStor[b] = 1;	          //each time val1 is greater then array value, store a 1 in this array.
}
else
{
binStor[b] = 0;	          //If val1 is smaller then current array value, store a 0.
}

}
}
else
{
cout<<"Too many arguements accepted"<<endl;	//Part of error checking on four octets
}
}

void IPConv::displayIP()
{
cout<<"Converted IP: "<<endl;
for (int c = 0; c < MAX; c++ )		//Display the binary conversion
{
cout<<binStor[c];
}
cout<<endl;
}

int main()
{
int decNum;
int loopCount = 0;		//Also part of four octet error checking

cout<<"Welcome to the 'Internet Protocol Address'"<<endl;
cout<<"Decimal-to-Binary conversion program."<<endl;
cout<<endl;
cout<<"Enter the IP in standard four octet sets, "<<endl;
cout<<"including all three digits of each octet."<<endl;

while ( loopCount < 4 )
{
loopCount++;

cout<<endl;
cout<<"Enter your decimal octet: "<<endl;
cout<<endl;

cin>>decNum;		//Accept user input of decimal number
cout<<endl;

if ( decNum > 255 || decNum < 0 )	//Ensures user input is valid with IP address
{
cout<<"Please enter a proper IP octet (e.g. 0 - 255)"<<endl;
loopCount--;
}
else
{
IPConv obj(decNum);	//Initialize object of our class
obj.convertIP();	//Call class method to convert decimal to binary
obj.displayIP();	//call class method to display converted IP.
}

};

cin.get();
return 0;

}

```

### Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

S M T W T F S
1234567
891011121314
151617181920 21
22232425262728
293031

### 1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)