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 1

Go down

My C++ notes --- Part 1 Empty My C++ notes --- Part 1

Post  Admin Thu Aug 28, 2008 9:49 am

I am sharing the notes of c++ that I had made while preparing for interviews ..... Hope you like it... Do send in your comments !!!!!!


1. What is the output of printf(“%d”)?
1. %d helps to read integer data type of a given variable
2. when we write (“%d”, X) compiler will print the value of x assumed in the main
3. but nothing after (“%d”) so the output will be garbage
4. printf is an overload function doesnt check consistency of the arg list – segmentation fault
2. What will happen if I say delete this? - destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test()
1. pointer var will still be valid
2. object created by new exists until explicitly destroyed by delete
3. space it occupied can be reused by new
4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
5. delete = delete objects
6. delete[] – delete array
7. delete operator destroys the object created with new by deallocating the memory assoc. with the object
8. if a destructor has been defined fir a class delete invokes that desructor

3. Difference between C structure and C++ structure - 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 name
2. classes and structures make program modular, easier to modify make things compact
3. useful when a lot of data needs to be grouped together
4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
5. members of a struct in C are by default public, in C++ private
6. 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 @ the same time
7. pointers can point to struct:
8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
9. 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 visible
10. structs usually used for data only structures, classes for classes that have procedures and member functions
11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object
12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
13. 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
4. Difference between assignment operator and copy constructor - -assignment operator = assigning a variable to a value - copy constructor
1. constructor with only one parameter of its same type that assigns to every nonstatic class member variable of the object a copy of the passed object
2. copy assignment operator must correctly deal with a well constructed object - but copy constructor initializes uninitialized memory
3. copy constructor takes care of initialization by an object of the same type x
4. for a class for which the copy assignment and copy constructor not explicitly declared missing operation will be generated by the compiler. Copy operations are not inherited - copy of a class object is a copy of each member
5. memberwise assignment: each member of the right hand object is assigned to the corresponding member of the left hand object
6. if a class needs a copy constructor it will also need an assignment operator
7. copy constructor creates a new object, assignment operator has to deal w/ existing data in the object
8. assignment is like deconstruction followed by construction
9. assignment operator assigns a value to a already existing object
10. copy constructor creates a new object by copying an existing one
11. copy constructor initializes a freshly created object using data from an existing one. It must allocate memory if necessary then copy the data
12. the assignment operator makes an already existing object into a copy of an existing one.
13. copy constructor always creates a new object, assignment never does
5. 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
6. 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
7. 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
8. 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)
9. 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
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