C++ Interview Questions
C++ Interview Questions
Function overloading is the concept of having many functions with the same name.
These functions should have different parameters for different types of functioning.
A class is a collection of different variables and different functions. The variables are
called data members and the functions are called member functions.
If a class is mentioned as a friend class to another class, then it can access private and
protected members of the other class.
Example:
class ClassA
private:
int a;
public:
ClassA()
a=10;
};
class FriendClass
private:
int b;
public:
void printClassA(ClassA& p)
cout<<“a=”<<p.a<<endl;
};
int main()
ClassA x;
FriendClass y;
y.printClassA();
return 0;
}
7. How can we access data members and member functions of a class?
8. What is inheritance?
Inheritance is the feature in which an object acquires the properties of another class.
• Single inheritance
• Multiple inheritance
• Multi-level inheritance
• Hierarchical inheritance
• Hybrid inheritance
In Single Inheritance, there is one derived class and one base class.
This is when a class can be derived with more than one parent class.
In hierarchical inheritance, many classes are derived from a single parent class.
This is a combination of more than one inheritance, called hybrid inheritance. It is also
called virtual inheritance.
16. What are access specifiers? What are the different types?
Access Specifiers define the accessibility of the members of the class. There are three
types of access specifiers:
A friend function to a class has access to all the members of the class, even the private
ones. We declare it outside the class.
Data hiding is hiding data in objects. This is generally done by having private data
members of the class.
A Copy Constructor is a member function of a class. When called while creating a new
object of the class, it initializes the object using another object of the same class. In
order to use a Copy Constructor, there should be already an existing object of that
class.
Example:
class Copy
private:
int a;
public:
Copy(int z)
a=z;
a=x.a;
void print()
};
int main()
Copy p(10);
p.print();
Copy q = p;
q.print();
There are many ways to use scope resolution. Some use examples include:
If a function is inline, the compiler places a copy of the code of that function at each
point where the function is called at compile time.
The variables of the structures are public, whereas the data member of the class can be
made as private.
A variable, when declared inside a code block, is called a local variable. We can use this
Local Variable only inside the code block. A global variable is declared on the top of the
program, before all the function definitions. It can be accessed from anywhere in the
code.
The time Compiler takes for compiling a piece of code is called Compilation Time of the
code. The time taken by the program to run is called Run Time.
Exception handling is used when some exception is encountered in the program. Three
keywords are used for exception handling: try, catch, throw.
A destructor is a special member function. The Destructor destructs the object and
frees up space.
When we try to delete an object of the derived class using a base class pointer, some
undefined is encountered. To prevent this, a virtual destructor should be defined in the
base class.