GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / 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 09-May-2008, 16:09
hungle hungle is offline
New Member
 
Join Date: May 2008
Posts: 3
hungle is on a distinguished road

a problem with Abstract Class


Hi
I am building a paint for my homework and i have a problem with abstract class that i dont understand
The error "error C2259: 'CFigure' : cannot instantiate abstract class c:\documents and settings\hung\desktop\paint_h\paint_h\arrays.cpp"

here is the code:

CPP / C++ / C Code:
Figure.h

#ifndef __FIGURE_H__
#define __FIGURE_H__
#include <windows.h>

class CFigure
{
protected:
	int m_xStart;
	int m_yStart;
	int m_x;
	int m_y;
	HBRUSH Brush;
	HPEN pen;

public:
	CFigure();
	void Setpara(int xStart,int yStart,int x,int y);
	virtual void Draw(HDC hdc)=0;
	bool IsInside(int x,int y);
	//void Move(int xS, int yS, int x, int y);
};

#endif 



Figure.cpp

#include "Figure.h"
#include <windows.h>


CFigure::CFigure()
{
	Setpara(0,0,0,0);
}

void CFigure::Setpara(int xStart,int yStart,int x,int y)
{
	m_xStart=xStart;
	m_yStart=yStart;
	m_x=x;
	m_y=y;
}


bool CFigure::IsInside(int x,int y)
{
	if ( (x>m_xStart) && (x<m_x))
		if ( (y>m_yStart) && (y<m_y))
			return true;
	return false;
}

//void CFigure::Move(int x, int y)
//{
//	m_Xstart+=x;
//	m_Ystart+=y;
//	m_Xend+=x;
//	m_Yend+=y;
//}



Arrays.h

#ifndef __ARRAYS_H__
#define __ARRAYS_H__
#include <windows.h>
#include "Figure.h"
#include <iostream>
using std::ostream;
using std::istream;

class Array
{  
public:
	CFigure *FigArray[100];
	Array();
	~Array();
	int getSize() const;
	CFigure* operator=(const CFigure *);
	CFigure* &operator[]( int );
	void IncFig();
private:
   int size; 
   CFigure *ptr;
   
}; 

#endif



Arrays.cpp

#include "Figure.h"
#include <iostream>
#include "Arrays.h"


Array::Array()
{
	size =0;
	ptr=new CFigure[100];  // The Error point to here!!!
	for (int i=0;i<100;i++)
		FigArray[i]=NULL;
}

Array::~Array()
{
   delete [] ptr;
} 

int Array::getSize() const
{
   return size;
}

CFigure* &Array::operator[]( int subscript )
{
	return this->FigArray[subscript]; // reference return
}


CFigure* Array::operator=(const CFigure *right)
{
	CFigure *temp;
	return temp;
}

void Array::IncFig()
{
	size++;
}


Please help i have a date line for the project
  #2  
Old 09-May-2008, 16:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: a problem with Abstract Class


Quote:
Originally Posted by hungle
... i dont understand...
The error "error C2259: 'CFigure' : cannot instantiate abstract class c:\documents and settings\hung\desktop\paint_h\paint_h\arrays.cpp"

CPP / C++ / C Code:
class CFigure
{
.
.
.
	virtual void Draw(HDC hdc)=0;
.
.
.
};

The existence of a pure virtual function makes CFigure an abstract class.

You cannot create an object of an abstract class type.

So when you do the following:
CPP / C++ / C Code:
.
.
	ptr=new CFigure[100];  // The Error point to here!!!
.
.
.

you are attempting to create an array of 100 CFigure objects and the compiler is telling you that it's illegal.

Regards,

Dave
  #3  
Old 10-May-2008, 01:13
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road

Re: a problem with Abstract Class


Any class with pure virtual function is an abstract class.

You can derived a class from abstract class to extend its capabilities and create object from there.
__________________
Linux is the best OS in the world.
  #4  
Old 10-May-2008, 07:06
hungle hungle is offline
New Member
 
Join Date: May 2008
Posts: 3
hungle is on a distinguished road

Re: a problem with Abstract Class


Thanks alot

instead of that i made an array of pointers to the abstract class
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

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
Return pointer to template's nested class problem Kimmo CPP / C++ Forum 2 20-Sep-2007 23:39
A class problem, very confused!! allenfanwenyuan CPP / C++ Forum 2 16-Sep-2007 05:13
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
calling abstract base class method calls draw instead achoo FLTK Forum 1 19-Dec-2004 09:38

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

All times are GMT -6. The time now is 04:34.


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