Virtual functions/Inheritance.

Member
Posts: 277
Joined: 2004.10
Post: #1
Ok, I'm gona use smalltalk lingo here...
(superclass = base class (C++) subclass = derived class (C++))

I have a superclass and two subclasses that inherit the superclass.

Now, I create a superclass pointer. baseclass * Ptr;
And I put a virtual function in the super class called dowop.
Both subclasses have a function named dowop.
Now, one subclass prints dowop do doo wop...... and the other calls another function.
(that prints something different)

I then point the Ptr to one of the subclasses I made and call Ptr->();
Then I point the Ptr to the other subclass I made and call Ptr->dowop();
(should I call Ptr->baseclass::dowop?!?!? Huh)
Now, the first should print dowop do doo wop.... and the other should print something else.

All is nice and dandy but when I compile I get undefined errors when linking.
/usr/bin/ld: Undefined symbols:
___tf9baseclass
___ti9baseclass
___vt_9baseclass
_dowop__C9baseclass

*edit*
Just tryed Ptr->baseclass::dowop(); and got exact same errors...
*/edit*

Anyway, Here's the code... I have no idea what's going on... just want to see if I made one of my classic "try something that's not in the tutorial" errors... LOL
(oh and I'm building a C++ tool just so you guys know... cin works so I can test large classes in terminal... instead of painstaking debugging)

Code:
#include <iostream>

class baseclass {
public:
void print(void);
virtual void dowop() const;
};


class subclass1 : public baseclass {
public:
void dowop(void);
};


class subclass2 : public baseclass {
public:
void dowop(void);
void somewop(void);
};


void baseclass::print(void)
{
cout << "baseclass print\n";
}

void subclass1::dowop(void)
{
cout << "DoWop do doo wop do doo wop do wop\n";
}

void subclass2::dowop(void)
{
somewop();
}

void subclass2::somewop(void)
{
cout << "some wop...\n";
}


int main (int argc, const char * argv[]) {
subclass1 one;
subclass2 two;
baseclass * Ptr;

Ptr = &one;

Ptr->print();
Ptr->dowop();

Ptr = &two;

Ptr->print();
Ptr->dowop();

return 0;
}

Global warming is caused by hobos and mooses
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
You have to actually define the function in baseclass, then override it in the sub classes (you can just put empty brackets). You should also put const after dowop in the 2 subclasses, since it's there in the base class.
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #3
akb Wrote:You have to actually define the function in baseclass, then override it in the sub classes (you can just put empty brackets). You should also put const after dowop in the 2 subclasses, since it's there in the base class.

Ah, thanks dude!

Um, stupid question... what does the const do? Sneaky
I don't want to put something that might cause major problems later on Wink

Global warming is caused by hobos and mooses
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
const after the function means that you aren't changing anything in the object this. You should either remove it from baseclass, or add it to the subclasses.
Quote this message in a reply
Moderator
Posts: 434
Joined: 2002.09
Post: #5
FYI, if you don't want the base class to define a method, thus forcing any subclasses to define it, you can declare it "pure virtual" in the base class, as follows:

Code:
class baseclass {
public:
void print(void);
virtual void dowop() const [b]= 0;[/b]
};

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #6
Thanks guys!

I understand the rest.
I couldn't find an example of virtual functions that didn't have a polymorphism pointer stuff that had nothing to do with me :-P

Global warming is caused by hobos and mooses
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  virtual functions not working supersonic 2 4,749 Mar 10, 2011 06:22 AM
Last Post: compiler
  exporting c functions and variables NelsonMandella 6 4,631 Apr 1, 2010 05:07 PM
Last Post: NelsonMandella
  Haskell Data Types and Functions wyrmmage 1 3,266 Mar 4, 2008 05:25 PM
Last Post: OneSadCookie
  Ambiguity/private inheritance scope [C++] sealfin 4 5,173 Dec 29, 2006 02:27 PM
Last Post: OneSadCookie
  Virtual method segfault (C++) sealfin 4 4,023 Dec 4, 2006 04:10 AM
Last Post: sealfin