LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2010, 05:08 PM   #1
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
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...
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-15-2010, 05:13 PM   #2
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
http://en.wikipedia.org/wiki/Virtual_function
 
2 members found this post helpful.
Old 05-15-2010, 05:24 PM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
merci
 
Old 05-15-2010, 05:29 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.

Last edited by johnsfine; 05-15-2010 at 05:32 PM.
 
2 members found this post helpful.
Old 05-15-2010, 06:44 PM   #5
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
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.
 
Old 05-15-2010, 11:20 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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).

Last edited by paulsm4; 05-15-2010 at 11:22 PM.
 
Old 05-16-2010, 01:56 AM   #7
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
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.
 
Old 05-16-2010, 06:45 AM   #8
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Original Poster
Rep: Reputation: 16
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!
 
Old 05-16-2010, 06:50 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by brazilnut View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: derived class? unihiekka Programming 4 02-03-2009 09:40 AM
Fastest Ubutu-derived distro? dhave Ubuntu 6 07-14-2007 10:17 PM
In C++, how to write destructor of derived class? ArthurHuang Programming 3 06-04-2006 10:40 PM
Derived work and documentation under GPL v2 klnasveschuk Linux - General 2 02-06-2006 05:25 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration