C++ Programming Question and Answers

11. What are the advantages of inheritance?
  • It permits code reusability.
  • Reusability saves time in program development.
  • It encourages the reuse of proven and debugged high quality software, thus reducing problem after a system becomes functional.
Your Name Your Email-ID
Your Answer
12. Explain virtual class and friend class?
  Virtual Base Class:
  • It is used in context of multiple inheritance in C++. If you want to derive two classes from a class, and further derive one class from the two classes in the second level, you need to declare the uppermost base class as "virtual" in the inherited classes. These prevent multiple copies of the uppermost base class data members when an object of the class at the third level of hierarchy is created.
Friend Class:
  • When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend class member methods.
Your Name Your Email-ID
Your Answer
13. What are the differences between references and pointers?
  • Both references and pointers can be used to change local variables of one function inside another function. Both of them can also to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency again.
References are less powerful than pointers
  • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
  • References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing.
  • A reference must be initialized when declared. There is no such restriction with pointers.
References are safer and easier to use.
  • Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don't refer to a valid location.
  • Easier to use: References don't need referencing operator to access the value. They can be used like normal variables. '&' operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator ('.'), unlike pointers where arrow operator (->) is needed to access members.
Your Name Your Email-ID
Your Answer
14. Explain deep copy and a shallow copy?
  Deep copy:
  • It involves using the contents of one object to create another instance of the same class. Here, the two objects may contain the same information but the target object will have its own buffers and resources. The destructor of either object will not affect the remaining object.
Shallow copy:
  • It involves the contents of one object into another instance of the same class. This creates a mirror image. The two objects share the same externally contained contents of the other object to be unpredictable. This happens because of the straight copying of references and pointers.
Your Name Your Email-ID
Your Answer
15. What is virtual class and friend class?
  • Friend class are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private.
  • For instance, it may be desirable for class. Database Cursor to have more privilege to the internal of class Database than main() has.
Your Name Your Email-ID
Your Answer

131415 Page 3 of 15