# C++ program to convert octal number to hexadecimal using class

Submitted by Shubh Pachori, on August 22, 2022

## Problem statement

Given an octal number, we have to convert it using the class and object approach.

Example:

```Input:
Enter Octal Number: 56

Output:
```

## C++ code to convert octal number to hexadecimal using the class and object approach

```#include <iostream>
using namespace std;

// create a class
class OctalToHexa {
// private data member
private:
int octal;

// public functions
public:
// getOctal() function to get
// the octal number
void getOctal() {
cout << "Enter Octal Number:";
cin >> octal;
}

// OToH() function to convert octal
string OToH() {

// int type variables for operations
int decimal = 0, remain;

// Initializing base value to 1
int base = 1;

// Initializing Temporary variable
// and assigning octal into it
int temp = octal;

// while loop to convert
// octal number to decimal
while (temp) {

// Extracting last digit
remain = temp % 10;

// dividing temp by 10 to discard
// the last digit
temp = temp / 10;

//  Multiplying remain with base
// value and adding it to decimal
decimal = decimal + remain * base;

// multiplying base by 8 at every iteration
base = base * 8;
}

// Initializing a char array hexa of length
// 100 to store hexadecimal value
char hexa[100];

//  counter for hexadecimal
//  number array
int index_1 = 0;

// while loop to convert decimal value
// to hexadecimal value
while (decimal != 0) {

//  Temporary variable to
//  store remainder
int temp = 0;

//  Storing remainder in
//  temp variable.
temp = decimal % 16;

//  Check if temp < 10
if (temp < 10) {

// assigning temp with addition of
// 48 in the hexa array
hexa[index_1] = temp + 48;

// increment of 1 in the index_1
index_1++;
}

// else it comes in else part
else {

// assigning temp with addition of
// 87 in the hexa array
hexa[index_1] = temp + 87;

// increment of 1 in the index_1
index_1++;
}

// dividing decimal by 16
decimal = decimal / 16;
}

// string type variable to store the
// reverse of the hexa array
string hexadecimal = "";

//  Printing hexadecimal number array
//  in reverse order
for (int index_2 = index_1 - 1; index_2 >= 0; index_2--) {

// copying reverse of hexa in hexadecimal
}

}
};

int main() {
// create an object
OctalToHexa O;

// string type variable to store
string hex;

// function is called by the object to
// input octal number
O.getOctal();

// OToH() function to convert octal
hex = O.OToH();

cout << "Hexadecimal:" << hex;

return 0;
}
```

### Output

```Enter Octal Number:3344
```

### Explanation

In the above code, we have created a class OctalToHexa, one int type data member octal to store the octal number, and public member functions getOctal() and OToH() to store and convert the given octal number to hexadecimal.

In the main() function, we are creating an object O of class OctalToHexa,  reading the octal number given by the user using getOctal() function, and finally calling the OToH() member function to convert the given octal number. The OToH() function contains the logic to convert the given octal number to hexadecimal number and printing the result.