Most FAQ C C++ Satyam Aptitude questions
- Different types of polymorphism?
- types related by inheritance as polymorphic types because we can use many forms of a derived or base type interchangeably
- only applies to ref or ptr to types related by inheritance.
- Inheritance - lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different
- derived classes
- can use w/o change those operations that dont depend on the specifics of the derived type
- redefine those member functions that do depend on its type
- derived class may define additional members beyond those it inherits from its base class.
- can use w/o change those operations that dont depend on the specifics of the derived type
- Dynamic Binding - lets us write programs that use objects of any type in an inheritance hierarchy w/o caring about the objects specific types
- happens when a virtual function is called through a reference || ptr to a base class
- The fact that a reference or ptr might refer to either a base or derived class object is the key to dynamic binding
- calls to virtual functions made though a reference/ptr resolved @ runtime
- the function that is called is the one defined by the actual type of the object to which ref/ptr refers
- types related by inheritance as polymorphic types because we can use many forms of a derived or base type interchangeably
- How to implement virtual functions in C - keep function pointers in function and use those function ptrs to perform the operation
- What are the different type of Storage classes?
- automatic storage: stack memory - static storage: for namespace scope objects and local statics
- free store: or heap for dynamically allocated objects == design patterns
- automatic storage: stack memory - static storage: for namespace scope objects and local statics
- What is a namespace?
- every name defined in a global scope must be unique w/in that scope
- name collisions: same name used in our own code or code supplied to us by indie producers == namespace pollution
- name clashing - namespace provides controlled mechanism for preventing name collisions
- allows us to group a set of global classes/obj/funcs
- in order to access variables from outside namespace have to use scope :: operator
- using namespace serves to assoc the present nesting level with a certain namespace so that objectand funcs of that namespace can be accessible directly as if they were defined in the global scope
- every name defined in a global scope must be unique w/in that scope
- Types of STL containers - containers are objects that store other objects and that has methods for accessing its elements - has iterator - vector
- Difference between vector and array - -array: data structure used dto store a group of objects of the same type sequentially in memory - vector: container class from STL - holds objects of various types - resize, shrinks grows as elements added - bugs such as accessing out of bounds of an array are avoided
- Write a program that will delete itself after execution. Int main(int argc, char **argv) { remove(argv[0]);return 0;}
- What are inline functions?
- treated like macro definitions by C++ compiler
- meant to be used if there's a need to repetitively execute a small block if code which is smaller
- always evaluates every argument once
- defined in header file
- avoids function call overload because calling a function is slower than evaluating the equivalent expression
- it's a request to the compiler, the compiler can ignore the request
- treated like macro definitions by C++ compiler
- What is strstream? defines classes that support iostreams, array of char obj
- Passing by ptr/val/refArg?
- passing by val/refvoid c::f(int arg) – by value arg is a new int existing only in function. Its initial value is copied from i. modifications to arg wont affect the I in the main function
- void c::f(const int arg) – by value (i.e. copied) the const keyword means that arg cant be changed, but even if it could it wouldnt affect the I in the main function
- void c::f(int& arg) - -by reference, arg is an alias for I. no copying is done. More efficient than methods that use copy. Change in arg == change in I in the calling function
- void c::f(const int& arg) - -by reference, int provided in main call cant be changed, read only. Combines safety with efficacy.
- void c::f(const int& arg) const – like previous but final const that in addition the function f cant change member variables of cArg passing using pointers
- void c::f(int *arg) – by reference changing *arg will change the I in the calling function
- void c::f(const int *arg) – by reference but this time the I int in the main function cant be changed – read only
- void c::f(int * const arg) – by reference the pointer arg cant be changed but what it points to (namely I of the calling function) can
- void c::f(const int * const arg) by reference the pointer arg cant be changed neither can what it points to
- passing by val/refvoid c::f(int arg) – by value arg is a new int existing only in function. Its initial value is copied from i. modifications to arg wont affect the I in the main function
No comments:
Post a Comment