Derived Virtual Function Declaration

Is it possible to define the inhertied virtual functions outside the derived class definition using '::' operator?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class outline
{
protected:
    virtual void insert();
    virtual void remove();
    virtual void display();
    void mainmenu();
};
class stack : outline
{
    int top;
public:
    stack() { top=-1;}
    using outline::mainmenu;
};

My question is whether
void stack::insert() { /*code*/ }
is possible or will I have to define it in the class like:
1
2
3
4
5
6
7
8
class stack : outline
{
    int top;
    void insert() { /*code*/ }
public:
    stack() { top=-1;}
    using outline::mainmenu;
};

Last edited on
Hi,

Is it possible to define the inhertied virtual functions outside the derived class definition using '::' operator?


Absolutely yes, it's preferable to split your class definition and implementation into class.hpp and class.cpp. Don't put more than one class into a file.

If you have template code, all of it has to go in a header.

Also, look into using a member initialiser list, rather than assignment.

mainmenu should have it's own class.

Why does stack inherit from outline? That implies that stack "IS A" outline

Hope this helps a little bit :+)
@TheIdeasMan

To be quite honest, I'm not familiar with the concept of linkers and stuff (would love if you could link (pun intended) some help pages).

Yeah, I got the whole ' IS A'/'HAS A' thing, but I wanted to somehow create virtual functions such that they could be defined uniquely in classes like stack,queue,linked lists, etc, basically an AIO data structure program.
Is there any way to do this, apart from virtual functions? Also, could you elaborate
member initialiser list
?

(PS - not an assignment or anything, was just curious to see how could it effectively be implemented.
PS(S) - I've heard of "override" and could you show how one would use that aswell?
)
To be quite honest, I'm not familiar with the concept of linkers and stuff (would love if you could link (pun intended) some help pages)


http://www.lurklurk.org/linkers/linkers.html
Also, could you elaborate
member initialiser list


Here's one, more if you Google :+)

http://en.cppreference.com/w/cpp/language/initializer_list


Yeah, I got the whole ' IS A'/'HAS A' thing, but I wanted to somehow create virtual functions such that they could be defined uniquely in classes like stack,queue,linked lists, etc, basically an AIO data structure program.
Is there any way to do this, apart from virtual functions?


Well the STL has templates for each of those. I am not sure it's worth it to have inheritance for this. Even if the function were pure virtual, I don't see any advantage.

If you want to practice with Polymorphism, maybe you can choose a different topic like Animals, or People say?
@cire

Thanks, finally a detailed enough page!

@TheIdeasMan

If the functions are virtual and I inherit them, wouldn't they save the 'hassle' of including prototypes in every subsequent 'stack','linkedstack','queue','linkedqueue' class?

I would, but this is, it sort of helps me for my upcoming exams aswell.. :P

Thanks,though!
If the functions are virtual and I inherit them, wouldn't they save the 'hassle' of including prototypes in every subsequent 'stack','linkedstack','queue','linkedqueue' class?


But you would have to redefine them all anyway, because they do different things operating on different objects. I know that sounds mad: that is what specialisation is all about. But I can't help thinking you are doing polymorphism here for the sake of it.

With polymorphism, one does try to push variables and functions as high up the inheritance tree as possible, creating a new base class where needed. This has the advantage that functions can take pointers to the base class without having to overload for each and every derived class, thus streamlining the code. I can see this is what you seem to be doing.

The STL seems to avoid that, by using templates and such things as type traits and TMP (Template Meta Programming). For example, the std::find algorithm arguments are templated iterators and a value.
http://en.cppreference.com/w/cpp/algorithm/find


Also, things like insert() aren't algorithms, I imagine because they are a function belonging to their individual container class (string, stack, queue etc.) Note that container is a category, not a class.

I had a look at the implementation on my system, there doesn't seem to be a base (container) class for std::stack, std::queue, std::list etcetera. I know the implementation of things is not set in stone, your implementation might be different. But I think the idea will be the same.


Did you Google override ? It's a pretty simple concept :+)

Regards
Topic archived. No new replies allowed.