Thread: [B]Overloading Functions In Inheritance[/B]

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Question Overloading Functions In Inheritance

    Hi Folks,

    My understanding is that to overload functions across class, the base class's function must be declared in the derived class and then it would allow me to overload a function. But the below code is not compiling. Any help in this direction would be appreciated.

    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
          public:
                 void hello(int i);
    };
    
    class B : public A
    {
          public:
                 void A::hello(int);
                 void hello(char *ch);
          
                 
    };
    
    void A::hello(int i)
    {
         cout << "Value of int is i " << i << endl;
    }
    void B::hello(char *ch)
    {
         cout << "Value of char is " << *ch << endl;
    }
    int main()
    {
        B b1;
        b1.hello((int) 10);
        char ch = 'A';
        b1.hello((char*) &ch);   
        return 0;
    }
    Last edited by shiv_tech_quest; 12-13-2005 at 11:05 AM. Reason: Correcting Typo Error

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    Hi Guys,

    Recollected the concept. Here's the solution

    Instead of coding
    void A::hello(int) // in class B,
    I should have typed
    using A::hello;

    regards,
    Shiv
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Why do you cast the value of things when you are passing them to your functions this is not needed.
    Code:
    #include <iostream>
    
    class A
    {
        public:
            void hello(int i);
    };
    
    class B : public A
    {
        public:
            void hello(char *ch);
    };
    
    void B::hello(char *ch)
    {
        std::cout<<ch<<std::endl;
    }
    
    void A::hello(int i)
    {
        std::cout<<"I = "<<i<<std::endl;
    }
    
    int main()
    {
        B b1;
        char ch = 'h';
        
        b1.hello(10);
        b1.hello(&ch);
        
        std::cin.get();
        
        return 0;
    }
    Last edited by prog-bman; 12-13-2005 at 11:54 AM.
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with pointers to an abstract class and inheritance
    By bainevii in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2009, 07:51 AM
  2. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 08:47 AM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  4. Overloading functions
    By mrafcho001 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2006, 07:04 PM
  5. Inheritance and Overloading
    By XSquared in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2004, 07:40 PM