Using Classes and Inheritance
First time really using the OOP side of C++, working with classes.
Is the following code appropriate for the application in which I'm using it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Decimal to Hexadecimal and Octal converter //
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Converter {
protected:
int num;
int quotient;
int remain;
string result;
public:
void userInput(int);
};
class Hexadecimal: public Converter {
public:
string convert(int num) {
string hexArray[16];
hexArray[0] = "0";
hexArray[1] = "1";
hexArray[2] = "2";
hexArray[3] = "3";
hexArray[4] = "4";
hexArray[5] = "5";
hexArray[6] = "6";
hexArray[7] = "7";
hexArray[8] = "8";
hexArray[9] = "9";
hexArray[10] = "a";
hexArray[11] = "b";
hexArray[12] = "c";
hexArray[13] = "d";
hexArray[14] = "e";
hexArray[15] = "f";
quotient = (int)num/16; // 8
remain = num%16; // 4
result = hexArray[remain]; // 4
while (quotient != 0)
{
remain = quotient%16; // 7
quotient = (int)quotient/16; // 1
result = hexArray[remain]+result; // 11
}
return result;
}
};
class Octal: public Converter {
public:
string convert(int num) {
string octalArray[8];
octalArray[0] = '0';
octalArray[1] = '1';
octalArray[2] = '2';
octalArray[3] = '3';
octalArray[4] = '4';
octalArray[5] = '5';
octalArray[6] = '6';
octalArray[7] = '7';
// decimal = 23
quotient = num/8; // 2
remain = num%8; // 7
result = octalArray[remain]; // 7
while (quotient != 0)
{
remain = quotient%8; // 2
quotient = (int)quotient/8; // 0
result = octalArray[remain]+result; // ((string)2) + 7 = 27 (octal number for 23)
}
return result;
}
};
void Converter::userInput (int decimal) {
num = decimal;
}
int main()
{
char repeat = 'y';
char resp = 'c';
int decimal(0); // Declare decimal
Hexadecimal hex;
Octal oct;
cout << "Decimal to Hexadecimal and Octal converter" << endl << endl;
while (repeat == 'y') {
cout << "Please enter a decimal number: "; cin >> decimal;
cout << endl;
cout << "Would you like to convert " << decimal << " to hexadecimal or octal?" << endl;
cout << "a.) hexadecimal" << endl;
cout << "b.) octal" << endl;
cout << "Reponse: "; cin >> resp;
// Handle menu
if (resp == 'a')
cout << "0x" << hex.convert(decimal);
else if (resp == 'b')
cout << oct.convert(decimal);
else
cout << endl << "Please enter a or b" << endl;
cout << "\n\aWould you like to convert another decimal (y or n): ";
cin >> repeat;
cout << endl;
}
return 0;
}
|
Also, I see nobody else uses "#include "stdafx.h"" in their includes, but if I leave that out it does not work. Why?
Last edited on
stdafx.h is particular to MS Visual C++. It has to do with the use of precompiled headers.
I don't see you using Converter::UserInput.
Line 25 & 61, you're declaring an array of strings. You only need one string for each.
It also strikes me that you can move the conversion loop into the base class:
1 2 3 4 5 6 7 8 9 10
|
string Converter::ConvertNumber (int num, int base, const string & array)
{ quotient = num;
do
{ quotient = (int)quotient/base;
remain = quotient%base;
result += array[remain];
}
while (quotient != 0);
return result;
}
|
Octal::convert and Hex::convert become simpler:
1 2 3 4
|
string Octal::convert (int num)
{ string octalArray = "01234567";
return ConvertNumber (num, 8, octalArray);
}
|
Hi iam new to c++ i want to create a library which contain classes and i want dis classes to be efficient flexible please tell me which book is better
advance thanks
Last edited on
@maddy1445 - Please don't hijack someone else's thread.
Topic archived. No new replies allowed.