Thread: Full Polymorphism ?

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    please tell me if this is a correct example of polymorphism! I focused mainly on polymorphism of variables which seems to work without the use of virtual but problems??
    I compiled, run and the result seems OK:

    num=1
    num=2.00

    Code:
    class ClassB
    {
    public:
    	int num;
    	virtual void setnum(){num=1;}
    	virtual void printnum(){printf("num=%d\n",num);}
    };
    
    class ClassIn:public ClassB
    {
    public:
    	float num;
    	void setnum(){num=2.0;}	
    	void printnum(){printf("num=%.2f\n",num);}
    
    };
    
    ..
    
    
    main()
    
       		ClassB B;
       		ClassIn In;
       		ClassB *CP;
       		
       		CP=&B;
       		CP->setnum();
       		CP->printnum();
       		
       		CP=&In;
       		CP->setnum();
       		CP->printnum(); 		
       		
       		getchar();

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Looks like polymophism to me.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    just bear in mind that classb::num and classin::num are two different variables.

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by cpjust View Post
    Looks like polymophism to me.
    but is that correct? shouldn't I put "virtual" also in front of the variable or do something different?
    may be you will suggest to declare the variable protected and then access it only via member functions?
    But that is not nice cause the same member function would have different return type in the two classes??

  5. #5
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    i will try to make the problem more specific
    the problem is that with my application i need to drive different industrial cameras with different APIs and specifically different Types to address grabbed images

    So my case simplified would be
    .. Better if put this in anew thread probably..

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but is that correct? shouldn't I put "virtual" also in front of the variable or do something different?
    If the name hiding of ClassB::num by ClassIn::num was not intentional, perhaps what you want to do is to simply inherit the member variable (and at least declare it protected in the base class).

    may be you will suggest to declare the variable protected and then access it only via member functions?
    A protected member from a base class can be accessed directly by derived class member functions. If you declare the member private, as is most common for member variables, then derived class member functions would need to access such a member variable via the protected or public member functions of the base class.

    But that is not nice cause the same member function would have different return type in the two classes??
    You just have a toy program to learn how polymorphism works, syntactically speaking. In more realistic class design, one would ask if ClassB and ClassIn really have an is-a relationship before using public inheritance.

    By the way, you might want to post an actual test program that you compiled, not some weird fragment that looks (and is) syntactically incorrect at a glance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    shouldn't I put "virtual" also in front of the variable or do something different?
    Variables can never be virtual and don't take part in polymorphism. Polymorphism is about the member functions. Variables with the same name in different classes of a hierarchy have absolutely no relation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Just as an aside and a rule of thumb. If you have any virtual functions in a class, you should declare your own constructor and make it virtual as well.
    It's not really necessary for this example, I know, but it's a good habit to get into.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    Just as an aside and a rule of thumb. If you have any virtual functions in a class, you should declare your own constructor and make it virtual as well.
    It's not really necessary for this example, I know, but it's a good habit to get into.

    QuantumPete
    You mean virtual DESTRUCTOR - there is no such thing as a virtual constructor - as you can't really use the object to refer to the virtual function before it has been constructed.

    --
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A virtual clone() function probably comes close to being a "virtual copy constructor" without actually being one, but I agree that a virtual destructor is probably what was intended.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Woops, yes, I meant destructor. Sorry

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Going back to the original question.

    If you have a base class with a member variable X, and want to REPLACE that member variable in a derived class with one that is of a different, then you are effectively hiding the original X. It would then be better to have an "empty" base-class (abstract base class) that doesn't have ANY X member, and implement two classes that derive from the base-class, one with X of type 1, and the other with X of type 2.

    In the case of a camera, you may have completely different formats of the camera buffer. To implement a common functionality, you probably would have to TRANSLATE, or provide a function to read out individual pixels in a common format, since you may have one camera that packs 12 bits per pixel per plane into a buffer of 48 bits (6 bytes), and another than puts 8 bits per pixel per plane into 24 bits (3 bytes), yet another uses 8 bits per pixel, but for efficient memory handling stores it in 32 bits (4 bytes). The order of R, G, B may differ, etc, etc. In this case, you can't just have a function to fetch the buffer, but you need a function that retrieves the pixels in a uniform way.

    --
    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.

  13. #13
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    Going back to the original question.

    If you have a base class with a member variable X, and want to REPLACE that member variable in a derived class with one that is of a different, then you are effectively hiding the original X. It would then be better to have an "empty" base-class (abstract base class) that doesn't have ANY X member, and implement two classes that derive from the base-class, one with X of type 1, and the other with X of type 2.

    In the case of a camera, you may have completely different formats of the camera buffer. To implement a common functionality, you probably would have to TRANSLATE, or provide a function to read out individual pixels in a common format, since you may have one camera that packs 12 bits per pixel per plane into a buffer of 48 bits (6 bytes), and another than puts 8 bits per pixel per plane into 24 bits (3 bytes), yet another uses 8 bits per pixel, but for efficient memory handling stores it in 32 bits (4 bytes). The order of R, G, B may differ, etc, etc. In this case, you can't just have a function to fetch the buffer, but you need a function that retrieves the pixels in a uniform way.

    --
    Mats

    yes thank you
    have a look also to the thread "Different Camera vendors driving : polymorphism "
    I am actually developing that stuff in C but I would like to switch to C++ sooner or later as I will have to drive different cameras

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    yes thank you
    have a look also to the thread "Different Camera vendors driving : polymorphism "
    I am actually developing that stuff in C but I would like to switch to C++ sooner or later as I will have to drive different cameras
    Of course, you don't have to switch to C++ to support multiple device in a device driver. Most Windows device drivers support multiple devices with the same driver, and they do that by using different sets of function pointers - just as an alternative solution.

    Edit: http://cboard.cprogramming.com/showthread.php?t=106398, page 2, around post 20 or so, I give an example of how to do a linked list using "object oriented technique" in C.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. Dialog box with full screen app.
    By curlious in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2003, 10:37 AM
  3. Counting days
    By TheShaggmeister in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2003, 10:29 AM
  4. hashing help
    By alokin in forum C Programming
    Replies: 17
    Last Post: 10-28-2002, 06:33 PM
  5. The glass is half full
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-30-2002, 03:08 PM