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; well the interviewer didn't start out that way. She gave me codes in java, where a derived class overrides a ...

  1. #16
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    well the interviewer didn't start out that way. She gave me codes in java, where a derived class overrides a function in the base class. She asked me if I invoke the overridden function of the derived class which is cast into its base class type, the base's or the derived's gets run. I said the derived's. She said good, then asked what if this is in C++. I said it'd be the same and she said NO, C++ works in the opposite way. I hereby seek enlightenment from you guys.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    using namespace std;
    class Base
    {
    public:
    	int func_novirt();
    	virtual int func_virt();
    };
    
    class Derived : public Base
    {
    public:
    	int func_novirt();
    	virtual int func_virt();
    };
    
    int Base::func_novirt (  ) 
    {
    	cout << "Base\n";
    	return 0;
    }
    
    int Derived::func_novirt ( )
    {
    	cout << "derived\n";
    	return 0;
    }
    int Base::func_virt (  ) 
    {
    	cout << "Base virtual\n";
    	return 0;
    }
    
    int Derived::func_virt ( )
    {
    	cout << "derived virtual\n";
    	return 0;
    }
    
    
    int main()
    {
    	Base b;
    	Derived d;
    
    	b.func_novirt ();
    	d.func_novirt ();
    
    	cout << "Virtuals\n";
    
    	b.func_virt ();
    	d. func_virt ();
    
    	cout << "through base\n";
    	Base *bp;
    	bp = &b;
    	bp->func_virt ();
    	bp->func_novirt ();
    	bp = &d;
    	bp->func_virt ();
    	bp->func_novirt ();
    
    	return 0;
    }
    compile && run && study
    Last edited by robwhit; 02-19-2008 at 09:05 PM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    She asked me if I invoke the overridden function of the derived class which is cast into its base class type, the base's or the derived's gets run. I said the derived's. She said good, then asked what if this is in C++. I said it'd be the same and she said NO, C++ works in the opposite way. I hereby seek enlightenment from you guys.
    The interviewer is correct. What she was asking about is type slicing, where an object of a derived class is cast to an object of its base class. Polymorphism in C++ works via pointers and references, so if she presented a case where a pointer to a derived class is cast to a pointer to the base class, then your answer would be correct.

    This is an example that demonstrates type slicing and then polymorphic behaviour via a pointer:
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        virtual void foo() const
        {
            std::cout << "Base::foo()" << std::endl;
        }
    };
    
    class Derived : public Base
    {
    public:
        void foo() const
        {
            std::cout << "Derived::foo()" << std::endl;
        }
    };
    
    int main()
    {
        Derived derived;
    
        static_cast<Base>(derived).foo(); // Cast from Derived to Base: type slicing occurs, so Base::foo() is called.
    
        Base* base_ptr = &derived; // Implicit cast from pointer to Derived to pointer to Base.
        base_ptr->foo();           // Polymorphism is used, so Derived::foo() is called.
    }
    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

  4. #19
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    I see. I actually tried writing similar codes to test it out but it didn't come out right. apparently there's one crucial difference. When coercing the derived into the base type I used explicit casting.

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

    Smile Thank you everyone.

    Thanks everyone again for the information. What makes really fun here is solving the problem I have in C/C++ programming together with everyone's help. I am really glad I come across to this site.


    I have more questions to post here soon, so I hope everyone will be patient enough to teach me C and C++. God bless.


    Respectfully Yours,

    MarkSquall

    P.S.
    But how about Java? Is there a nice forum like this one about Java programming? Hope someone could share it too.
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

Page 2 of 2 FirstFirst 12
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