473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Abstract classes

Hello. I'm trying to make an abstract class, but when I declare one of
the derived classes, I get the error "cannot instantiate abstract
class". Can anyone tell me what I'm doing wrong?

ABSTRACT CLASS:

class CDCtrl
{

protected:
CDCtrl(CDCtrl &c);

public:
CDCtrl( void );
virtual ~CDCtrl( void );
/* Control Unique ID */
unsigned int UID;

/* LOCATION ON THE PALLATE */
double X;
double Y;
double CX;
double CY;

/* APPEARANCE */
unsigned int ForeColor;
unsigned int BackColor;
bool Visible;

void Opacity(double Opacity);
double Opacity(void);

virtual bool InRect(double x, double y) const = 0;
virtual bool OnLButtonDown(double x, double y) = 0;
virtual bool OnLButtonUp(double x, double y) = 0;
virtual bool OnRButtonDown(double x, double y) = 0;
virtual bool OnRButtonUp(double x, double y) = 0;

virtual bool OnMouseMove(double x, double y, bool button_flag)
= 0;

virtual bool OnKeyDown( unsigned int VKeyCode, unsigned int ModKeys )
= 0;

virtual void Draw( DPalette &refPalette );


protected:
double m_opacity;

}; /* Class CDCtrl */
/*
* DCtrl.c
* The Daedalus control base classes and templates.
*/

#include ".\DCtrl.h"

/*
* CONSTRUCTOR
*/
CDCtrl::CDCtrl( void ) :
X(0),
Y(0),
CX(0),
CY(0),
UID(0),
ForeColor(0x000000),
BackColor(0xFFFFFF),
Visible(true),
m_opacity(1)
{
} /* CDCtrl::CDCtrl */

/*
* DESTRUCTOR
*/
CDCtrl::~CDCtrl( void )
{
} /* CDCtrl::CDCtrl */

/*
* CDCtrl::Opacity
*/
void
CDCtrl::Opacity( double Opacity )
{
if ( Opacity < 0 )
{
m_opacity = 0;
}
else if ( Opacity 1 )
{
m_opacity = 1;
}
else
{
m_opacity = Opacity;
}
} /* CDCtrl::Opacity */
/*
* CDCtrl::Opacity
*/
double
CDCtrl::Opacity(void)
{
return m_opacity;
}
DERIVED CLASS:

class CDCtrl_Button : public CDCtrl
{
public:

CDCtrl_Button(void);
~CDCtrl_Button(void);

int Value() const { return m_value & 0x01; }
void Value(int value);

/*
virtual bool InRect(double x, double y);
virtual bool OnLButtonDown(double x, double y);
virtual bool OnLButtonUp(double x, double y);
virtual bool OnRButtonDown(double x, double y);
virtual bool OnRButtonUp(double x, double y);

virtual bool OnMouseMove(double x, double y, bool
button_flag);

virtual bool OnKeyDown( unsigned int VKeyCode, unsigned int
ModKeys );
*/

virtual void Draw( DPalette &refPalette );

private:

int m_value;
bool m_mouse_move;
};

/*
* CONSTRUCTOR
*/
CDCtrl_Button::CDCtrl_Button(void) :
CDCtrl(),
m_value(0),
m_mouse_move(0)
{
}

/*
* DESTRUCTOR
*/
CDCtrl_Button::~CDCtrl_Button(void)
{
}

/*
* CDCtrl_Button::draw
* Render the control on to the pallete.
*/
void
CDCtrl_Button::Draw( DPalette &refPalette )
{

refPalette.roundedRect( X, Y, CX, CY, 2, 2, 2, 2 );

}
FROM THE MAIN PROGRAM:

CDCtrl_Button m_button;
This is my first abstract class. Can anyone tell me what I'm doing
wrong?

Thanks in advance,
TBJ

Nov 9 '07 #1
6 1615
Where are:

virtual bool InRect(double x, double y); virtual bool
OnLButtonDown(double x, double y); virtual bool
OnLButtonUp(double x, double y); virtual bool
OnRButtonDown(double x, double y); virtual bool
OnRButtonUp(double x, double y);

