pure virtual

Discussion in 'C++' started by vasanth kumar, Sep 29, 2004.

  1. I am writing a small example below. I want to know, is this allowed as per
    the standard C++. We make an abstract class by making any membor method as
    pure virtual. when we make it pure virtual, what is the meaning of providing
    definition as shown below. are the code lines in bold are good practice.

    class Vehicle {
    public:
    virtual void startEngine() = 0;
    virtual ~Vehicle() {};
    };
    void Vehicle::startEngine()
    {
    }
    class Car : public Vehicle {
    public:
    void startEngine(){
    Vehicle::startEngine();
    }
    };

    void main()
    {
    Car objCar=new Car();
    objCar.Vehicle::startEngine();
    }

    Thanks,
    Vasanth
     
    vasanth kumar, Sep 29, 2004
    #1
    1. Advertisements

  2. vasanth kumar

    Sharad Kala Guest

    "vasanth kumar" <> wrote in message

    > I am writing a small example below. I want to know, is this allowed as per
    > the standard C++. We make an abstract class by making any membor method as
    > pure virtual. when we make it pure virtual, what is the meaning of

    providing
    > definition as shown below. are the code lines in bold are good practice.


    Providing definition for pure virtual functions is legal. It's not a
    question of good/bad practice but more to do with your requirements.
    Consider this code -
    struct base
    {
    // ...
    virtual ~base() = 0; //Definition required
    };

    struct derived : base
    {

    };

    int main()
    {
    derived d;
    }; // Linker error



    > void main()


    Always, int main ()

    Sharad
     
    Sharad Kala, Sep 29, 2004
    #2
    1. Advertisements

  3. thanks for the reply.

    yes, destructor case I understand. provides default destructor. can anyone
    think of a requirement for providing this definition for other member
    functions.

    -Vasanth
    -------------
    "Sharad Kala" <> wrote in message
    news:...
    >
    > "vasanth kumar" <> wrote in message
    >
    > > I am writing a small example below. I want to know, is this allowed as

    per
    > > the standard C++. We make an abstract class by making any membor method

    as
    > > pure virtual. when we make it pure virtual, what is the meaning of

    > providing
    > > definition as shown below. are the code lines in bold are good practice.

    >
    > Providing definition for pure virtual functions is legal. It's not a
    > question of good/bad practice but more to do with your requirements.
    > Consider this code -
    > struct base
    > {
    > // ...
    > virtual ~base() = 0; //Definition required
    > };
    >
    > struct derived : base
    > {
    >
    > };
    >
    > int main()
    > {
    > derived d;
    > }; // Linker error
    >
    >
    >
    > > void main()

    >
    > Always, int main ()
    >
    > Sharad
    >
    >
     
    vasanth kumar, Sep 29, 2004
    #3
  4. vasanth kumar

    Sharad Kala Guest

    "vasanth kumar" <> wrote in message
    > thanks for the reply.


    You are welcome, please don't top-post.

    > yes, destructor case I understand. provides default destructor. can anyone
    > think of a requirement for providing this definition for other member
    > functions.


    Sure, Herb Sutter gives 3 cases in which pure virtual functions could have a
    body - http://www.gotw.ca/gotw/031.htm. He also says that the destructor
    example is the most common.

    Sharad
     
    Sharad Kala, Sep 29, 2004
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    Re: Pure virtual methods

    Victor Bazarov, Jul 12, 2003, in forum: C++
    Replies:
    8
    Views:
    581
    Ron Natalie
    Jul 14, 2003
  2. Maciej Sobczak
    Replies:
    1
    Views:
    432
    Siemel Naran
    Aug 12, 2003
  3. IK
    Replies:
    2
    Views:
    804
    hemraj
    Jul 23, 2004
  4. Todd Aspeotis
    Replies:
    3
    Views:
    744
    Kanenas
    May 30, 2005
  5. John Goche
    Replies:
    10
    Views:
    1,034
    Marcus Kwok
    Dec 8, 2006
  6. Replies:
    7
    Views:
    825
    James Kanze
    May 2, 2007
  7. a
    Replies:
    7
    Views:
    585
    dasjotre
    Jun 28, 2007
  8. Replies:
    4
    Views:
    1,014
    Ben C
    Mar 29, 2008
Loading...