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. Advertising

  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. Advertising

  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. Advertising

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. IK
    Replies:
    2
    Views:
    612
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    467
    Kanenas
    May 30, 2005
  3. John Goche
    Replies:
    10
    Views:
    753
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    589
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
Loading...

Share This Page