A virtual function error

yamrajbhalla

yamrajbhalla

@yamrajbhalla-ZPvBoL • Oct 26, 2024

i am working in ubuntu
so please help so that it works in ubuntu

/*Write a program having a base class- num. Have this class hold  an integer value  and contain a virtual function shownum(). Create two  derived classes- displayhex and displayoct that inherit class num. Have  the derived classes override  shownum() so that to display  the value in heaxdecimal  and octal respectively.*/


#include<iostream>
 
using namespace std;

class num
{
 protected:
  int i;
 public:
  num(int n)
 {i=n;}

 virtual void shownum()
  {
    cout<<endl<<"the value of i is "<<i;
  }  

};


class hex:public num
{
  public:
  
  hex(int n):num(n)
  {
      }
 
  void shownum()
{
  cout<<endl<<"the value of int i in hex is "<<i;
}

};


int main()
{ int i;
  cout<<endl<<"enter the value of integer u wana to change to hex decimal";
  cin>>i;
  hex h1(i);
  num *h;
  h=&h1;
  h->shownum();

 return 0;
 
  delete h;
}  

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Rifaa

    Rifaa

    @rifaa-NaWZZ4 Apr 27, 2009

    I don't think any one uses ubuntu here dude. I did once just to check it out. Sorry I could not tell u more yet.

  • arunhero99

    arunhero99

    @arunhero99-LRZv4M Apr 28, 2009

    I got a Red Hat Enterprise Linux. i didn't actually understand what you expect by making it work in ubuntu. Is it working in all other platforms? will making it compile in linux do?

  • vivek.m

    vivek.m

    @vivekm-aDMB2K Apr 28, 2009

    Hi,

    Please paste the errors you get in ubuntu in the following code

    #include <iostream>
    using std::cout;
    using std::endl;
    using std::ios;
    
    class num
    {
    protected:
        int i;
    public:
        num(int n)
        {
            i=n;
        }
    
        virtual void shownum()
        {
            cout<<"the value of i is "<<i;
        }  
    };
    
    
    class hex:public num
    {
    public:
        hex(int n):num(n)
        {
        }
        void shownum()
        {
            cout.setf ( ios::hex, ios::basefield );
            cout.setf(ios::uppercase);
            cout<<"the value of int i in hex is 0x"<<i<<endl;
            cout.unsetf(ios::uppercase);
            cout.unsetf(ios::basefield);
        }
    };
    
    class oct:public num
    {
    public:
        oct(int n):num(n)
        {
        }
        void shownum()
        {
            cout.setf ( ios::oct, ios::basefield );
            cout<<"the value of int i in oct is o"<<i<<endl;
            cout.unsetf(ios::basefield);
        }
    };
    
    int main()
    { 
        num* objHex = new hex(10);
        num* objOct = new oct(20);
    
        objHex->shownum();
        objOct->shownum();
    
        delete objOct;
        delete objHex;
        return 0;
    }
    
  • yam.rules

    yam.rules

    @yamrules-GtZ3mR May 14, 2009

    oh bahlle so got that working in ur op

  • ahammedali

    ahammedali

    @ahammedali-ufkcPK May 14, 2009

    sorry man i dont know more about ubuntu
    😕

  • ahammedali

    ahammedali

    @ahammedali-ufkcPK May 14, 2009

    hellow im ahammedali from kerala. im new to crazy engineers.com .so will u plz tell me how to add a query or doubts in this forum? also reply for how to add my photo. plz reply to my e-id <removed by admin>