473,659 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1802
"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??......Th en 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******** *************@d read12.news.tel e.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******** *************@d read12.news.tel e.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******** *************@d read12.news.tel e.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 misunderstandin g. 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
3769
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 data in my vtable? RTTI could be implemented this way instead of as a special case for the compiler. It would also be useful for class specific memory management implementations. -- Daniel A. Graifer
5
1577
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 base class. Unfortunately, the baseclass method gets called but the preceding code in the derived class isn't. Mind you you that the base class is a template class. Does anybody of you have an idea why this isn't working?
12
2059
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 class BaseClass is declared with a function me() that takes no arguments and returns itself, or a pointer or reference to itself. - A derived class DerivedClass that override me() that takes no arguments and returns itself, a pointer or reference...
4
1632
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 handful of inherited classes. The static method calls the ctor of the appropriate inherited class and returns it as type IBaseClass. There are no new methods or properties in the inherited classes and public clients never need to know which...
2
1289
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( ) abstract ; // i've tried '= 0' syntax too, no luck } ;
4
2016
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 for the creation of objects of a type that inherit from a base class. Factory is probably the wrong word here, as it's really a persistance class, but the problem is the same. Example: abstract BaseClass abstract BaseFactory, uses BaseClasses...
7
1456
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 disassembly. I know that you must use vtables for all virtual functions (and interfaces), but if there are non-virtual functions, does the compiler use a vtable for everything anyway?
9
1207
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
1383
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 cleanup(); virtual ~BaseClass() { cleanup(); } };
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.