By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

Class Design Question

P: n/a
Hi all,

I'm designing a few classes and basically I have a base class which is
very abstract - it has only one method which has to be implemented in
all others. My question is how to implement it? If I try implementing
it as
class ClassName
{
public:
virtual int GetSomeInternalThing();
};

would that work? Is it okay to leave it without a ctor and a dtor?

Thanks

Dec 25 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 25 Dec 2005 11:58:20 -0800, "gamehack" <ga******@gmail.com> wrote:
Hi all,

I'm designing a few classes and basically I have a base class which is
very abstract - it has only one method which has to be implemented in
all others. My question is how to implement it? If I try implementing
it as
class ClassName
{
public:
virtual int GetSomeInternalThing();
};

would that work? Is it okay to leave it without a ctor and a dtor?

Thanks


You have what looks like an abstract class, in which case the
function(s) meant to be overridden by derived classes need to be
declared pure virtual in the base class.

If you leave out the default constructor and the destructor, the
compiler will generate these (as well as an assignment operator) for
you. Whether or not these are appropriate depend on how you plan to
use the classes. Since it looks like your base class has no data
members, i.e. doesn't manage any resources, this should be OK except
possibly for the virtual destructor which you would need to provide.
For example, if you ever need to delete an instance of a derived class
allocated dynamically through a pointer to the base class, you must
provide a virtual destructor for the base class; otherwise, the
deletion causes undefined behavior.

That would mean something like this:

struct ClassName
{
virtual ~ClassName() {}
virtual int GetSomeInternalThing() const = 0;
};

If GetSomeInternalThing() doesn't change the state of any members in
the derived implementations, it should be declared const as I have
done above. But then ALL of the derived classes must declare it const
in order to override it.

--
Bob Hairgrove
No**********@Home.com
Dec 25 '05 #2

P: n/a
gamehack wrote:
Hi all,

I'm designing a few classes and basically I have a base class which is
very abstract - it has only one method which has to be implemented in
all others.
My question is how to implement it? If I try implementing
it as
class ClassName
{
public:
virtual int GetSomeInternalThing();
};

would that work? Is it okay to leave it without a ctor and a dtor?


This is not an abstract. A class with one or more pure virtual funcions
is called an abstract class, and no objects of abstract class can be
created. An abstract class can be
used only as an interface and as a base class for other classes.

Make it like:

class ClassName
{
public:
virtual int getSomeIntegralThing() = 0;
};

class OneHundred : public ClassName
{
public:
virtual int getSomeIntegralThing() { static int i = 100; return i++; }
};

class SuperEnumerator : public ClassName
{
int start_;
public:
SuperEnumerator(int start) : start_(start) { }

virtual int getSomeIntegralThing() { return start_++; }
};

int main()
{
OneHundred oh;
SuperEnumerator se(-20);
int id1, id2;

id1 = oh.getSomeIntegralThing();
id2 = se.getSomeIntegralThing();
}

etc.

If you do not explicitly define your own constructor, the compiler will
generate a default constructor for you. A default constructor is a
constructor that can be called without supplying an argument.

If you do not explicitly define your own destructor then the compiler
generates one which does nothing.

So it's ok to leave a class without a ctor and dtor.
P.Krumins

Dec 25 '05 #3

P: n/a
On 25 Dec 2005 12:37:34 -0800, "Peteris Krumins"
<pe*************@gmail.com> wrote:
So it's ok to leave a class without a ctor and dtor.


Unless, of course, an object of the derived type is deleted through a
pointer to the base class. In that case, you must provide the base
class with a virtual destructor; otherwise, the behavior is undefined.

--
Bob Hairgrove
No**********@Home.com
Dec 25 '05 #4

P: n/a
Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?

Thanks

Dec 25 '05 #5

P: n/a
On 25 Dec 2005 13:16:48 -0800, "gamehack" <ga******@gmail.com> wrote:
Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?


Have you read this?
http://www.parashift.com/c++-faq-lite/

--
Bob Hairgrove
No**********@Home.com
Dec 25 '05 #6

P: n/a
gamehack wrote:
Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?


http://parashift.com/c++-faq-lite/co...html#faq-18.10

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 25 '05 #7

P: n/a

Bob Hairgrove wrote:
On 25 Dec 2005 12:37:34 -0800, "Peteris Krumins"
<pe*************@gmail.com> wrote:
So it's ok to leave a class without a ctor and dtor.


Unless, of course, an object of the derived type is deleted through a
pointer to the base class. In that case, you must provide the base
class with a virtual destructor; otherwise, the behavior is undefined.


Thanks for noting that!
P.Krumins

Dec 25 '05 #8

P: n/a
Right :) After I googled a bit I found about pure virtual functions
which make the class abstract. That was exactly what I was looking
about. Thanks.

Dec 25 '05 #9

P: n/a

"gamehack" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?

Thanks


The const means that this method can't change anything in the class. the =
0 means that it's a pure virtual method, so derived classes must provide
their own or they won't compile.
Dec 26 '05 #10

P: n/a
Jim Langston wrote:
"gamehack" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks a lot guys :)
BTW I've always wondered
virtual int GetSomeInternalThing() const = 0;
what do the keywords after () mean and the assignment in the context?

Thanks

The const means that this method can't change anything in the class.


Not in the class, but the object.
Method declared as const has "read-only" access to the object itself,
because inside method declared as const "this" pointer is a const
pointer to const object (MyClass const* const).
Cheers
--
Mateusz oskot
http://mateusz.loskot.net
Dec 26 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.