C++ Interview Questions For Freshers
1. What are the different data types present in C++?
The 4 data types in C++ are given below:
- Primitive Datatype(basic datatype). Example- char, short, int, float, long, double, bool, etc.
- Derived datatype. Example- array, pointer, etc.
- Enumeration. Example- enum
- User-defined data types. Example- structure, class, etc.
2. What is the difference between C and C++?
The main difference between C and C++ are provided in the table below:
C |
C++ |
C is a procedure-oriented programming language. |
C++ is an object-oriented programming language. |
C does not support data hiding. |
Data is hidden by encapsulation to ensure that data structures and
operators are used as intended. |
C is a subset of C++ |
C++ is a superset of C. |
Function and operator overloading are not supported in C |
Function and operator overloading is supported in C++ |
Namespace features are not present in C |
Namespace is used by C++, which avoids name collisions. |
Functions can not be defined inside structures. |
Functions can be defined inside structures. |
calloc() and malloc() functions are used for memory allocation and
free() function is used for memory deallocation. |
new operator is used for memory allocation and deletes operator is
used for memory deallocation. |
3. What are class and object in C++?
A class is a user-defined data type that has data members and member functions. Data members are the data variables and member functions are the functions that are used to perform operations on these variables.
An object is an instance of a class. Since a class is a user-defined data type so an object can also be called a variable of that data type.
A class is defined as-
class A{
private:
int data;
public:
void fun(){
}
};
Class and Object in C++
For example, the following is a class car that can have properties like name, color, etc. and they can have methods like speed().
4. What is the difference between struct and class?
In C++ a structure is the same as a class except for a few differences like security. The difference between struct and class are given below:
Structure |
Class |
Members of the structure are public by default. |
Members of the class are private by default. |
When deriving a struct from a class/struct, default access specifiers
for base class/struct are public. |
When deriving a class, default access specifiers are private. |
5. What is operator overloading?
Operator Overloading is a very essential element to perform the operations on user-defined data types. By operator overloading we can modify the default meaning to the operators like +, -, *, /, <=, etc.
For example -
The following code is for adding two complex number using operator overloading-
class complex{
private:
float r, i;
public:
complex(float r, float i){
this->r=r;
this->i=i;
}
complex(){}
void displaydata(){
cout<<”real part = “<<r<<endl;
cout<<”imaginary part = “<<i<<endl;
}
complex operator+(complex c){
return complex(r+c.r, i+c.i);
}
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
}
6. What is polymorphism in C++?
Polymorphism in simple means having many forms. Its behavior is different in different situations. And this occurs when we have multiple classes that are related to each other by inheritance.
For example, think of a base class called a car that has a method called car brand(). Derived classes of cars could be Mercedes, BMW, Audi - And they also have their own implementation of a cars
The two types of polymorphism in C++ are:
Compile Time Polymorphism
Runtime Polymorphism
Polymorphism in C++
7. Explain constructor in C++
The constructor is a member function that is executed automatically whenever an object is created. Constructors have the same name as the class of which they are members so that the compiler knows that the member function is a constructor. And no return type is used for constructors.
Example:
class A{
private:
int val;
public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);
return 0;
}
8. Tell me about virtual function
Virtual function is a member function in the base class that you redefine in a derived class. A virtual function is declared using the virtual keyword. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.
9. Compare compile time polymorphism and Runtime polymorphism
The main difference between compile-time and runtime polymorphism is provided below:
Compile-time polymorphism |
Run time polymorphism |
In this method, we would come to know at compile time which method
will be called. And the call is resolved by the compiler. |
In this method, we come to know at run time which method will be
called. The call is not resolved by the compiler. |
It provides fast execution because it is known at the compile time. |
It provides slow execution compared to compile-time polymorphism
because it is known at the run time. |
It is achieved by function overloading and operator overloading. |
It can be achieved by virtual functions and pointers. |
Example - int add(int a, int b){ return a+b; } int add(int a, int b, int
c){ return a+b+c; }
int main(){ cout<<add(2,3)<<endl; cout<<add(2,3,4)<<endl;
return 0; } |
Example - class base{ public: void fun(){ cout<<”base “; } }; class derived: public base{ public: void fun(){ cout<<”derived ”; } }; int main(){ A *a=new B; a->fun();
return 0; } |
10. What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it is declared as friends.
Like friend class, friend function can also access private, protected, and public members. But, Friend functions are not member functions.
For example -
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
Read More :- MNC Interview question
Really thanks for it ....
ReplyDeleteThanks for such good questions
ReplyDelete