1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Exact use and need of Virtual functions in c++

Discussion in 'C++' started by santhoshks, Jun 4, 2012.

  1. santhoshks

    santhoshks New Member

    Joined:
    Jun 2, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Hi friends,
    Hope everyone gud. I am new to c++. I have some confusion in Virtual Function and Abstract Class Concepts. What is the exact use of those concepts.? can anyone please help me to get rid of that..
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I'm not really a fan of c++ and don't know much about the advanced topics, but I'll try to help if I can. A virtual function is one that is declared in your base class, but its definition can be overridden during runtime by a derived class.

    I suspect that one advantage would be you could manipulate a single data set differently through these functions in varying derived classes. Seems like overkill to me though. :D

    I'd encourage you to mill over tech docs from major companies like ibm, etc to see how they use them.

    Code:
    class base_class {
        public : base_class(int new_value) { myvar = new_value; }
                   virtual void show_value() const = 0; // pure virtual function
                   virtual int getData() const { return myvar; }
                   virtual ~base_class() {} // destructor
        private : int myvar;
    };
    
    class derived_class : public base_class {
        public : derived_class(int newval) : base_class(newval) {}
                   void show_value() const {
                       std::cout << "the value is: " << (getData()) << "\n";
                   }
                   virtual ~derived_class() {} // destructor
    };
    
    int main() {
        base_class *test = new derived_class(100);
        test->show_value();
    
        delete test;
        test = NULL;
    }
    hope that helps
     
  3. santhoshks

    santhoshks New Member

    Joined:
    Jun 2, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Thanks for your help hobbyist..!!
     
  4. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    NP; always glad if/when I can help. :)
     
  5. sumtaru

    sumtaru New Member

    Joined:
    May 17, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    1) you cannot create objects of abstract class.It consist of pure virtual function.
    2) virtual function means you have same signature of a function in base and derived classes.Depends on the content of pointer appropriate function is called.
     
  6. santhoshks

    santhoshks New Member

    Joined:
    Jun 2, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Ya you are correct sumtaru... :) Depends on the content of pointer appropriate function is called....
     

Share This Page