Implemented? You've made them pure virtual in your parent class, but I
don't see the child class implementation of them. You can't instantiate
an object of a class that has any pure virtual methods (inherited or not).
Nov 9 '07 #2
On Nov 9, 9:48 am, Brian Szmyd <brian.sz...@gmail.comwrote:
Where are:

virtual bool InRect(double x, double y); virtual bool
OnLButtonDown(double x, double y); virtual bool
OnLButtonUp(double x, double y); virtual bool
OnRButtonDown(double x, double y); virtual bool
OnRButtonUp(double x, double y);

Implemented? You've made them pure virtual in your parent class, but I
don't see the child class implementation of them. You can't instantiate
an object of a class that has any pure virtual methods (inherited or not).
Ah! I thought I could pick and choose which virtual functions I wanted
to implement.

Thanks!
TBJ

Nov 9 '07 #3
looks like you didn't provide implementation for the pure virtual
functions "OnMouseMove" and "OnKeyDown", which make the derived class
still an abstract class, which of course cannot be instantiated.

Nov 9 '07 #4
TBass wrote:
On Nov 9, 9:48 am, Brian Szmyd <brian.sz...@gmail.comwrote:
>Where are:

virtual bool InRect(double x, double y); virtual bool
OnLButtonDown(double x, double y); virtual bool
OnLButtonUp(double x, double y); virtual bool
OnRButtonDown(double x, double y); virtual bool
OnRButtonUp(double x, double y);

Implemented? You've made them pure virtual in your parent class, but
I don't see the child class implementation of them. You can't
instantiate an object of a class that has any pure virtual methods
(inherited or not).

Ah! I thought I could pick and choose which virtual functions I wanted
to implement.
You can. The problem is that your class cannot control which functions
of it are going to be called. And calling a pure virtual function (if
you don't implement it, it stays pure, right?) has undefined behaivour.

If you want to be able to have an object that does nothing upos one of
those actions, don't make those functions pure. Provide the compiler
with an implementation in the base class, and let it return 'false' (or
whatever makes sense for "default behaviour -- nothing is done"). Then
you can pick which behaviour change from the default to custom.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 9 '07 #5
TBass <tb*@automateddesign.comwrote in
news:11**********************@i38g2000prf.googlegr oups.com:
On Nov 9, 9:48 am, Brian Szmyd <brian.sz...@gmail.comwrote:
>Where are:

virtual bool InRect(double x, double y); virtual bool
OnLButtonDown(double x, double y); virtual bool
OnLButtonUp(double x, double y); virtual bool
OnRButtonDown(double x, double y); virtual bool
OnRButtonUp(double x, double y);

Implemented? You've made them pure virtual in your parent class, but
I don't see the child class implementation of them. You can't
instantiate an object of a class that has any pure virtual methods
(inherited or not).

Ah! I thought I could pick and choose which virtual functions I wanted
to implement.

Thanks!
TBJ
No... Any method declared in a class must be implemented somewhere.
The 'where' is what changes between pure virtuals, virtuals, and non-
virtuals. With pure virtuals, the method is guaranteed to be
implemented in a derived class. With virtuals, an implementation must
be available in the base class, but it can be overridden in the derived
classes. And, finally with non-virtuals, the implementation must be in
the base class and can't be overridden by a child. (Note, it can be
hidden be a new implementation in the child, but it is a bad idea.)

The class declaration is your contract with your user as to what
services your object provides. Rarely is faulting out an acceptible
response to an attempt to use a service.

joe
Nov 9 '07 #6
[snip]
No... Any method declared in a class must be implemented somewhere.
The 'where' is what changes between pure virtuals, virtuals, and non-
virtuals. With pure virtuals, the method is guaranteed to be
implemented in a derived class. With virtuals, an implementation must
be available in the base class, but it can be overridden in the derived
classes. And, finally with non-virtuals, the implementation must be in
the base class and can't be overridden by a child. (Note, it can be
hidden be a new implementation in the child, but it is a bad idea.)
[/snip]

That makes sense. I've got the code up and running now. Thanks
everyone!

Until my next stupid question... :-)

Nov 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.