Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2020, 02:55 AM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Runtime Polymorphism in C++

Hello Everyone, I am confused about the concept of Runtime Polymorphism in C++. I have faced one interview and the interviewer asked me this question where I was not given the answer. I have checked in google with code example but I didn't get it. Can anyone know about it and suggest me some more questions which are very useful to me for my upcoming interviews? I have added one code example of runtime polymorphism, is it right?

#include
using namespace std;
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base {
public:
void show() { cout<<"In Derived \n"; }
};

int main(void) {
Base *bp = new Derived;
bp->show(); // <- Runtime Polymorphism in Action
return 0;
}
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2020, 04:38 AM
Registered User
Points: 16, Level: 1
Points: 16, Level: 1 Points: 16, Level: 1 Points: 16, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the aforementioned program bp is a pointer of type Base. A call to bp->show() calls show() function of the Derived class. This is because bp points to an object of the Derived class.

Things essential to writing a virtual function in C++ are:

1. A base class
2. A derived class
3. A function with the same name in both the classes i.e. the base class and the 4. derived class
5. A pointer or reference of base class type that points or refers, respectively to an object of the derived class

Now i have clear about virtual functions in C++, thanks to this post. It's very useful and helpful.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
polymorphism abilify BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 0 January 25th, 2010 07:25 AM
Polymorphism Christopher C# 2008 aka C# 3.0 7 December 17th, 2009 02:57 AM
Polymorphism -- Help Me Please rlynx BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 4 October 18th, 2009 06:56 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM



All times are GMT -4. The time now is 05:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.