Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Superclass to Subclass. Dynamic/virtual I believe?

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> C / C++
Author Message
jaised
Grasshopper


Joined: 12 Feb 2009
Posts: 1

PostPosted: Thu Feb 12, 2009 1:21 am    Post subject: Superclass to Subclass. Dynamic/virtual I believe? Reply with quote

Ok, I will do my best to explain my situation. I have tried looking around to try to make this work and have yet to find anything so I hope you can help me out. Thanks in advance!!!

NOTE:::: .omponent is supposed to be with a c but wont let me post it due to dumb automated msg, calling it spam.

So I have a Document class which contains a vector<DocComponent*>. These docComponents are things such as Text, Table, and Graphic objects. Each are child classes of DocComponent. In my Document class, I have a addElement method:

----------------------------------------------------------------------------------
Code:
void Document::addElement(DocComponent &component)
{
   // binary search and i-sort pg. 147
   components.push_back(&component);
}

----------------------------------------------------------------------------------
Therefore, it adds each type of object i.e. Text, Graphic, ect. Each of these classes has an overloaded operator<< method used to just display their variables. Below, I am trying to print out each object with their respectful overloaded operator<< method. However, it keeps calling the DocComponent's << method and not the respectful class.

For example I create and add each kind of child class objects to the document component array. When I print out the document it displays 1, 2, 3 (which is what the DocComponent << does) where it is actually supposed to each field.
-----------------------------------------------------------------------------------
Code:
void main() {       
        Document d;
   Text* t = new Text(1, 2, 3, 4, 5, 6);
   Table* tbl = new Table(1, 2, 3, 4, 5, 6, 7);
   Graphics* g = new Graphics(1, 2, 3, 4, 5);
   d.addElement(*t);
   d.addElement(*tbl);
   d.addElement(*g);
   cout << d << endl;
}

------------------------------------------------------------------------------------
DocComponent << method:

Code:
ostream& operator<<(ostream &s, DocComponent &doc)
{
   // formatting for output to the user via cout command
   s << "*DOC_COMPONENT*\n" <<
      "Top Margin (in inches): " << doc.top << "\n" <<
      "Left Margin (in inches): " << doc.left << "\n" <<
      "Right Margin (in inches): " << doc.right << "\n\n";
   return s;
}

------------------------------------------------------------------------------------
All 3 subclasses have similar << methods, but here is the example of Text:

Code:
ostream& operator<< (ostream  &s, Text &t)
{
   s << "*TEXT*\n" <<
      "Top Margin (in inches): " << t.top << "\n" <<
      "Left Margin (in inches): " << t.left << "\n" <<
      "Right Margin (in inches): " << t.right << "\n" <<
      "Font Type: " << t.fontType << "\n" <<
      "Font Size: " << t.fontSize << "\n" <<
      "Number of Characters: " << t.noChars << "\n\n";
   return s;
}

---------------------------------------------------------------------------------------
Below is the Document's << method:
------------------------------------------------------------------------------------
Code:
ostream& operator<< (ostream &s, Document &d)
{
   // display the Document's information via for-loop
   s << "*DOCUMENT*\n\n\n";

   // DocComponents
   s << "components vector\n";
   for(int i = 0; i < d.omponents.size(); i++)
   {
      s<< "\t" << *(d.omponents[i]);
      s << "\n";
   }
   s << "\n";
   return s;
}

----------------------------------------------------------------------------------
I read all around and it said that it had to do something with virtual, but I cannot find a way to make << method both friend and virtual. Any help with this would be outstanding! Thank you for the help!
Back to top
aschenta
*narf*


Joined: 07 May 2003
Posts: 542
Location: Windsor Ontario Canada

PostPosted: Thu Feb 12, 2009 8:39 am    Post subject: Reply with quote

my c++ is a bit rusty but I'll give it a shot. virtual goes on the method decaration:
Code:
friend:
virtual ostream& operator<<(ostream &s, DocComponent &doc);

I think you have another issue as well. The << operator for Text passes a Text as the second parameter which differs from the DocComponent << operator. Even if it is virtual it will still likely call the DocComponent << operator.

If I'm correct you have to specify DocComponent as the second parameter for all child classes to achieve what you want.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> C / C++ All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group