I got the whole static binding and dynamic binding on runtime if a function is declared virtual in the base class, but if you do something like this...

class base
{
public:
	void print()
	{
		cout << "base" << endl;
	}
};

class derived : public base
{
public:
	void print()
	{
		cout << "derived" << endl;
	}
	
};

a derived obj print function still prints "derived" like you would assume. So even if the print function was declared virtual in the base class, it would not change anything in this scenario. So what exactly is up? Thanks

Recommended Answers

All 7 Replies

The derived object is not the main focus, its the base object. By the base object
being able to point to the derived object, it calls Derived::print function, if it in fact points to a derived object else it calls its own. Of course this assumes print is
a virtual function. So what exactly is the problem you are having?

Im just trying to understand the use of virtual functions in a real life situation.

Maybe some code would help :

struct Shape{
 virtual void draw()const{}
};
struct Rectangle : Shape{
 void draw()const{ rect.draw(); /*some low level command*/ }
};
struct Circle : Shape{
 void draw()const{ circle.draw(); /*some low level command*/ }
}
int main(){
 Shape allShapes[3] = { Shape(), Rectangle(), Circle() };
 for(int i = 0; i < 3; ++i){
  allShapes[i].draw(); //draw circle, rectangle or whatever it points to
 }
}

in real life. if you want to update some software. you must install some packages. that real software's base class and functions are installed in your system. when will you update the packages contains the derived class and functions. so that time the derived class have a same function and newly updated one. in your software use the base class object for that software. the packages are derived class so no possible to change the base class object. instead if we declare the base class functions are virtual. the software call the derived class (new package's function) function.

i think you understand what i say. if u not understand ask the question again.

They're useful when you want to treat a bunch of differently-typed but related objects as if they were all the same in some specific respect. For example, pretend you have two classes derived from a base class:

class Animal
{
public:
   virtual void Eat() = 0; // Pure virtual
};

class Dog : public Animal
{
public:
   void Eat(); { /* Do Dog-specific things */ }
};

class Cat : public Animal
{
public:
   void Eat(); { /* Do Cat-specific things */ }

Now you can store a bunch of Animals in a single container - say, an array of pets in a house, Animal* pets[NUM_PETS] . Instead of keeping separate lists for Cats and Dogs, we can keep pointers to them all in a single list. When we want to feed all of the animals, we can simply loop over pets and call Eat() on each Animal. The correct version of the function will be called for Dogs and Cats, even though we only have an array of Animals.

Maybe some code would help :

Shape allShapes[3] = { Shape(), Rectangle(), Circle() };

AFAIK polimorphism works only for pointers and reference, so this should be array of pointers.

using your own code a simple example below. Try changing print function to virtual in the base class and the output is different. YNote we are using pointers to the base class for both base and derived objects.

int main()
{
	base* ptr = new base();
	base* ptrDeriv = new derived();
	ptr->print();
	ptrDeriv->print();

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.