C++ Interview
C++ Interview
C++ Interview
A class in C++ can be defined as a collection of function and related data under a single name. It
is a blueprint of objects. A C++ program can consist of any number of classes.
By using the keyword class followed by identifier (name of class) you can specify the class in
C++.
Inside curly brackets, body of the class is defined. It is terminated by semi-colon in the end.
For example,
class name{
/ / some data
/ / some functions
1 For example,
2 class name{
3 / / some data
4 / / some functions
5 };
To run the C++ application it involves two steps, the first step is a compilation where conversion
of C++ code to object code take place. While second step includes linking, where combining of
object code from the programmer and from libraries takes place. This function is operated by
main () in C++ language.
Class gives blueprints for object, so basically an object is created from a class or in other words
an object is an instance of a class. The data and functions are bundled together as a self-
contained unit called an object. Here, in the example A and B is the Object.
For example,
Class Student
{
Public:
Int rollno;
1 Class Student
2 {
3 Public:
4 Int rollno;
5 String name;
6 } A, B;
The member function regulates the behaviour of the class. It provides a definition for supporting
various operations on data held in the form of an object.
Namespace std; defines your standard C++ library, it consists of classes, objects and functions of
the standard C++ library. You can specify the library by using namespace std or std: : throughout
the code. Namespace is used to differentiate the same functions in a library by defining the name.
• While loop
• For loop
• Do-while loop
11) Explain what are Access specifiers in C++ class? What are the types?
Access specifiers determine the access rights for the statements or functions that follow it until
the end of class or another specifier is included. Access specifiers decide how the members of
the class can be accessed. There are three types of specifiers
• Private
• Public
• Protected
Operators are specific operands in C++ that is used to perform specific operations to obtain a
result. The different types of operators available for C++ are Assignment Operator, Compound
Assignment Operator, Arithmetic Operator, Increment Operator and so on.
For example arithmetic operators, you want to add two values a+b
#include
Using namespace std;
main ()
{
1 #include
2 Using namespace std;
3
4 main ()
5 {
6 int a= 21 ;
7 int b= 10 ;
8 int c;
9 c= a + b;
10 cout << “Line 1- Value of c is : ” << c << endl ;
11 return 0;
12 }
The string is actually a one-dimensional array of characters that is terminated by a null character
‘\0’.
For example, to type hello word
#include
Using namespace std;
Int main ()
{
1 #include
2 Using namespace std;
3 Int main ()
4 {
5 char greeting[6] = { ‘H’ , ‘e’ , ‘l’ ,’l’ , ‘o’ , ‘\0’};
6 cout << “Greeting message:” ;
7 cout << greeting << endl;
8 return 0;
9 }
Greeting message: HelloOn executing this code it will give the result like
A reference variable is just like a pointer with few differences. It is declared using & Operator.
In other words, reference is another name for an already existing variable.
Polymorphism in C++ is the ability to call different functions by using only one type of the
function call. Polymorphism is referred to codes, operations or objects that behave differently in
a different context.
For example, the addition function can be used in many contests like
Data abstraction is a technique to provide essential information to the outside world while hiding
the background details. Here in below example you don’t have to understand how cout display
the text “Hello guru99” on the user screen and at the same time implementation of cout is free to
change
For example,
#include
Using namespace std;
int main ( )
{
1 #include
2 Using namespace std;
3
4 int main ( )
5 {
6 Cout << “Hello guru99” <<endl;
7 return 0 ;
8 }
The problem that arises during execution of a program is referred as exceptional handling. The
exceptional handling in C++ is done by three keywords.
• Try: It identifies a block of code for which particular exceptions will be activated
• Catch: The catch keyword indicates the catching of an exception by an exception handler at the
place in a program
• Throw: When a problem exists while running the code, the program throws an exception
Encapsulation is an object oriented programming concept (oops) which binds together the data
and functions. It is also referred as data hiding mechanism.
• Simple functions
• Static functions
• Const functions
• Inline functions
• Friend functions
20) Mention what are the decision making statements in C++? Explain if statement with an
example?
• if statement
• switch statement
• conditional operator
For example, we want to implement if condition in C++
#include
Int main ( )
{
Int, x, y;
1 #include
2 Int main ( )
3 {
4 Int, x, y;
5 X= 10;
6 Y= 5;
7 If (x > y)
8 {
9 Cout << “x is greater than y”;
10 }
11 }
To run two or more programs simultaneously multi-threading is useful. There are two types of
Upcasting is the act of converting a sub class references or pointer into its super class reference
or pointer is called upcasting.
Pre-processors are the directives, which give instruction to the compiler to pre-process the
information before actual compilation starts.
COPY CONSTRUCTOR is a technique that accepts an object of the same class and copies its
data member to an object on the left part of the assignment.