CrazyEngineers
  • A virtual function error

    Updated: Oct 26, 2024
    Views: 1.4K
    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;
    }  
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Rifaa

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • arunhero99

    MemberApr 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?
    Are you sure? This action cannot be undone.
    Cancel
  • vivek.m

    MemberApr 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;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • yam.rules

    MemberMay 14, 2009

    oh bahlle so got that working in ur op
    Are you sure? This action cannot be undone.
    Cancel
  • ahammedali

    MemberMay 14, 2009

    sorry man i dont know more about ubuntu
    😕
    Are you sure? This action cannot be undone.
    Cancel
  • ahammedali

    MemberMay 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>
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register