Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 3rd, 2008, 04:42 PM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default polymorphism and abstract classes

I could not understand why to use a pure virtual function to make the class an abstract class insted of using only a virtual function.Or even why to use a virtual function and define a pointer to base class that points the derived class we could use a pointer of type derived class itself instead they say that is called polymorphism.Can someone please tell me advantages of using them by giving some practical examples.Thank you.

Reply With Quote
  #2 (permalink)  
Old February 20th, 2008, 04:16 AM
Authorized User
 
Join Date: May 2007
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Peter_APIIT
Default

I could not understand why to use a pure virtual function to make the class an abstract class instead of using only a virtual function

As far as i know, a class ia abstract when contain one pure virtual function.

The purpose of pure virtual function is this function may redefined my your later derived class function.

It seems unpractical for me because i also cannot see this usage of this features because this is out of my knowledge.

Polymorphism is the most difficult part of OOP.

http://en.wikipedia.org/wiki/Polymor...ed_programming



Linux is the best OS in the world.
Reply With Quote
  #3 (permalink)  
Old February 21st, 2008, 06:20 AM
Registered User
 
Join Date: Feb 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, maybe this simple example can help


Quote:
quote:
class Object
{
public:
    virtual void draw()=0;
};

class Car: public Object
{
public:
    virtual void draw()
    {
        //car specific drawing
    };
};

class Airplane: public Object
{
public:
    virtual void draw()
    {
        //Airplane specific drawing
    };
};

...

class Graphics
{
public:
    void DrawObjects(Object* pObject)
    {
        pObject->draw(); //can handle draw for every derived class from object
    }
};

...
Reply With Quote
  #4 (permalink)  
Old October 16th, 2008, 01:39 AM
Authorized User
 
Join Date: Oct 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Polymorphisms is a single variable is used with several different types of related objects at different places in a program and Abstract is a class which cannot be instantiated.
====================================
Victor
Our mission is to provide high quality end to end solutions to the BPO segment in a manner that will improve the operational efficiency while reducing the cost of the services to the client.
[email protected]


Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract classes/Interfaces vasancer BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 October 22nd, 2008 02:06 PM
Diffrences of Abstract Classes and Interfaces jimblanc Java Basics 4 September 4th, 2006 01:30 PM
question about abstract class/polymorphism csharplearner C# 4 October 11th, 2005 03:27 PM
Abstract classes guppyheart C# 1 September 9th, 2003 05:23 PM





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