LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Derived classes in C++ (https://www.linuxquestions.org/questions/programming-9/derived-classes-in-c-808066/)

brazilnut 05-15-2010 05:08 PM

Derived classes in C++
 
Hi, just looking at derived classes, the following example doesn't give me the expected results, i.e. using the derived classes functions.

Code:

#include <iostream>
#include <string>
#include <vector>

//        g++ classes.cpp -o classes

using namespace std;

//        BASE CLASS
class node {
        public:
                string name;
               
                node()        {        name = "test";        }
                void print()        {        cout << name << endl;        }
};

//        DERIVED CLASSES
class node_a : public node {
        public:
                void print()        {        cout << "a: " << name << endl;        }
};

class node_b : public node {
        public:
                node_b()        {        name = "in";        }
                void print()        {        node::print();        zoom();        }
                void zoom()        {        cout << "zoom: " << name << endl;        }
};

int main(int argc, char* argv[])
{
        std::vector <node> e;
       
        node n;
        node_a na;
        node_b nb;
       
        e.push_back(n);
        e.push_back(na);
        e.push_back(nb);
       
        e[0].print();
        e[1].print();
        e[2].print();
       
        //e[2].zoom();
       
        return 0;

}

the results i'm getting are:
Code:

test
test
in

but i'm after
Code:

test
a: test
zoom: in

cheers...

posixculprit 05-15-2010 05:13 PM

http://en.wikipedia.org/wiki/Virtual_function

brazilnut 05-15-2010 05:24 PM

merci

johnsfine 05-15-2010 05:29 PM

You cannot put a derived class object into a container of base class objects.

Trying to do so will make a base class object by copying the base class portion of the derived class object you tried to insert.

Virtual functions would be the answer if you were trying to use a base class reference or pointer that points to a derived class object and you wanted the derived class version of the function.

But containers of polymorphic objects represent a much harder problem in C++.

You can have a container of base class pointer, pointing to polymorphic objects (then you would still need to make the functions virtual to achieve what you want). But a container of pointers introduces a bunch of extra issues involving object ownership (decision of when to delete) as well as the syntactic overhead of the extra level of indirection.

ArthurSittler 05-15-2010 06:44 PM

Simpler example simplifies debugging
 
brazilnut, I usually start with simpler examples to simplify debugging. After the smaller pieces work, putting them into bigger pieces either will work, or the problem is in the bigger pieces. I inserted the following lines before the pushback calls:

Code:

        n.print();     
        na.print();
        nb.print();

The output then looked like

Quote:

test
a: test
in
zoom: in
test
test
in
So the problem is that the pushback() and print() functions of the vector class don't work as you expect.

paulsm4 05-15-2010 11:20 PM

ArthurSittler -

No, the main problem was that the OP wasn't considering the importance of virtual functions. posixculprit told him, and brazilnut totally got the message.

And Johnsfine's note about "object pointers" vs. "stack objects", and the whole issue of "copy semantics", was absolutely correct. Another (of the many reasons!) I believe C++ sucks ... at least as a beginners language.

Brazilnut - as a courtesy, you might want to "thank" posixculprit (the blue "thumbs up" icon) and/or mark his thread as "useful" (I did the same). You might also want to mark the thread as "solved" (assuming the problem is indeed resolved).

ArthurSittler 05-16-2010 01:56 AM

thanks
 
Thank you, brazilnut, paulsm4, and posixculprit.

The compiler never complained about any type conflict when it was asked to use the derived types. It simply used the objects' base classes. That is the type declared for the container.

Not as I expected, either.

brazilnut 05-16-2010 06:45 AM

paulsm4

I do use the thanks link, but it doesn't seem to work half the time, it's actually registered the thanks from last night now, so?

Also I don't see a solved link and can't change the title from edit... so there's a thanks up for grabs for the first to shed some light...

Cheers!

johnsfine 05-16-2010 06:50 AM

Quote:

Originally Posted by brazilnut (Post 3970404)
Also I don't see a solved link

In the Thread Tools menu near the top of your view of the thread.

Quote:

can't change the title from edit.
Edit your first post in the thread, then click the Go Advanced button. Then it lets you edit the title. But if adding "Solved" was the reason to edit the title, use the Thread Tools menu instead.


All times are GMT -5. The time now is 10:37 AM.