Commonly Asked C++ Interview Questions - Set 1: This This
Commonly Asked C++ Interview Questions - Set 1: This This
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
int main(void) {
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}
Run on IDE
Output:
In Derived
Private: Members declared as private are accessible only within the same class and they
cannot be accessed outside the class they are declared. Child classes are also not allowed to
access private members of parent.
Public: Members declared as public are accessible from anywhere.
Protected: Only the class and its child classes can access protected members.
Do you know What happens when more restrictive access is given to a derived class
method in C++?
Q. Major C++ features
Class: Class is a blueprint of data and functions or methods. Class does not take any space.
Object: Objects are basic run-time entities in an object oriented system, objects are instances
of a class these are defined user defined data types.
Encapsulation and Data abstraction: Wrapping up(combining) of data and functions into
a single unit is known as encapsulation. The data is not accessible to the outside world and only
those functions which are wrapping in the class can access it. This insulation of the data from
direct access by the program is called data hiding or information hiding.
Data abstraction – providing only needed information to the outside world and hiding
implementation details. For example, consider a class Complex with public functions as getReal()
and getImag(). We may implement the class as an array of size 2 or as two variables. The
advantage of abstractions is, we can change implementation at any point, users of Complex class
wont’t be affected as our method interface remains same. Had our implementation be public, we
would not have been able to change it.
Inheritance: Inheritance is the process by which objects of one class acquire the properties of
objects of another class. It supports the concept of hierarchical classification. Inheritance provides
reusability. This means that we can add additional features to an existing class without modifying
it.
Polymorphism: Polymorphism means ability to take more than one form. An operation may
exhibit different behaviors in different instances. The behavior depends upon the types of data
used in the operation.
Dynamic Binding: In dynamic binding, the code to be executed in response to function call is
decided at runtime. C++ has virtual functions to support this.
Message Passing: Objects communicate with one another by sending and receiving
information to each other. A message for an object is a request for execution of a procedure and
therefore will invoke a function in the receiving object that generates the desired results. Message
passing involves specifying the name of the object, the name of the function and the information
to be sent.
Q. Structure vs class in C++
In C++, a structure is same as class except the following differences:
Members of a class are private by default and members of struct are public by
default.
When deriving a struct from a class/struct, default access-specifier for a base
class/struct is public. And when deriving a class, default access specifier is
private.
Q. Malloc() vs new / Delete vs Free
Following are the differences between malloc() and operator new.
new is an operator, while malloc() is a function.
new returns exact data type, while malloc() returns void *.
new calls constructors( class instances are initalized and deinitialized automatically), while
malloc() does not( classes won’t get initalized or deinitialized automatically
Syntax:
1. int *n = new int(10); // initialization with new()
2. str = (char *) malloc(15); //malloc()
free( ) is used on resources allocated by malloc( ), or calloc( ) in C
Delete is used on resources allocated by new in C++
Q. Inline Functions
C++ provides an inline functions to reduce the function call overhead. Inline function is a function that
is expanded in line when it is called. When the inline function is called whole code of the inline function
gets inserted or substituted at the point of inline function call. This substitution is performed by the C++
compiler at compile time. Inline function may increase efficiency if it is small.
The syntax for defining the function inline is:
inline return-type function-name(parameters)
{
// function code
}
Remember, inlining is only a request to the compiler, not a command. Compiler can ignore
the request for inlining.
Q.Friend class and function in C++
A friend class can access private and protected members of other class in which it is declared as friend.
It is sometimes useful to allow a particular class to access private members of other class. For example
a LinkedList class may be allowed to access private members of Node.
Friend Function Like friend class, a friend function can be given special grant to access private and
protected members. A friend function can be:
a) A method of another class
b) A global function
Note: Overloading of functions with different return types are not allowed.
Operating overloading allows us to make operators to work for user defined classes. For example, we
can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using
+.
Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional
Number, Big Integer, etc.
Q. Copy Constructor
A copy constructor is a member function which initializes an object using another object of the same
class. A copy constructor has the following general function prototype: ClassName (const ClassName
&old_obj);
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
When is copy constructor called?
In C++, a Copy Constructor may be called in following cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a function) by value as an argument.
3. When an object is constructed based on another object of the same class.
4. When compiler generates a temporary object.
Can we make copy constructor private?
Yes, a copy constructor can be made private
Q.What Is Inheritance?
Different kinds of objects often have a certain amount in common with each other. Yet each also defines
additional features that make them different. Object-oriented programming allows classes to inherit
commonly used state and behavior from other classes