473,394 Members | 1,645 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,394 software developers and data experts.

classes: virtual functions from baseclass

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++ :(
Jul 22 '05 #1
7 1781
"verbatime" <no****@nospam.tk> wrote...
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
Jul 22 '05 #2

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
Jul 22 '05 #3

"verbatime" <no****@nospam.tk> wrote in message
news:40*********************@dread12.news.tele.dk. ..
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.
Jul 22 '05 #4

"verbatime" <no****@nospam.tk> wrote in message
news:40*********************@dread12.news.tele.dk. ..
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
Jul 22 '05 #5
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 ?

Jul 22 '05 #6
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" <no****@nospam.tk> wrote in message
news:40*********************@dread12.news.tele.dk. ..
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
Jul 22 '05 #7
>
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
Jul 22 '05 #8

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

Similar topics

3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
5
by: Peter Hagenaar | last post by:
Hello there. I have a question about a virtual template class. The class wich derives from this class overrides a virtual method in this template base class and calls the same method in this...
12
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
2
by: Peteroid | last post by:
I'm trying to create an abstract base class with a pure virtual method (using /clr and VS C++.NET 2005 Express). This will do the trick: ref class baseClass { protected: virtual void VMethod(...
4
by: anonymous.user0 | last post by:
Using the dotnet v1.1 framework (so no generics possible). I'd like to create a bunch of Factory classes that all inherit from a single abstract base Factory class. Each Factory is responsible...
7
by: ZenRhapsody | last post by:
Are ALL methods in subclasses implemented in C# with vtables and virtual functions? I know I could test this myself, but I'm still using Express edition of 2.0, so I cannot view the optimized...
9
by: Benny | last post by:
The methods within an interface is not virtual. How do I go about doing the following: class BaseClass : IComparable { int CompareTo(Object o) {...} }; class DerivedClass : BasedClass {
3
by: Juha Nieminen | last post by:
This is a simplified version of the situation (the actual situation is quite more complex, but when stripped to the bare minimum, it's like this): class BaseClass { public: virtual void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.