C InterviewQuestions
C InterviewQuestions
C InterviewQuestions
© Copyright by Interviewbit
Contents
C C++
Data is hidden by
encapsulation to ensure
C does not support data hiding. that data structures and
operators are used as
intended.
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(){
}
};
For example, the following is a class car that can have properties like name, color,
etc. and they can have methods like speed().
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
Members of the structure are public
class are private by
by default.
default.
When deriving a
When deriving a struct from a
class, default access
class/struct, default access specifiers
specifiers are
for base class/struct are public.
private.
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;
}
Polymorphism in C++
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;
}
It is achieved by function
It can be achieved by virtual
overloading and operator
functions and pointers.
overloading.
Example -
Example -
class A{
int add(int a, int b){ public:
return a+b; virtual void fun(){
} cout<<"base ";
int add(int a, int b, int c){ }
return a+b+c; };
} class B: public A{
public:
int main(){ void fun(){
cout<<add(2,3)<<endl; cout<<"derived ";
cout<<add(2,3,4)<<endl; }
};
int main(){
return 0; 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;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
}
Protected: All data members and member functions are accessible inside the class
and to the derived class.
Private: All data members and member functions are not accessible outside the
class.
int x=10;
int &ref=x; //reference variable
class A{
private:
int val;
public:
A(int x){
val=x;
}
A(){
}
~A(){ //destructor
}
}
int main(){
A a(3);
return 0;
}
19. What are the static members and static member functions?
When a variable in a class is declared static, space for it is allocated for the lifetime of
the program. No matter how many objects of that class have been created, there is
only one copy of the static member. So same static member can be accessed by all
the objects of that class.
A static member function can be called even if no objects of the class exist and the
static function are accessed using only the class name and the scope resolution
operator ::
Inheritance in C++
Class Bus, Class Car, and Class Truck inherit the properties of Class Vehicle.
The most important thing about inheritance is that it permits code reusability.
class A{
int x,y;
A(int x, int y){
this->x=x;
this->y=y;
}
};
int main(){
A a1(2,3);
A a2=a1; //default copy constructor is called
return 0;
}
We can define our copy constructor. If we don’t define a copy constructor then the
default copy constructor is called.
22. What is the difference between shallow copy and deep copy?
The difference between shallow copy and a deep copy is given below:
class base{
public:
virtual void fun(){
}
};
class base{
public:
virtual void fun()=0;
};
Here, = sign has got nothing to do with the assignment, and value 0 is not assigned to
anything. It is used to simply tell the compiler that a function will be pure and it will
not have anybody.
The derived class has two parts, a base part, and a derived part. When C++ constructs
derived objects, it does so in phases. First, the most-base class(at the top of the
inheritance tree) is constructed. Then each child class is constructed in order until
the most-child class is constructed last.
So the first Constructor of class B will be called and then the constructor of class D
will be called.
During the destruction exactly reverse order is followed. That is destructor starts at
the most-derived class and works its way down to base class.
So the first destructor of class D will be called and then the destructor of class B will
be called.
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){
}
}
class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout<<”fun inside derived class”<<endl;
}
}
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
We can assign a pointer of any type to a void pointer but the reverse is not true unless
you typecast it as
str=(char*) ptr;
class A{
private:
int value;
public:
void setvalue(int x){
this->value=x;
}
};
int main(){
A a;
a.setvalue(5);
return 0;
}
Additional Resources
Practice Coding
C++ MCQ
C++ Tutorials
C Interview Questions
Difference Between C and C++
Difference Between C++ and Java
Online C++ Compiler
Features of C++