Technical Interview Questions and Answers :: C++
11 / 187
Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.
Class: Class is a successor of Structure. By default all the members inside the class are private.
12 / 187
Ø Public, protected and private are three access specifier in C++.
Ø Public data members and member functions are accessible outside the class.
Ø Protected data members and member functions are only available to derived classes.
Ø Private Data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.
13 / 187
A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associatied class is created.
It is called constructor because it construct the values of data members of the class.
14 / 187
a. default constructor(or)empty constructor
b. default argument constructor
c. constructor polymorphism(or)multiple constructor(or)constructor overloading
d. copy constructor
e. parameterized constructor.
15 / 187
When there is more than one constructor in a class with different parameters and with different data types we say that the constructor is overloaded.
16 / 187
Or
What is multiple constructor?
The sharing the same name by two or more functions is referred to as function overloading. Similarly when more than one constructor function is defined in a class. We say that the constructor is overloaded. It is also called multiple constructors.
Example :
Complex() { } //constructor with no arg.
Complex (float a) { x=y=a;} //constructor with one arg.
Complex (float real, float imag) //constructor with two arg.
17 / 187
Whenever new is used to allocate memory in the constructors. We should use delete to free that memory. For example, the destructor for the matrix class discussed above may; be defined as follows
Matrix :: ~matrix( )
{
for(int i=0;i
delete p;
}
18 / 187
Constructor:
1. The constructor is a ‘special’ member function whose task is to initialize the objects of its class.
2. It constructs the values of data members of the class
Destructor:
1. The destructor is a member function whose name is the same as the class name but is preceded by a tilde.
2. It destroys the objects that have been created by a constructor.
19 / 187
The constructor can also be used to allocate memory while creating objects.
This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size, thus resulting in the saving of memory;. Allocation of memory to object at the time of their construction is known as a dynamic construction of objects. The memory is allocated with the help of the new operator. The use of new in constructors that are used to construct strings in objects.
20 / 187
It is possible to define constructors with default arguments. for example, the constructor complex( ) can be declared as follows:
Complex(float real, float imag=0)
The default value of the argument imag is zero.then the statement
Complex c(5.0);
Assigns the value 5.0 to the real variable and 0.0 to imag (by default). However, the statement
Complex c(2.0,3.0);
Assigns 2.0 to real and 3.0 to imag. The actual parameter, when specified, overrides the default value. As pointed out earlier, the missing arguments must be the trailing ones.