Technical Interview Questions and Answers :: C++
1 / 187
Two ways to pass values as arguments to the constructor function when an object is declared.
1. By calling the constructor explicitly.
2. By calling the constructor implicitly.
Eg :
Explicit call:
Integer int1=integer(0,200);
Implicit call:
Integer int1(0,100);
2 / 187
It may be necessary to initialize the various data elements of different objects with different values when they are created.
C++ permits us to achieve this objective by passing an argument to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.
Eg:
Class A
{
int m,n;
public:
A(int x,int y); //parameterized constructor
………..
………..
};
A : : A(int x,int y)
{
m=x;
n=y;
}
3 / 187
A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A::A ( ). If no such constructor is defined, then the compiler supplies a default constructor. Therefore a statement such as A a;
Invokes the default constructor of the compiler to create the object a.
4 / 187
The constructor functions have some characteristics:
• They should be declared in the public section.
• They got invoked automatically when the objects are created.
• They do not have return types, not even void and therefore, they cannot return values.
• They cannot be inherited, through a derived class can call the base class constructor.
• Like other c++ functions, they can have default arguments.
• Constructors cannot be virtual.
• We cannot refer to their addresses.
• An object with a constructor cannot be used as a member of a union.
• They make implicit calls to the operators new and delete when memory allocation is required.
When a constructor is declared for a class, initialization of the class objects becomes mandatory.
5 / 187
• They should be declared in the public section.
• They got invoked automatically when the objects are created.
• They do not have return types, not even void and therefore, they cannot return values.
• They cannot be inherited, through a derived class can call the base class constructor.
• Like other c++ functions, they can have default arguments.
• Constructors cannot be virtual.
• We cannot refer to their addresses.
• An object with a constructor cannot be used as a member of a union.
• They make implicit calls to the operators new and delete when memory allocation is required.
• When a constructor is declared for a class, initialization of the class objects becomes mandatory.
6 / 187
A constructor is declared and defined as
Class integer
{
int m,n;
public:
integer(void); //constructor declared
……….
………..
};
integer : : integer(void)//constructor defined
{
m=0;
n=0;
}
7 / 187
C++ strongly supports the concept of reusability. The c++ classes can be reused in several ways .Once a class has been written and tested, it can be adapted by other programmers to suit their requirement .this done by creating new class reusing the properties of the existing ones is called reusability.
8 / 187
A derived class can be defined by specifying its relationship with the base class in addition to its own details. the general form of defining a derived class is
Class derived_class_name
{
--------------
--------------//members of derived class
--------------
};
9 / 187
Visibility mode is optional and may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
Ex:
Class abc: private Xyz // private derivation
{
members of abc;
};
Ex:
Class abc: private Xyz // public derivation
{
members of abc;
};
10 / 187
When a function is made virtual, c++ determines which function to use at run-time based on the type of the object pointed to by the base pointer, rather than the type of the pointer. By making the base pointer to point to different objects, we execute different versions of the virtual function.