Like Tree4Likes

A C++ program examples showing Polymorphism, please help.

This is a discussion on A C++ program examples showing Polymorphism, please help. within the C++ Programming forums, part of the General Programming Boards category; Dear CProgramming members and moderators, Hello. A pleasant day to everyone, I hope everyone is doing okay upon reading this ...

  1. #1
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Talking A C++ program examples showing Polymorphism, please help.

    Dear CProgramming members and moderators,

    Hello. A pleasant day to everyone, I hope everyone is doing okay upon reading this thread of mine. I'm currently (well, just reading a lillte) about polymorphism on C++, but I honestly find it difficult to understand sample programs especially on the Internet, I wondering (and requesting) if someone could give me at least four to five C++ example programs about polymorphism, with simple explanation on the workaround of the program. Well I just hope this is not too much to ask, just a very simple example and explanation will do.


    Thank you for everyone who could share little C++ programs. Thank you very much.



    Respectfully yours,

    MarkSquall

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Have you already tried searching this site with keywords "polymorphism" and/or "inheritance"?
    Arslan Akbar likes this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Smile

    Dear Dave_Sinkula ,

    Good day to you Sir. I should say "Yes", I have tried searching for those keywords on the Internet, but then again the example they provided are somehow too advanced on my part; well I could admit that I don't open more site, so I guess I'll search for more sites about it. But, however, I really still hope that I could get some examples of you all members out there, because I know you all can explain the program to me in a friendly manner. Thanks again everyone.



    Respectfully yours,

    MarkSquall

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Well, I kinda meant this site, as in http://cboard.cprogramming.com/searc...earchid=761509 such that perhaps we can all be on the same page without reinventing a couple wheels.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Simple example:
    Code:
    class A
    {
    public:
    	virtual void foo();
    };
    
    class B: public A
    {
    public:
    	virtual void foo();
    };
    
    int main()
    {
    	B b;
    	A* p = dynamic_cast<A*>(&b);
    	p->foo(); // Calls B.foo(), even though the pointer is A*
    	return 0;
    }
    Last edited by Elysia; 02-19-2008 at 03:32 AM.
    Arslan Akbar likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Dunno, compiles for me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    A little more accurately:
    Code:
    class A
    {
    public:
    	virtual void foo();
    };
    
    class B : public A
    {
    public:
    	virtual void foo();
    };
    
    int main()
    {
    	B b;
    	A* p = &b;
    	p->foo(); // Calls B.foo(), even though the pointer is A*
    	return 0;
    }
    Dunno, compiles for me.
    But it does not demonstrate polymorphism. You are casting a pointer to a class B to a pointer to an unrelated class A.
    Arslan Akbar likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    A very simple polymorphism example:
    Code:
    void PrintHello()
    {
         cout << "Hello\n";
    }
    void PrintHello(int n) 
    {
        for(int i=0; i<n; i++)
            cout << "Hello\n";
    }
    The two functions have the same name but do different things dependant on the parameters.
    Arslan Akbar likes this.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    A very simple polymorphism example:
    Code:
    void PrintHello()
    {
         cout << "Hello\n";
    }
    void PrintHello(int n) 
    {
        for(int i=0; i<n; i++)
            cout << "Hello\n";
    }
    The two functions have the same name but do different things dependant on the parameters.
    That is NOT polymorphism, but function overloading. A different concept all together.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Whoops. My bad.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by laserlight View Post
    But it does not demonstrate polymorphism. You are casting a pointer to a class B to a pointer to an unrelated class A.
    I think it's my time to say oops now
    Of course, I forgot to derive B from A.
    But with that done, it's definitely polymorphism, the concept of virtual functions.
    Original post updated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Of course, I forgot to derive B from A.
    But with that done, it's definitely polymorphism, the concept of virtual functions.
    Yep.

    Original post updated.
    I corrected your post with my reply earlier
    You do not need dynamic_cast since B is-a A.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    oh krap...in one of my interviews the interviewer said when a derived class is cast to a base class object, the base class's virtual function gets run. i admit i'm puerile enough to have fall for that!

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jian2587 View Post
    oh krap...in one of my interviews the interviewer said when a derived class is cast to a base class object, the base class's virtual function gets run. i admit i'm puerile enough to have fall for that!
    So we can hope for one of two things:
    1. That you get the job and can set them right.
    2. That the interviewer isn't part of the design-team...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by laserlight View Post
    Yep.

    I corrected your post with my reply earlier
    You do not need dynamic_cast since B is-a A.
    Hehe, so you did. But still, it looks better if the original post is correct too, no?
    At least it shows I incorporated the feedback and fixed the subtle bugs. And anyone who reads it won't get the wrong idea either!

    I suppose you're right about the cast, as well... Come to think of it, otherwise we'd have to cast the pointer everytime we passed it to a function that took a pointer to reference to the base class...

    Quote Originally Posted by jian2587 View Post
    oh krap...in one of my interviews the interviewer said when a derived class is cast to a base class object, the base class's virtual function gets run. i admit i'm puerile enough to have fall for that!
    Well, that's why the virtual keyword exists
    If you would omit the virtual, the base class's function would indeed be called. But when using virtual, the polymorphism is invoked and the compiler uses a virtual function table to call the correct function.

    A more practical example of how polymorphism might be used is maybe like this.
    You have a utility function that does a lot of work, presumably with an object. However, there are also lots of different types of that object. So the function can work from a base class and call virtual functions so the derived functions get called and do the correct work.
    You might compare it to a car assembly. Here's some pseudo:

    Code:
    class Car
    {
    public:
    	enum CarType { PART_MOTOR };
    	virtual bool AddPart(CarType Type) = 0;
    	virtual void Assemble() = 0;
    };
    
    class Ford: public Car
    {
    public:
    	virtual bool AddPart(CarType Type);
    	virtual void Assemble();
    };
    
    class Volvo: public Car
    {
    public:
    	virtual bool AddPart(CarType Type);
    	virtual void Assemble();
    }
    
    void AssembleCar(Car& car)
    {
    	// Assemble some parts into car
    	car.AddPart(Car::PART_MOTOR);
    	// Add some more parts
    	// Finally, assembly everything
    	car.Assemble();
    }
    
    int main()
    {
    	Volvo volvo;
    	Ford ford;
    	AssembleCar(volvo);
    	AssembleCar(ford);
    	return 0;
    }
    Since both cars wouldn't be assembled exactly the same, they can assemble themselves in their respective functions. This is an excellent example of how polymorphism is used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21