This is not really a set of interview questions, a reader sent TechInterviews.com what looks like a copy of his notes from C++ class that describes various tricks and code for C++.
- What is the output of printf("%d")?
- %d helps to read integer data type of a given variable
- when we write ("%d", X) compiler will print the value of x assumed in the main
- but nothing after ("%d") so the output will be garbage
- printf is an overload function doesnt check consistency of the arg list – segmentation fault
- %d helps to read integer data type of a given variable
- 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()
- pointer var will still be valid
- object created by new exists until explicitly destroyed by delete
- space it occupied can be reused by new
- delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
- delete = delete objects
- delete[] – delete array
- delete operator destroys the object created with new by deallocating the memory assoc. with the object
- if a destructor has been defined fir a class delete invokes that desructor
- pointer var will still be valid
- Difference between C structure and C++ structure - C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++
- structures are a way of storing many different values in variables of potentially diff types under under the same name
- classes and structures make program modular, easier to modify make things compact
- useful when a lot of data needs to be grouped together
- struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
- members of a struct in C are by default public, in C++ private
- 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
- pointers can point to struct:
- C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
- 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
- structs usually used for data only structures, classes for classes that have procedures and member functions
- use private because in large projects important that values not be modified in an unexpected way from the POV of the object
- advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
- 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
- structures are a way of storing many different values in variables of potentially diff types under under the same name
- Difference between assignment operator and copy constructor - -assignment operator = assigning a variable to a value - copy constructor
- 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
- copy assignment operator must correctly deal with a well constructed object - but copy constructor initializes uninitialized memory
- copy constructor takes care of initialization by an object of the same type x
- 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
- memberwise assignment: each member of the right hand object is assigned to the corresponding member of the left hand object
- if a class needs a copy constructor it will also need an assignment operator
- copy constructor creates a new object, assignment operator has to deal w/ existing data in the object
- assignment is like deconstruction followed by construction
- assignment operator assigns a value to a already existing object
- copy constructor creates a new object by copying an existing one
- copy constructor initializes a freshly created object using data from an existing one. It must allocate memory if necessary then copy the data
- the assignment operator makes an already existing object into a copy of an existing one.
- copy constructor always creates a new object, assignment never does
- 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