Some C++ buzzwords a programmer need to grasp really boggles my mind. I try to understand them but I strangely, get dim understanding of them (am I tired? Need a vacation?). Anyway please help me understand the following big words in C++

1. Virtual Functions
2. Polymorphism

Thanks alot

Virtual functions and polymorphism are closely related.

A virtual function means that a some class with a virtual function will be derived from, and that virtual function will be overridden by the derived class.

To declare a virtual function the syntax is :

class VirtualBase{
public :
virtual void draw(){ cout << "drawing in virtualBase\n"; }
};

Notice that the virtual function is inside of a class, thats because it only makes sense for it to be in a class.

Take a look at this example :

class Based{
public : 
virtual void speak(){ cout <<"based\n"; }
};
class Derived : public Based{
public:
void speak(){ cout<<"derived\n" }
};

Notice that Dervied inherits from the Based class, and overrides the speak function. Now here is where the polymorphism comes in. When you have objects from base or derived, a base class object pointer can
point to either the derived class or the base class, and when you call the function speak with the base class pointer, the compiler will call
the speak function of either the base class or the derived class, depending on which it points to. Take a look :

#include <iostream>

using namespace std;

class BaseClass{
public:
	virtual void speak(){
		cout<<"Base Class says : Rough...Rough\n";
	}
};

class DerivedClass : public BaseClass{
public:
	void speak(){
		cout<<"Derived Class says : Meow...Meow\n";
	}
};

//Remember that a BaseClass pointer object can point to 
//a BaseClass object, or a DerivedClass object.
//and using that pointer object to call a virtual function is what
//polymorphism is.
void talk(BaseClass* obj){
	obj->speak();
};
int main()
{	 
	DerivedClass der;
	BaseClass	 base;
	
	cout<<"Passing derived class ... ";
	talk(&der);
	cout<<"\n\nPassing base class ... ";
	talk(&base);
	cout<<"\n\n";

	return 0;

}

And as you see polymorphism is when you have a base class pointer object thats pointing to either a base class or a derived class object, and calling a specific function, that was labeled virtual in the base class.

Re-read it again.

Edited 6 Years Ago by firstPerson: n/a

Comments
droppin' some mad knowlege on a complex topic

Man, you qualify to teach young children rocket science ;)
Great, clear I love it! Anyone can illuminate anything first person didn't touch or add more
Thanks!

Here are other Buzzwords I met today
1. Difference between Template class and template functions. Me I don't see except one is class and other is function

2. How to best use try, throw and catch

BTW, I found the book I'm reading the Author used void main :)

Here are other Buzzwords I met today
1. Difference between Template class and template functions. Me I don't see except one is class and other is function

But a template class can have default arguments for a template
parameter.

2. How to best use try, throw and catch

Depends, if you even need it.

BTW, I found the book I'm reading the Author used void main :)

What book ?

This article has been dead for over six months. Start a new discussion instead.