pure virtual

V

vasanth kumar

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
 
S

Sharad Kala

vasanth kumar said:
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
 
V

vasanth kumar

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
-------------
 
S

Sharad Kala

vasanth kumar said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top