C++ Interview Questions Part 6

C++ interview question: What are virtual functions? 
Answer: C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.
C++ virtual function is, 
* A member function of a class 
* Declared with virtual keyword 
* usually has a different functionality in the derived class 
* A function call is resolved at run-time 

C++ interview question: We can overload assignment operator as a normal function.But we can not overload assignment operator as friend function why?
Answer :If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend fucntionThus all operator such as =, *=, +=, etc are naturally defined as member functions not friend functions Conversely, if the operator does not modify any of its operands, but needs only a representation of the object, it does not have to be a member function and often less confusing. This is the reason why binary operators are often implemented as friend functions such as + , *, -, etc..


C++ interview question: What is the difference between class and structure? 
Answer: 1:By default, the members of structures are public while that for class is private
2: structures doesn't provide something like data hiding which is provided by the classes
3: structures contains only data while class bind both data and member functions 

C++ interview question: What is virtual class and friend class? 
Answer: Friend classes are used when two or more classes are designed to work together and virtual base class aids in multiple inheritance.

C++ interview question: Is there any way to write a class such that no class can be inherited from it. Please include code 
Answer: Simple, make all constructors of the class private.

C++ interview question: Why can’t we overload the sizeof, :?, :: ., .* operators in c++ 
Answer: The restriction is for safety. For example if we overload. Operator then we can’t access member in normal way for that we have to use ->.

C++ interview question :What is importance of const. pointer in copy constructor? 
Answer :Because otherwise you will pass the object to copy as an argument of copy constructor as pass by value which by definition creates a copy and so on... an infinite call chain

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP