• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ pure virtual function question

hans007

Lifer
so i was asked this at an interview today and did not know the answer.


when you declare pure virtuals in c++

its like say virtual int bob (int x)=0;


if you go like virtual int bob (int x)=9;

or something like that , a number besides 0 what happens?
 
Originally posted by: hans007
so i was asked this at an interview today and did not know the answer.


when you declare pure virtuals in c++

its like say virtual int bob (int x)=0;


if you go like virtual int bob (int x)=9;

or something like that , a number besides 0 what happens?


You get an error.

Any in function in the base class that has virtual in front of it can be overridden in the inheriting classes. You must provide functionality in the base class.

In a pure virtual class (has at lease one pure virtual function)you do not provide functionality in the pure virtual function of the base class. You cannot create an instance of the base class, but you can create a pointer to the base class so you can handle mixed inherited classes.
 
Making a function member of a class virtual means that a derived class may redefine that function. Making it pure virtual means that a derived class must redefine it - at least if you want to instantiate members of that class.

A pure virtual function makes its class an abstract base class - you can't create objects of that type. An abstract base class is often used to define a common API for a class hierarchy. A typical example is an abstract base class called Shape. Various classes could inherit from Shape such as Circle, Square, Trangle, etc.

If you put a pure virtual function called area in shape, then every derived class must implement the area function with the formula appropriate to that sub class. So, as a downstream user, you know that if you are using a class derived from Shape, it must have an appropriate area function.

edit - I obviously can't read this a.m. :roll:
I tried it on one of my classes and got the following compiler error with the Gnu compiler:
In file included from point.cc:7:
point.h:327: error: invalid initializer for virtual method `virtual void
Point:😛ropagate(double)'
 
um yeah i think you guys are not understanding what i asked. i dont need an explanation of what a pure virtual IS, or what an abstract class is. i know all that stuff.

what i know is t a virtual to be pure has to have =0; that doesnt mean the function is =0 it seems to just be the notation for pure virtual.

but the interviewer asked me what if you put =9 or another number. does it make a difference?

and i honestly did not know.. i just used to always assume =0 meant more or less "equal null"

 
yea, it makes all the difference. c++ handles inheritance through a jump table.. when u include runtime type-info, it uses that table to determine where to jump. the entries in the table are the offsets to the respective methods.
most compilers won't let you make that actual assignment, but if they did it may also think that the address is valid too - so it would probably allow you to instantiate the class, expecting the implementation for the particular method at address 9.
 
Originally posted by: itachi
yea, it makes all the difference. c++ handles inheritance through a jump table.. when u include runtime type-info, it uses that table to determine where to jump. the entries in the table are the offsets to the respective methods.
most compilers won't let you make that actual assignment, but if they did it may also think that the address is valid too - so it would probably allow you to instantiate the class, expecting the implementation for the particular method at address 9.



ah thanks man that makes a lot of sense. i wa swondering because i was not sure whether all compilers would have the error at all error levels.
 
I would think that = 0 is just a way of saying "pure virtual" and doesn't really have any other literal meanings (ie there's no real assignment going on). So = 9 would be invalid and would give an error.
 
Originally posted by: itachi
yea, it makes all the difference. c++ handles inheritance through a jump table.. when u include runtime type-info, it uses that table to determine where to jump. the entries in the table are the offsets to the respective methods.
most compilers won't let you make that actual assignment, but if they did it may also think that the address is valid too - so it would probably allow you to instantiate the class, expecting the implementation for the particular method at address 9.

That doesn't make any sense at all. If it was going to use 9 as the address it would have also used 0 as the address.

To know what it would do you need to pay for and read the C++ standard.
 
Originally posted by: smack Down
Originally posted by: itachi
yea, it makes all the difference. c++ handles inheritance through a jump table.. when u include runtime type-info, it uses that table to determine where to jump. the entries in the table are the offsets to the respective methods.
most compilers won't let you make that actual assignment, but if they did it may also think that the address is valid too - so it would probably allow you to instantiate the class, expecting the implementation for the particular method at address 9.

That doesn't make any sense at all. If it was going to use 9 as the address it would have also used 0 as the address.

To know what it would do you need to pay for and read the C++ standard.
not necessarily.. =0 characterizes a pure virtual function, throughout the application no reference can be made to it.. since it doesn't exist. if a reference were to be made without raising any compiler errors, the respective entry would be 0.
 
Originally posted by: xtknight
What I wonder is why they'd even bother asking a question like that.

because at the big name software company i work for, they ask crap like that in sad attempts by hans to transfer to another group with a promotion.


needless to say i did not get the promotion though they offered to let me transfer at current pay and with the promise that the work would be "exciting". i then heard from a friend in that group that it just meant there were 15 more hours of it.

haha.

yeah, but yeah... the interviewer was some indian dude with a masters. he just kept asking questions, and if you got it right, ask even a deeper more obscure ridiculous trivia. and kept asking "are you sure?", in some ploy to get me flustered i guess. lets jsut say at that point with the =0 or =9 i was totally stumped.

 
Back
Top