classes: virtual functions from baseclass

V

verbatime

Please explain me how this works - or should work:

Got my two classes - bcBasic (baseclass) and the derived cBasic.

//---------------------------------------
class bcBasic
{
int number;
virtual long myfunc(void);
}
//---------------------------------------
class cBasic : public bcBasic
{
int something;
};
//---------------------------------------

I want to specify the code for the "myfunc" (in the baseclass) in my code
for cBasic ? how on earth do i do that ?

I could do this:

//---------------------------------------
class cBasic : public bcBasic
{
int something;
long myfunc(void);
};
//---------------------------------------

And write the code - but is that the right way to do it ? should i somehow
be able to access cBasic:.bcBasic functions without defining the header for
the function once again inside the derived class ?

Or am i missing something completely fundemental about classes / C++ :(
 
V

Victor Bazarov

verbatime said:
Please explain me how this works - or should work:

Got my two classes - bcBasic (baseclass) and the derived cBasic.

//---------------------------------------
class bcBasic
{
int number;

public: // probably
virtual long myfunc(void);
}

; // definitely
//---------------------------------------
class cBasic : public bcBasic
{
int something;
};
//---------------------------------------

I want to specify the code for the "myfunc" (in the baseclass) in my code
for cBasic ?

what on earth does that mean?
how on earth do i do that ?

I could do this:

//---------------------------------------
class cBasic : public bcBasic
{
int something;
long myfunc(void);

That declares the function 'myfunc' that _overrides_ the same function in
the base class. In most cases that's how polymorphic behaviour is
implemented.
};
//---------------------------------------

And write the code - but is that the right way to do it ?

To do what? If your derived class does NOT override the base class' member
function, when you call that function for an object of the derived class,
the base class' member function will be called.
should i somehow
be able to access cBasic:.bcBasic functions without defining the header for
the function once again inside the derived class ?

I am not sure I understand what you are asking here. If the base class has
a member function, the derived class _inherits_ it (unless it's declared
private, like in your case, but I think it was just a typo).
Or am i missing something completely fundemental about classes / C++ :(

Probably. Sounds like it. What C++ book are you using to learn? Read
the chapter on derived classes and on member access specifiers.

V
 
R

raghavendra

I want to specify the code for the "myfunc" (in the baseclass) in my code
for cBasic ? how on earth do i do that ?
What u have written is not very clear to me. DO u want to have a different
implementation for myFunc in derived class??......Then no need of virtual.
Just use it.
Or do u want to access the implementation of derived's myfunc from base
class pointer?? ....In that case u need virtual in base class.
Or do u simply wanna acess the myfunc in base class from derived
class.....Then u simply call it on the derived object..It'll be inherited.

regards,
Raghavendra Mahuli
 
S

SaltPeter

verbatime said:
Please explain me how this works - or should work:

Got my two classes - bcBasic (baseclass) and the derived cBasic.

//---------------------------------------
class bcBasic
{
int number;
virtual long myfunc(void);
}
//---------------------------------------
class cBasic : public bcBasic
{
int something;
};
//---------------------------------------

I want to specify the code for the "myfunc" (in the baseclass) in my code
for cBasic ? how on earth do i do that ?

I could do this:

//---------------------------------------
class cBasic : public bcBasic
{
int something;
long myfunc(void);
};
//---------------------------------------

And write the code - but is that the right way to do it ? should i somehow
be able to access cBasic:.bcBasic functions without defining the header for
the function once again inside the derived class ?

Or am i missing something completely fundemental about classes / C++ :(

The virtual keyword is not needed for your requirements. The derived class
inherits the "behaviour" of its base class's protected or public member
functions.
So...

class BASE
{
private:
int number; // private int var
protected:
long myfunc(void); // inheritable behaviour
};

class DERIVED : public BASE // what's public stays public, etc
{
private:
int derived_number; // private to this derived class
public:
int something; // publicly accesible variable
};

.... a DERIVED class object can then be declared:

DERIVED derived_object;

.... and the inherited behaviour can be called since its a protected access
specifier:

derived_object.myfunc(void);

note that 'derived_object' can't access 'number' in BASE class because of
the private: access specifier. However, 'myfunc(void)' can access 'number'.
Not specifying an access specifier in a class makes all its attributes and
behaviours private (unlike a structure which defaults to public).

The point is that if you need myfunc(void) to act on derived_number instead
of number, declare myfunc(void) in derived class instead.

class DERIVED : public BASE // what's public stays public, etc
{
private:
int derived_number; // private to this derived class
public:
int something; // publicly accesible variable
protected:
long myfunc(void); // inheritable behaviour
};

This is an overly simplified view on access specifiers and class
inheritance. Its usually a good idea to include constructors and destructors
in your class declarations and definitions. Use meaningfull names. Don't
concern yourself with virtual and pure virtual member functions right now.
 
J

John Harrison

verbatime said:
Please explain me how this works - or should work:

Got my two classes - bcBasic (baseclass) and the derived cBasic.

//---------------------------------------
class bcBasic
{
int number;
virtual long myfunc(void);
}
//---------------------------------------
class cBasic : public bcBasic
{
int something;
};
//---------------------------------------

I want to specify the code for the "myfunc" (in the baseclass) in my code
for cBasic ? how on earth do i do that ?

I could do this:

//---------------------------------------
class cBasic : public bcBasic
{
int something;
long myfunc(void);
};
//---------------------------------------

And write the code - but is that the right way to do it ? should i somehow
be able to access cBasic:.bcBasic functions without defining the header for
the function once again inside the derived class ?

Yes you should.
Or am i missing something completely fundemental about classes / C++ :(

Its not at all clear what you are asking, but rest assured it is possible in
C++. That's the best I can do, please rephrase your question.

There are very simple guidelines, which will get you exactly the help you
want, but for some reason posters almost never follow them. Post a complete
compilable program, say what the output of that program is, and what you
expected it to be. You will then get help to fix the problem. If you can't
get something to compile then post all the code you are attempting to
compile, say what you expect the code to do, and what the compiler error
message is.

Always cut and paste code, don't attempt to type it in again, since you will
inevitably make mistakes, and we'll fix your typing mistakes not the real
problem.

Follow these rules and you'll get lots of useful help from this group, don't
follow them and you'll get less.

john
 
V

verbatime

Okay its also pretty hard to explain i think:

My architecture is a main application, that uses a DLL - that calls back to
the main application. The DLL supplies access to its class (bcQueue) - and
the mainapplication supplies (via passing a pointer to the DLL) access to
its bcGlobal class.

Thats the reason i have the virtual functions anyway, because they are
defined in the DLL, and not in the headerfile, since the code is to be
loaded in runtime, and the code can differ from DLL to DLL.

What i have is a baseclass defined in "plugin.h" - with pure virtual
functions - the reason why, is that "plugin.h" is included in both in the
main and in the dll (code should be defined in DLL - but not in the main):

// base class
class bcQueue
{
public:
int queuesize;
int nrOfClients;
char sfilename[200];
virtual bool AddIPentry (ipentry *entry) { }
virtual int getIPentryindex (void) {}
}

The function-code are NOT defined in this "plugin.h" - because the code
itselfs is in a DLL - the baseclass is only meant to be a skeleton, so the
both the main application & the DLL understand the structure.

So the main application - when looking at the bcQueue - can only see what
variables and functions it consist of - it doesnt know anything about the
code that resides inside the functions.

What i want (and trying to explain) is how i can define the code for this
structure - in plugin.cpp (which is the DLL) ?

My way was to make a new class, which looked exactly the same:

cQueue : public bcQueue

But that doesnt work, because then ex. the variables are the one from the
new class, and not the one's from the baseclass - that gives problems when
using pointers ofcourse.

So i think its very fundemental what im trying to ask - but is very very
hard to put it down with words ?

But if you get the idea that i have a DLL and the defintion placed
differently places, then it might lead you on track ?
 
J

John Harrison

Well here's my take on it, still not convinced I've understood you. My best
guess is that you are hung up on something which is a non-issue.

verbatime said:
Okay its also pretty hard to explain i think:

My architecture is a main application, that uses a DLL - that calls back to
the main application. The DLL supplies access to its class (bcQueue) - and
the mainapplication supplies (via passing a pointer to the DLL) access to
its bcGlobal class.

Thats the reason i have the virtual functions anyway, because they are
defined in the DLL, and not in the headerfile, since the code is to be
loaded in runtime, and the code can differ from DLL to DLL.

What i have is a baseclass defined in "plugin.h" - with pure virtual
functions - the reason why, is that "plugin.h" is included in both in the
main and in the dll (code should be defined in DLL - but not in the main):

// base class
class bcQueue
{
public:
int queuesize;
int nrOfClients;
char sfilename[200];
virtual bool AddIPentry (ipentry *entry) { }
virtual int getIPentryindex (void) {}
}

There are no pure virtual functions in this code, despite what you say.
The function-code are NOT defined in this "plugin.h" - because the code
itselfs is in a DLL - the baseclass is only meant to be a skeleton, so the
both the main application & the DLL understand the structure.

So the main application - when looking at the bcQueue - can only see what
variables and functions it consist of - it doesnt know anything about the
code that resides inside the functions.

What i want (and trying to explain) is how i can define the code for this
structure - in plugin.cpp (which is the DLL) ?

My way was to make a new class, which looked exactly the same:

cQueue : public bcQueue

But that doesnt work,

This is the problem, what do you mean by 'doesn't work', does it not
compile, does it not run correctly?
because then ex. the variables are the one from the
new class, and not the one's from the baseclass - that gives problems when
using pointers ofcourse.

So i think its very fundemental what im trying to ask - but is very very
hard to put it down with words ?

But if you get the idea that i have a DLL and the defintion placed
differently places, then it might lead you on track ?

As it happens all C++ programs have definitions placed in different places.
When you include a header file in two different source files you are getting
two (identical) definitions in two different places. As long as the
definitions are identical, C++ can cope. I haven't read anything in your
description that suggest you want the same class defined *differently* in
two different places. If that is what you do want then that's a whole
different ball game.

Seems like the following should work perfectly well.

class bcQueue
{
public:
int queuesize;
int nrOfClients;
char sfilename[200];
virtual bool AddIPentry (ipentry *entry) = 0;
virtual int getIPentryindex (void) = 0;
};

class cQueue : public bcQueue
{
public:
virtual bool AddIPentry (ipentry *entry)
{
// whatever
}
virtual int getIPentryindex (void)
{
// whatever
}
};

Have you tried that? You can access the member variables defined in bcQueue
from cQueue (especially since there are public). Seems perfectly good to me,
does everything you want as far as I can tell.

As I said before the correct way to post question like yours is to post
COMPLETE COMPILABLE CODE. Then there is no room for misunderstanding. You
post some code, say what it does and what you want it to do. Then I put it
in might compiler and tell you why not. Surely you can think of a couple of
toy classes with a single method that illustrates the difficulty you are
having. Put them in a complete program, post it, and you'll get your answer
in hours if not minutes.

john
 
J

John Harrison

cQueue : public bcQueue

But that doesnt work, because then ex. the variables are the one from the
new class, and not the one's from the baseclass - that gives problems when
using pointers ofcourse.

Maybe, just maybe I've understood you. Are you saying that you want to
access variables defined in cQueue, when all you have is a pointer to
bcQueue, and you don't even have the definition of cQueue?

If that is your question then the answer is that you can't.

You have to define your base class so that is not necessary. Every operation
that you need to do should be a pure virtual function in your base class,
that way you don't need to access variables in derived classes.

Something like this

class Base
{
public
int getVariable() const = 0;
void setVariable(int val) = 0;
};

class Derived : public Base
{
public
int getVariable() const { return variable; }
void setVariable(int val) { variable = val; }
private:
int variable;
};

See?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top