1 Replies - 252 Views - Last Post: 30 April 2010 - 07:22 PM Rate Topic: -----

#1 Guest_Carl Smith*


Reputation:

Base class number that holds a single integer value and contains one m

Posted 30 April 2010 - 07:06 PM

A base class number that holds a single integer value and contains one member function, print_it. Define three derived classes to print the value in hex, octal, and decimal..


#include<iostream>
#include<conio.h>
#include<cstdlib>
using namespace std;

class stack
{
public:
    stack () {stackSize = 0;};
    ~stack() {}
    void push(char i)
    {
        stackChar[stackSize++] = i;
    }    
    char pop()
    {
        return stackChar[--stackSize];
    }
    bool isEmpty ()
    {
        if (stackSize == 0)
            return true;
        else
            return false;
    }
private:
    char stackChar[100];
    int stackSize;
};
void binary(int number) 
{
    int remainder;
 
    if(number <= 1) 
    {
        cout << number;
        return;
    }
 
    remainder = number%2;
    binary(number >> 1);    
    cout << remainder;
}
 
void hexadecimal(int number)
{
 int remainder;
    while (number >= 1)
    {
        remainder = number%16;
        if (remainder > 9)
        {
            if (remainder == 10)
                cout << "A";
            if (remainder == 11)
                cout << "B";
            if (remainder == 12)
                cout << "C";
            if (remainder == 13)
                cout << "D";
            if (remainder == 14)
                cout << "E";
            if (remainder == 15)
                cout << "F";
        }
        else
        {
            cout << remainder;
        }
        number = number/16;
    }
};
void octal(int number)
{
    int remainder;
    int i;
    int ten = 10;
    remainder = number % 8;
    number = number/8;
    i = remainder;
    while (number >= 1)
    {
        remainder = number%8;
        number = number/8;
        i += remainder * ten;
        ten = ten * 10;
    }
    cout << i << "\n";
};
int main ()
{
    int number;
    cout << "Enter a number in decimal: ";
    cin >> number;
    int spec;
    cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
    cin >> spec;
    if (spec == 1)
    {
        binary (number);
        cout << "\n";
    }
    if (spec == 2)
        octal (number);
    if (spec == 3)
        hexadecimal (number);
    return 0;
}
 



How do I take this code and covert it to the needed specs required! Thanks for any help!

Is This A Good Question/Topic? 0

Replies To: Base class number that holds a single integer value and contains one m

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Base class number that holds a single integer value and contains one m

Posted 30 April 2010 - 07:22 PM

Duplicate of this topic.

Closed. Don't open duplicate topics, and don't expect us to do your homework. Do you think we're fools? A stack class has nothing whatsoever to do with your assignment.

#include <iostream>
#include <string>
class DoMyHomework
{
public:

    DoMyHomework(std::string className) : className_(className) {}
    friend std::ostream& operator<<(std::ostream &os, DoMyHomework &dmh);

private:
    DoMyHomework() {}
    std::string className_;
};

std::ostream&
operator<<(std::ostream &os, DoMyHomework &dmh)
{
    os << "Please do my " << dmh.className_ << " homework!!!" << std::endl;
}

int main()
{
    DoMyHomework myHomework("Computer Science");
    std::cout << myHomework;

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1