polymorhism

Started by
13 comments, last by chairthrower 16 years ago
@Gage64: You're right. I knew that didn't feel right when I wrote it...
Advertisement
Quote:
In order to use polymorphism, you must make use of inheritance and virtual functions. Here is a simple example.

The first point is correct, but your example does not demonstrate dynamic dispatch as you perhaps intended -- the function you call, Compete(), is not virtual.

Quote:
im not getting snippy why would i need to take advice if im asking for help TO build something with polymorism

Because the example code you posted has serious flaws. You are clearly inexperienced and lacking in understanding of some fundamental concepts. You were advised as to how you might improve that, and you chose instead to respond with a baseless but subtle insult to the user that offered you advice.

Don't do that again.

Quote:
im here to learn not to get fussed at like im a child

Then stop acting like one. If you are here to learn, you'll appreciate having your errors pointed out so you can learn to fix them, even if they are not directly related to your original question. If you choose, instead, to retaliate against members who don't give the answer you want (instead of a correct answer), then I -- at least -- question your actual desire to better yourself.

Quote:
to my issue now i have a function in my text.cpp named test 1 and a virtual function called test 2 the idea is to set a simple cout in these two functions and allocate memory a variable and then i setup the function so it can open a file somewhere else,get some simple input cout a message from a message class

Slow down, take a deep breath. You need to explain your problem better -- perhaps with some punctuation. It is unclear what you are trying to achieve beyond some simple demonstration of polymorphic behavior.

So let's look at your code. I don't see main() anywhere, so it's impossible to debug your output. I can tell you, however, that you're probably not involving polymorphic behavior anywhere, and you certainly aren't getting the expected virtual function calls.

You're trying to achieve 'dynamic dispatch' -- calling a function based on the dynamic (actual at runtime) type of the object, rather than the static (compile time) type of the object. This is done via virtual methods, as has been pointed out. You have a virtual method test1() in class Text, but you never overload it in any of the base classes. Also not that for dynamic dispatch to work it must occur via a reference or pointer; without seeing your main() and how you create these objects, it's hard to tell if you're doing that part right.

A simple example:
#include <iostream>struct Base {  virtual void DoStuff() {    std::cout << "Base::DoStuff()\n";  }};struct Derived : Base {  void DoStuff() {    std::cout << "Derived::DoStuff()\n";  }};int main() {  Base* b = new Base();  Base* d = new Derived();  b->DoStuff(); // Base::DoStuff()  d->DoStuff(); // Derived::DoStuff()  delete b;  delete d;}

Since DoStuff() is virtual, and the calls are through pointers, the call through d will call the derived method since the dynamic type of the object is Derived, even though the static type is Base. The C++ FAQ discusses this in detail, as do many good C++ books.

Other things wrong with your code you should consider (in brief, major glaring issues only): prefer std::string in C++, not char*, for string data. Do not employ "using namespace std;" in headers files as it pollutes the global namespace. Your comments are mostly superfluous and noisy, and you could really stand to spend some time organizing the code better and coming up with more descriptive names.
Quote:Original post by jpetrie
Quote:
im not getting snippy why would i need to take advice if im asking for help TO build something with polymorism

Because the example code you posted has serious flaws. You are clearly inexperienced and lacking in understanding of some fundamental concepts. You were advised as to how you might improve that, and you chose instead to respond with a baseless but subtle insult to the user that offered you advice.

Don't do that again.

Quote:
im here to learn not to get fussed at like im a child

Then stop acting like one. If you are here to learn, you'll appreciate having your errors pointed out so you can learn to fix them, even if they are not directly related to your original question. If you choose, instead, to retaliate against members who don't give the answer you want (instead of a correct answer), then I -- at least -- question your actual desire to better yourself.


Just what I was thinking.

Quote:
The C++ FAQ discusses this in detail, as do many good C++ books.


Yep; that's the best starting point I could think of. In particular, pay attention to section 6.
Quote:

Slow down, take a deep breath. You need to explain your problem better -- perhaps with some punctuation. It is unclear what you are trying to achieve beyond some simple demonstration of polymorphic behavior.




Ok what im actually trying to do with test is show a basic example of polymorphic behavior my by using the two functions I created in The class called Text and the function called test() .

From the example I read I believe I misunderstood the coding example I was reading , I thought u had to state the test() and name a virtual function with a similar name .. so now that I have seen your code I.



I didn’t display my main function because I do not have any thing in there but



main();

{

Choices showUserDisplay;



showUserDisplay.firstUsersChoices();

}



Everything else is controlled in choices ..so what my flow is :

Text = base class , I created a child class which is Message , now it suppose to

inherit the function called test and give test polymorphic behavior that I set in the Message class, from here I go to my choices class it will call the original function ,display its message and then I will call the next function with polymorphic behavior and show its contents for display ,which are declared in case 5 of the “firstUsersChoices();” function in choices.cpp





Quote:

Other things wrong with your code you should consider (in brief, major glaring issues only): prefer std::string in C++, not char*, for string data. Do not employ "using namespace std;" in headers files as it pollutes the global namespace.




Can you show me an example I have only been doing this for 2 months ?


There are two sides to this polymorphic thing. first there's knowing how technically, to structure and invoke the behavour in a specific programming language. along with this, there is an independent issue in knowing when and where it might make sense to apply it as a way to solve structure problems in any given program design. You are getting lots of responses from members - posting short snippets of code demonstrating the principle as an alternative to commenting in relation to your own code. i think in part, this is because its not really clear that your example problem best presents a clear use case or need for polymorphic behaviour, or else its is too complicated when your question really is - how do I go about doing method overriding in c++?

the example code given by jpetrie is really very good. why? because it is very simple and very clear, allowing anyone to take it and very quickly experiment. copy/paste it into a file and compile it. remove the virtual keyword in the base class definition and see what happens?. change the name of the method so that it is *different* to the name of the method in the parent class and see what happens? change the typing on the signature. In ten minutes you will probably have a lot better a grasp on the subject than trying to compose your own example that runs to several pages. And what is not clear from this you can probably piece together from the other links that have been given in the thread or else could be asked about here with a clearer focus.

Once you grok the detail of the technical implementation then polymorphism becomes much more of a design question - eg would it be appropriate to write a file reader and then subclass the stream reading behvavor to do automatic conversion of utf-8 to ascii or is there a better way? btw the jury is still out about whether/why/when to use polymorphism - i am sure i have seen a video of stroustrop the inventer of the c++ language stating that he think's it is used 'much too often'. also there are lots of threads on the forums here that try to consider other approaches to dynamic object inheritance (static polymorphism, components,aggregation etc).This probably doesnt mean very much at the moment however i say it to provide an explanation as to why you may not be getting the clear cut answers to your question and code that you may have expected.

This topic is closed to new replies.

Advertisement