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

  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 10:05 PM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    }
    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

  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

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, 07:49 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM