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
Differentiate between “new” and “malloc” ?
1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete].
2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”.
3.) “new” will initialize the new memory to 0 but “malloc()” gives random value in the new alloted memory location
What is Constructor ? How it is called ?
Answer :
Constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.Ways of calling constructor:
1) Implicitly: automatically by complier when an object is created.
2) Calling the constructors explicitly is possible, but it makes the code unverifiable.
Difference between C structure and C++ structure?
Answer :
C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++
1. structures are a way of storing many different values in variables of potentially diff types under under the same name2. classes and structures make program modular, easier to modify make things compact3. useful when a lot of data needs to be grouped together4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure5. members of a struct in C are by default public, in C++ private6. unions like structs except they share memory – allocates largest data type in memory - like a giant storage: store one small OR one large but never both at the same time7. pointers can point to struct:8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible10. structs usually used for data only structures, classes for classes that have procedures and member functions11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming
What are inline functions?
Answer :
1. Treated like macro definitions by C++ compiler.
2. Meant to be used if there’s a need to repetitively execute a small block if code which is smaller.
3. Always evaluates every argument once.
4. Defined in header file.
5. Avoids function call overload because calling a function is slower than evaluating the equivalent expression.
6. It’s a request to the compiler, the compiler can ignore the request.
Difference between calloc and malloc?
Answer :
malloc: allocate n bytescalloc: allocate m times n bytes initialized to 0
In C, why is the void pointer useful? When would you use it?
Answer :
The void pointer is useful becuase it is a generic pointer that any pointer can be cast into and back again without loss of information.
In C, what is the difference between a static variable and global variable?
Answer :
A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).
What is polymorphism?
Answer :
Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.
Wednesday, February 7, 2007
Subscribe to:
Posts (Atom)