Professional Documents
Culture Documents
Interview Questions
Interview Questions
Answer: C++ allows classes to inherit some of the commonly used state and behavior from other
classes. This process is known as inheritance.
As the inlining is a request, not a command, the compiler can ignore it.
#include
int main()
sum += numbers[i];
return 0;
#include
class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};
public:
};
int main(void) {
return 0;
In the aforementioned program bp is a pointer of type Base. A call to bp->show() calls show()
function of the Derived class. This is because bp points to an object of the Derived class.
Question: Take a look at the following two code examples for printing a vector:
Sample Code 1:
vector vec;
/* ... .. ... */
itr->print();
Sample Code 2:
vector vec;
/* ... .. ... */
itr->print();
#include
int main()
int num;
float* ptr;
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
delete [] ptr;
return 0;
Question: What is a mutable storage class specifier? How can they be used?
Answer: A mutable storage class specifier is applied only on non-static and non-constant
member variable of the class. It is used for altering the constant class object's member by
declaring it. This can be done by using a storage class specifier.
Question: What are the differences between a shallow copy and a deep copy?
Answer: The differences between a shallow copy and a deep copy can be stated as under.
It allows memory dumping on a bit by bit basis from It allows the copy field, which is done by field from
one object to another. one object to another.
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
Question: What is the main difference between the keyword struct and class?
Answer: The keyword struct is used for resembling public members by default, while the
keyword class is used for resembling private members by default.
#include <iostream>
using namespace std;
int my_var = 0;
int main(void) {
int my_var = 0;
::my_var = 1; // set global my_var to 1
my_var = 2; // set local my_var to 2
cout << ::my_var << ", " << my_var;
return 0;
}
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction: It is used for hiding the internal implementations and displays only the
required details to the user. Abstraction can be implemented with the help of abstract
class or interface. For e.g. Calculator. In the calculator, it shows only output whatever
input is given. How the calculations are done, that is not necessary detail for the user.
and operations together and hides that details from the user. Encapsulation can be
achieved with the help of access specifiers that are public, private and protected in C+
+. With the help of them, we can provide access or prevent the access directly to a
user.
classes. It means to inherit the properties of the parent class by the child class. The
parent class is also called as Base class and child class is also referred to as a Derived
class. Inheritance is used mainly for code reusability and to extend the parent classes.
function is called, it will execute the different function depending on the type of
are used to set the accessibility of classes, function, methods and other members.
There are only 3 access specifiers for C++ that are: 1. Public 2. Private 3. Protected
outside the class. They can be accessed only inside the class.
Protected: The members or fields that are declared as protected can be
accessed outside the class but only in a class derived from that class.
1 It can have an instance method and can have an It is implicitly abstract and cannot
implementation. implementation.
2 It can extend other class and multiple interfaces. It can extend the interface only.
protected.
Inheritance explained earlier, please check question no. 2. There are different types of
Single Inheritance: It refers to the inheritance when there is only one parent
Data types are used to define the type of variable and accordingly, OS allocates the
memory space and what can be stored. Variables are declared for which the value will
be assigned and while declaring it reserves the memory space to hold the values. For
e.g. int X, in this example, ‘int’ is data type and ‘X’ is variable. Some data type
The constructor has been executed when the new objects of the class are created. It
will have the same name exactly as the class name. A constructor can be the default
constructor, which is not required to mention and it does not have any parameter. A
Destructor has been executed when the objects of a class are not in scope or it’s no
longer in use. It will have the same name exactly as the class name but should be
prefixed with (~) sign. It is mainly used for releasing resources like a closing of files,
etc.
This is the advanced C++ interview questions that are asked in an interview. It is
mainly used to replace the implementation of the parent class. These are the functions
that are declared in class with the keyword ‘virtual’. When the parent class type
reference is initialized with an object of child class type and overridden method
(declared as virtual) is invoked using the parent class reference then the method of
S.No. C C++
5. Scanf() and printf()are used for input and output cin and cout is used for the same.