BuzzTalk
Would you like to react to this message? Create an account in a few clicks or log in to continue.

My C++ notes --- Part 2

Go down

My C++ notes --- Part 2 Empty My C++ notes --- Part 2

Post  Admin Thu Aug 28, 2008 9:49 am

10. 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
11. Different types of polymorphism
1. types related by inheritance as polymorphic types because we can use many forms of a derived or base type interchangeably
2. only applies to ref or ptr to types related by inheritance.
3. Inheritance - lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different
4. derived classes
1. can use w/o change those operations that dont depend on the specifics of the derived type
2. redefine those member functions that do depend on its type
3. derived class may define additional members beyond those it inherits from its base class.
5. Dynamic Binding - lets us write programs that use objects of any type in an inheritance hierarchy w/o caring about the objects specific types
6. happens when a virtual function is called through a reference || ptr to a base class
7. The fact that a reference or ptr might refer to either a base or derived class object is the key to dynamic binding
8. calls to virtual functions made though a reference/ptr resolved @ runtime
9. the function that is called is the one defined by the actual type of the object to which ref/ptr refers
12. How to implement virtual functions in C - keep function pointers in function and use those function ptrs to perform the operation
13. What are the different type of Storage classes?
1. automatic storage: stack memory - static storage: for namespace scope objects and local statics
2. free store: or heap for dynamically allocated objects == design patterns
14. What is a namespace?
1. every name defined in a global scope must be unique w/in that scope
2. name collisions: same name used in our own code or code supplied to us by indie producers == namespace pollution
3. name clashing - namespace provides controlled mechanism for preventing name collisions
4. allows us to group a set of global classes/obj/funcs
5. in order to access variables from outside namespace have to use scope :: operator
6. 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
15. Types of STL containers - containers are objects that store other objects and that has methods for accessing its elements - has iterator - vector
16. 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
17. Write a program that will delete itself after execution. Int main(int argc, char **argv) { remove(argv[0]);return 0;}
18. What are inline functions?
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
19. What is strstream? defines classes that support iostreams, array of char obj
20. Passing by ptr/val/refArg?
1. 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
2. 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
3. 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
4. void c::f(const int& arg) - -by reference, int provided in main call cant be changed, read only. Combines safety with efficacy.
5. 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
6. void c::f(int *arg) – by reference changing *arg will change the I in the calling function
7. void c::f(const int *arg) – by reference but this time the I int in the main function cant be changed – read only
8. 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
9. void c::f(const int * const arg) by reference the pointer arg cant be changed neither can what it points to
21. Mutable keyword?
1. keyword is the key to make exceptions to const
2. mutable data member is allowed to change during a const member function
3. mutable data member is never const even when it is a member of a const object
4. a const member function may change a mutable member
22. Something you can do in C but not in C++? C++ applications generally slower at runtime and compilation - input/output
23. Difference between calloc and malloc?
1. malloc: allocate s bytes
2. calloc: allocate n times s bytes initialized to 0
24. What will happen if I allocate memory using new & free memory using free? WRONG
25. Difference between printf and sprintf?
1. sprintf: a function that puts together a string, output goes to an array of char instead of stdout
2. printf: prints to stdout
26. map in STL?
1. used to store key - value pairs, value retrieved using the key
2. store data indexed by keys of any type desire instead of integers as with arrays
3. maps are fast 0(log(n)) insertion and lookup time
4. std::map<key_type, data_type>EX:Std::map<string, char> grade_list //grade_list[“john”] = b
27. When will we use multiple inheritance?
1. use it judiciously class
2. when MI enters the design scope it becomes possible to inherit the same name (function/typedef) from more than one base class == ambiguity
3. C++ first identifies the function thats the best match for the call
4. C++ resolves calls to overload before accessibility, therefore the accessibility of Elec_Gadget() checkout is never evaluated because both are good matches == ERROR
5. resolve ambiguity mp.Borrowable_Item::checkOut(); mp.Elec_Gadget::checkOut(); //error because trying to access private
6. deadly MI diamond: anytime you have an inheritance hierarchy w/ more than one path between a base class and a derived classEX:FileInput File Output FileIOFile//File and IOFile both have paths through InputFile and OutputFile
28. Multithreading - C++ does not have a notion of multithreading, no notion of concurrency
29. Why is the pre-increment operator faster than the post-increment operator? pre is more efficient that post because for post the object must increment itself and then return a temporary containing its old value. True for even built in types
30. What is a hash and what would you use it for?
31. What is a dot product and what is a cross product - what would you use them for?
32. What is 2 ^ 101?
Admin
Admin
Admin

Number of posts : 37
Registration date : 2008-08-08

https://buzztalk.1talk.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum