Can anyone help me understand function polymorphism ?

Recommended Answers

All 6 Replies

It's pretty simple, really, kinda sorta. You have a base class with a virtual function, and a derived class that redefines the virtual function. By creating a reference to an object of the base class and initializing it with an object of the derived class, the virtual function will call the derived class version instead of the base class version even though you're calling on behalf of a reference to the base class:

#include <iostream>

using namespace std;

class Base {
public:
  virtual void func() { cout<<"Base class"<<endl; }
};

class Derived: public Base {
public:
  void func() { cout<<"Derived class"<<endl; }
};

int main()
{
  Base *a = new Derived;

  a->func();
}

The reason this works is because the function is declared as virtual (ie. polymorhpic). The compiler "knows" to use the actual type of the object rather than the declared type. The declared (static) type of the a in the example is a pointer to Base, but the actual (dynamic) type of a is a pointer to Derived. Because f is virtual, the compiler makes the connection and calls Derived::func instead of Base::func. If you remove virtual then the compiler doesn't make the connection and calls Base::func:

#include <iostream>

using namespace std;

class Base {
public:
  void func() { cout<<"Base class"<<endl; }
};

class Derived: public Base {
public:
  void func() { cout<<"Derived class"<<endl; }
};

int main()
{
  Base *a = new Derived;

  a->func();
}

The code is identical except "virtual" has been removed.

That's all a there is to polymorphism. It's simply a fancy way of saying that instead of using functions of the declared type, the compiler is smart enough to use functions of the actual type when you tell it to.

hi all,
i have a doubt...please clarify
Instead of having a base class pointer and pointing it to derived class...can we not have a derived class pointer itself to call the virtual function..how are the two cases different and what is the benefit of one over the other?

Member Avatar for jencas

What if you want to hold the pointers in a container i.e. std::vector and call the virtual functions for all elements pointed to by each container member?

sorry, i don't have much idea about vectors and containers.. can you please elaborate on what you are trying to tell

Member Avatar for jencas

Suppose you have (for explanation unnecessary features stripped!):

class Animal
{
    virtual void output() = 0;
};

class Dog : public Animal
{
      virtual void output() {bark();}
}

class Bird : public Animal
{
      virtual void output() {tweet();}
}

A container can only hold objects of same type, so you have to use

std::vector<Animal *> animals;

You can't use

std::vector<Animal> animals;

because polymorphism needs pointers or references to work

Now you can do something like:

animals.push_back(new Dog);
animals.push_back(new Bird);
std::vector<Animal *>::iterator it = animals.begin();
for (; it != animals.end(); ++it)
{
      (*it)->output();
}

Function overloading is also called function polymorphism. Poly means 'any' and morph means 'form': a polymorphic function is many-formed.

Function polymorphism refers to the capability to 'overload' a function with more than one meaning. By changing the number or type of parameters, you can give two or more functions the same function name, and the right one will be called by matching the parameters used. This enables you to create a function that can average integers, doubles and other values without having to create individual names for each function.

Suppose you write a function the doubles whatever input you give it. You would like to be able to pass in an int, a long, a float or a double. Without function overloading, you would have to create four function names:

int DoubleInt(int);

long DoubleLong(long);

float DoubleFloat(float);

double DoubleDouble(double);

With function overloading, you make this declaration:

int Double(int);

long Double(long);

float Double(float);

double Double(double);

This is easier to read and to use. You do not have to worry about which one to call, you just pass in a variable and the right function is called automatically.

The following code demonstrates the use of function overloading:

// example of function polymorphism
#include <iostream.h>

int Double(int);
long Double(long);
float Double(float);
double Double(double);

int main()
{
	int myInt = 6500;
	long myLong = 65000;
	float myFloat = 6.5F;
	double myDouble = 6.5e20;

	int doubledInt;
	long doubledLong;
	float doubledFloat;
	double doubledDouble;

	cout << "myInt = " << myInt << endl;
	cout << "myLong = " << myLong << endl;
	cout << "myFloat = " << myFloat << endl;
	cout << "myDouble = " << myDouble << endl;

	doubledInt = Double(myInt);
	doubledLong = Double(myLong);
	doubledFloat = Double(myFloat);
	doubledDouble =  Double(myDouble);

	cout << "doubledInt = " << doubledInt << endl;
	cout << "doubledLong = " << doubledLong << endl;
	cout << "doubledFloat = " << doubledFloat << endl;
	cout << "doubledDouble = " << doubledDouble << endl;


	return 0;
}

int Double (int original)
{
	return 2 * original;
}

long Double (long original)
{
	return 2 * original;
}

float Double (float original)
{
	return 2 * original;
}

double Double (double original)
{
	return 2 * original;
}
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.