Sunday, May 18, 2008

C C++ overloading and overriding Most Faq

C C++ overloading and overriding Most Faq

1.Difference between overloading and overriding?
1. Overload - two functions that appear in the same scope are overloaded if they have the same name but have different parameter list
2. main() cannot be overloaded
3. notational convenience - compiler invokes the functions that is the best match on the args – found by finding the best match between the type of arg expr and parameter
4. if declare a function locally, that function hides rather than overload the same function declared in an outer scope
5. Overriding - the ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass
6. the overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class
7. writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature
8. all functions in the derived class hide the base class functions with the same name except in the case of a virtual functions which override the base class functions with the same signature
2.
Virtual
1. single most important feature of C++ BUT virtual costs
2. allows derived classes to replace the implementation provided by the base class
3. without virtual functions C++ wouldnt be object oriented
4. Programming with classes but w/o dynamic binding == object based not OO
5. dynamic binding can improve reuse by letting old code call new code
6. functions defined as virtual are ones that the base expects its derived classes to redefine
7. virtual precedes return type of a function
8. virtual keyword appears only on the member function declaration inside the class
9. virtual keyword may not be used on a function definition that appears outside the class body
10. default member functions are nonvirtual
3.
Dynamic Binding
1. delaying until runtime the selection of which function to run
2. refers to the runtime choice of which virtual function to run based on the underlying type of the object to which a reference or a pointer is based
3. applies only to functions declared as virtual when called thru reference or ptr
4. in C++ dynamic binding happens when a virtual function is called through a reference (|| ptr) to a base class. The face that ref or ptr might refer to either a base or a derived class object is the key to dynamic binding. Calls to virtual functions made thru a reference or ptr are resolved at run time: the function that is called is the one defined by the actual type of the object to which the reference or pointer refers
4.
Explain the need for a virtual destructor
1. destructor for the base parts are invoked automatically
2. we might delete a ptr to the base type that actually points to a derived object
3. if we delete a ptr to base then the base class destructor is run and the members of the base class are cleared up. If the objectis a derived type then the behavior is undefined
4. to ensure that the proper destructor is run the destructor must be virtual in the base class
5. virtual destructor needed if base pointer that points to a derived object is ever deleted (even if it doesnt do any work)
5.
Rule of 3
1. if a class needs a destructor, it will also need an assignment operator and copy constructor
2. compiler always synthesizes a destructor for us
3. destroys each nonstatic member in the reverse order from that in which the object was created
4. it destroys the members in reverse order from which they are declared in the class1. if someone will derive from your class2. and if someone will say new derived where derived is derived from your class3. and if someone will say delete p, where the actual objects type is derived but the pointer ps type is your class
5. make destructor virtual if your class has any virtual functions
6. Why do you need a virtual destructor when someone says delete using a Base ptr thats pointing @ a derived object? - when you say delete p and the class of p has a virtual destructor the destructor that gets invoked is the one assoc with the type of the object*p not necessarily the one assoc with the type of the pointer == GOOD

0 comments: