GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jul-2008, 22:57
kaizen03 kaizen03 is offline
New Member
 
Join Date: Jul 2008
Posts: 1
kaizen03 is on a distinguished road

Regarding virtual function ...getting an error


CPP / C++ / C Code:
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;
class eventhandler
{
public:
	virtual void execute()=0; 
};
class handlerlist
{
public:
	virtual void add(eventhandler* h)=0;
	virtual void dispatch()=0;
};
class events 
{
public:
	virtual string getname()=0;
	//static const string name;
	handlerlist* list;
	
};


class event_add : public events
{
public:
	event_add(){};
	event_add(handlerlist* l)
	{
		list=l;
	}
	string getname()
	{
		return "+";
	}

	//static const string name;
};
class event_mul : public events
{
public:
	event_mul(){};
	event_mul(handlerlist* l)
	{
		list=l;
	}
	string getname()
	{
		return "*";
	}

	//static const string name;
};
class event_subtract : public events
{
public:
	event_subtract(){}
	event_subtract(handlerlist* l)
	{
		list=l;
	}
	string getname()
	{
		return "-";
	}
	//static const string name;
};


class eventhandler_add_list : public handlerlist{

protected :
	vector<eventhandler*> handlers;
public :
	void add(eventhandler* c) 
	{ 
		handlers.push_back(c); 
	}
	void dispatch() 
	{
		vector<eventhandler*>::iterator it;
		it = handlers.begin();
		while(it != handlers.end())  
		(*it++)->execute();
	}
};
class add_handler1 : public eventhandler
{
	public:
		void execute ()
		{
			int x,y;
			cout<<"enter 2 integers to be added seperated by spaces: ";
			cin>>x;
			cin>>y;
			cout<<x<<"+"<<y<<"="<<x+y<<endl;
		}
};
class add_handler2 : public eventhandler
{
public:
	void execute ()
	{
		int x,y,z;
		cout<<"enter 3 integers to be added seperated by spaces: ";
		cin>>x;
		cin>>y;
		cin>>z;
		cout<<x<<"+"<<y<<"+"<<z<<"="<<x+y+z<<endl;
	}
};
class add_handler3 : public eventhandler
{
	public:
		void execute ()
		{
			int x;
			cout<<"enter one integers   ";
			cin>>x;
			
			cout<<x<<"*"<<x<<"="<<x*x<<endl;
		}
};


class eventhandler_mul_list : public handlerlist
{
protected :
	vector<eventhandler*> handlers;
public :
	void mul(eventhandler* c) 
	{ 
		handlers.push_back(c); 
	}
	void dispatch() 
	{
		vector<eventhandler*>::iterator it;
		it = handlers.begin();
		while(it != handlers.end())  
		(*it++)->execute();
	}
};
class mul_handler1 : public eventhandler
{
	public:
		void execute ()
		{
			int x;
			cout<<"enter one integers   ";
			cin>>x;
			
			cout<<x<<"*"<<x<<"="<<x*x<<endl;
		}
};

class eventhandler_subtract_list : public handlerlist

{
      protected :
	vector<eventhandler*> handlers;
public :
	void add(eventhandler* c) 
	{ 
		handlers.push_back(c); 
	}
	void dispatch() 
	{
		vector<eventhandler*>::iterator it;
		it = handlers.begin();
		while(it != handlers.end())  
		(*it++)->execute();
	}
};
class subtract_handler1 : public eventhandler
{
	public:
		void execute ()
		{
			int x,y;
			cout<<"enter 2 integers to be subtracted : ";
			cin>>x;
			cin>>y;
			cout<<x<<"-"<<y<<"="<<x-y<<endl;
		}
};
class subtract_handler2 : public eventhandler
{
public:
	void execute ()
	{
		float x,y;
		cout<<"enter 2 floating point numbers to be subtracted: ";
		cin>>x;
		cin>>y;
		cout<<x<<"-"<<y<<"="<<x-y<<endl;
	}
};
 /*const string event_add::name = "+";
 const string event_subtract::name = "-";*/

int main()
{
	eventhandler_add_list a;
	eventhandler_subtract_list s;
    eventhandler_mul_list m;
	mul_handler1 m1;
    add_handler1 a1;
	add_handler2 a2;
	add_handler3 a3;
	subtract_handler1 s1;
	subtract_handler2 s2;

	event_add add(&a);
	event_subtract subtract(&s);event_mul mul(&m);
	vector<events*> e;
	e.push_back(&add);
	e.push_back(&subtract);
	e.push_back(&mul);
    add.list->add(&a1);
	add.list->add(&a2);
	add.list->add(&a3);
	subtract.list->add(&s1);
	subtract.list->add(&s2);
	mul.list->add(&m1);
/*	eventhandler_pow_list m;
	mul_handler1 m1;
	event_mul pow(&m);
	e.push_back(&pow);
	pow.list->pow(&m1);
	*/
	while(1)
	{
		string s;
		cout<<"enter the event name (+ or - or *) or q to quit:";
		cin>>s;
		if(s=="q")
			exit(0);
		for(int i=0; i<e.size() ; i++)
		{
			if(s==e[i]->getname())
				e[i]->list->dispatch();
		}
		
	}
		

}

am getting error in int main()
itz giving like :cannot declare variable `m' to be of type `eventhandler_mul_list'

because the following virtual functions are abstract:

14 C:\Dev-Cpp\assign1.cpp virtual void handlerlist::add(eventhandler*)

i know we cant create objects of a class with virtual function is meaning less but ,a not able to find the prblm in here...

can any body please help me over this????
Last edited by LuciWiz : 03-Jul-2008 at 23:58. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 04-Jul-2008, 00:02
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Regarding virtual function ...getting an error


The class eventhandler_mul_list inherits from eventhandler, which has a pure virtual function declared.

All children MUST define that function, otherwise they will inherit it as pure virtual as well, and will not be able to be instantiated (and will pass it to their children).

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 04-Jul-2008, 01:42
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Regarding virtual function ...getting an error


Quote:
Originally Posted by LuciWiz
All children MUST define that function, otherwise they will inherit it as pure virtual as well, and will not be able to be instantiated (and will pass it to their children).
Clarification: no class within an inheritance hierarchy will be able to be instantiated until all pure virtual member functions have been resolved.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
Linked Lists advice request promsan C Programming Language 74 23-May-2007 09:29
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 12:25
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 11:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 21:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.