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..
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. 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
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.