Q1. What is C++?
C++ is a general-purpose, object-oriented programming language developed by
Bjarne Stroustrup as an extension of the C language. It supports procedural, object-
oriented, and generic programming.
Q2. What are the basic concepts of Object-Oriented Programming (OOP) in
C++?
The basic concepts are:
Class
Object
Encapsulation
Inheritance
Polymorphism
Abstraction
Q3. What is the difference between C and C++?
C is procedural; C++ is both procedural and object-oriented.
C does not support classes and objects; C++ supports them.
C++ has features like function overloading, inheritance, templates, which C
lacks.
Q4. What is a Class and Object?
Class: A blueprint for creating objects, defining their properties and
behaviors.
Object: An instance of a class.
Q5. What is a constructor?
A constructor is a special member function that is automatically called when an
object is created. It initializes the object.
Q6. What is a destructor?
A destructor is a special member function that is automatically invoked when an
object is destroyed. It is used to free resources.
Q7. What is function overloading?
Function overloading is the ability to define multiple functions with the same name
but different parameters (type or number).
Q8. What is operator overloading?
Operator overloading allows us to redefine the way an operator works for user-
defined types (like objects).
Q9. What is inheritance?
Inheritance is a feature that allows a class (derived class) to acquire properties and
behaviors (methods) from another class (base class).
Q10. What is polymorphism?
Polymorphism means "many forms." It allows a function or operator to behave
differently based on the objects it is acting upon.
There are two types: compile-time (function overloading, operator overloading) and
runtime (virtual functions).
Q11. What is a virtual function?
A virtual function is a function in a base class that can be overridden in a derived
class. It supports runtime polymorphism.
Q12. What is a pointer?
A pointer is a variable that stores the memory address of another variable.
Q13. What is the difference between a structure and a class in C++?
In a structure, members are public by default.
In a class, members are private by default.
Q14. What are templates in C++?
Templates allow writing generic programs. They enable functions and classes to
operate with any data type without rewriting code for each type.
Q15. What is the difference between compile-time and run-time
polymorphism?
Compile-time polymorphism is achieved through function overloading and
operator overloading.
Run-time polymorphism is achieved through virtual functions.
Q16. What is the use of 'this' pointer?
The this pointer points to the current object of the class. It is used to access the
current object's members.
Q17. What is the difference between deep copy and shallow copy?
Shallow copy: Copies the object's reference (only addresses).
Deep copy: Copies the actual data and allocates separate memory for it.
Q18. What is exception handling in C++?
Exception handling is a mechanism to handle runtime errors using try, catch,
and throw blocks.
Q19. What is the difference between new and malloc()?
new is an operator in C++ which initializes memory and calls constructors.
malloc() is a C function that only allocates memory but does not call
constructors.
Q20. What are access specifiers in C++?
Access specifiers define the access level for members of a class:
public: Accessible everywhere.
private: Accessible only within the class.
protected: Accessible within the class and its derived classes.
21. What is a class in C++?
A class is a user-defined data type in C++ that contains data members (variables) and member
functions (methods). It defines the blueprint for creating objects.
22. What is an object in C++?
An object is an instance of a class. It has its own copy of the class’s data members and can use the
class’s member functions.
23. How do you define a class in C++?
cpp
CopyEdit
class Student {
public:
string name;
void display() {
cout << "Name: " << name;
}
};
24. How do you create an object of a class?
cpp
CopyEdit
Student s1; // s1 is an object of class Student
25. What are access specifiers?
Access specifiers define the access level of class members:
public – Accessible from anywhere.
private – Accessible only within the class.
protected – Accessible within the class and its derived classes.
26. What is the default access specifier in a class?
The default access specifier in a class is private.
27. What is the difference between a class and a structure in C++?
In C++:
Class members are private by default.
Struct members are public by default. Apart from that, both can have member functions
and access specifiers.
29. What is a constructor?
A constructor is a special function that is automatically called when an object is created. It is used to
initialize the object’s members.
30. What are the types of constructors in C++?
Default constructor
Parameterized constructor
Copy constructor
Dynamic constructor (allocates memory dynamically)
Constructor with default arguments
31. What is a destructor?
Answer:
A destructor is a special function that is called when an object is destroyed. It is used to release
resources. It has the same name as the class, preceded by a ~ tilde.
32. Can a class have multiple constructors?
Answer:
Yes, this is called constructor overloading. C++ allows multiple constructors with different
parameter lists.
33. What is the difference between member function and non-member
function?
A member function is defined inside a class and can access private members.
A non-member function is defined outside the class and cannot access private members
unless it is a friend function.
34. What is the this pointer in C++?
this is a pointer that points to the current object. It is implicitly passed to all non-static member
functions.
35. What are static members?
Static members belong to the class, not to any individual object. They are shared by all objects of the
class.
cpp
CopyEdit
class Example {
static int count;
};
36. Can a class have objects of another class as members?
Yes, this is known as composition or containment (a "has-a" relationship).
37. What is operator overloading?
Operator overloading is a feature in object-oriented programming that allows developers to
redefine the way operators work for user-defined types (like classes and structs). It enables
operators like +, -, ==, etc., to work with objects in a natural and intuitive way.
38. Why do we use operator overloading?
We use operator overloading to make code more readable and maintainable. For example,
adding two Complex number objects using c1 + c2 is more intuitive than calling a function
like c1.add(c2).
39. Which operators can be overloaded?
Most operators can be overloaded, including +, -, *, /, %, ==, !=, <, >, =, [], (), and ->.
However, some operators cannot be overloaded, such as:
. (member access)
.* (pointer-to-member)
:: (scope resolution)
sizeof
typeid
40. How do you overload an operator in C++?
You define a special function using the operator keyword. For example, to overload the +
operator:
cpp
CopyEdit
class Complex {
public:
int real, imag;
Complex operator+(const Complex& obj) {
Complex result;
result.real = real + obj.real;
result.imag = imag + obj.imag;
return result;
}
};
41. What is the difference between overloading operators as member vs. non-
member functions?
Member functions: The left-hand operand must be an object of the class.
Non-member (friend) functions: Allow overloading when the left-hand operand is
not of the class type (e.g., int + Complex).
42. Can you overload the assignment operator =?
Yes, you can overload the assignment operator to perform deep copying, especially when
dealing with dynamic memory allocation.
43. What is the syntax for overloading the unary operators like ++ or --?
Unary operators can be overloaded like this:
cpp
CopyEdit
class Counter {
int count;
public:
Counter() : count(0) {}
Counter operator++() { // Prefix ++
++count;
return *this;
}
Counter operator++(int) { // Postfix ++
Counter temp = *this;
++count;
return temp;
}
};
44. Is operator overloading polymorphism?
Yes, operator overloading is a form of compile-time (static) polymorphism, because the
function called is determined at compile time.
45. What is operator overloading?
Operator overloading is a feature in object-oriented programming that allows developers to redefine
the way operators work for user-defined types (like classes and structs). It enables operators like +, -
, ==, etc., to work with objects in a natural and intuitive way.
46. Why do we use operator overloading?
We use operator overloading to make code more readable and maintainable. For example, adding
two Complex number objects using c1 + c2 is more intuitive than calling a function like
c1.add(c2).
47. Which operators can be overloaded?
Most operators can be overloaded, including +, -, *, /, %, ==, !=, <, >, =, [], (), and ->.
However, some operators cannot be overloaded, such as:
. (member access)
.* (pointer-to-member)
:: (scope resolution)
sizeof
typeid
48. How do you overload an operator in C++?
You define a special function using the operator keyword. For example, to overload the +
operator:
cpp
CopyEdit
class Complex {
public:
int real, imag;
Complex operator+(const Complex& obj) {
Complex result;
result.real = real + obj.real;
result.imag = imag + obj.imag;
return result;
}
};
49. What is the difference between overloading operators as member vs. non-
member functions?
Member functions: The left-hand operand must be an object of the class.
Non-member (friend) functions: Allow overloading when the left-hand operand is not of the
class type (e.g., int + Complex).
50. Can you overload the assignment operator =?
Answer:
Yes, you can overload the assignment operator to perform deep copying, especially when dealing
with dynamic memory allocation.
51. What is the syntax for overloading the unary operators like ++ or --?
Unary operators can be overloaded like this:
cpp
CopyEdit
class Counter {
int count;
public:
Counter() : count(0) {}
Counter operator++() { // Prefix ++
++count;
return *this;
}
Counter operator++(int) { // Postfix ++
Counter temp = *this;
++count;
return temp;
}
};
52. Is operator overloading polymorphism?
Yes, operator overloading is a form of compile-time (static) polymorphism, because the function
called is determined at compile time.
53.. What is inheritance in C++?
Inheritance is a mechanism in C++ that allows a class (called the derived class) to inherit properties
and behaviors (data members and member functions) from another class (called the base class). It
promotes code reuse and establishes a relationship between classes.
54. What are the types of inheritance in C++?
C++ supports the following types of inheritance:
Single inheritance – One base and one derived class.
Multiple inheritance – A derived class inherits from more than one base class.
Multilevel inheritance – A class derived from another derived class.
Hierarchical inheritance – Multiple classes derived from a single base class.
Hybrid inheritance – Combination of two or more types of inheritance.
55. What is the syntax for inheritance?
cpp
CopyEdit
class Base {
// base class members
};
class Derived : public Base {
// derived class members
};
You can use public, protected, or private access specifiers to control inheritance.
56. What is the difference between public, private, and protected inheritance?
Inheritance Type Base Class Public Members Base Class Protected Members
public Remain public Remain protected
protected Become protected Remain protected
private Become private Become private
Private members are never inherited, but they are still present in memory and accessible via
public/protected methods.
57. What are base and derived classes?
A base class is the parent class from which members are inherited.
A derived class is the child class that inherits from the base class.
58. Can constructors be inherited?
Constructors are not inherited, but the base class constructor is called when an object of the derived
class is created. You can call a base class constructor explicitly using an initializer list.
59. What is the use of the protected keyword in inheritance?
protected members of a class are accessible within the class itself and by derived classes, but not
from outside the class. It's useful when you want derived classes to access members, but not
external functions.
60. What is the role of the virtual keyword in inheritance?
The virtual keyword is used to enable runtime polymorphism. If a base class declares a function
as virtual, it can be overridden in the derived class. The appropriate version is chosen at runtime
based on the actual object.
61.What is object slicing?
Object slicing occurs when a derived class object is assigned to a base class object. Only the base
portion is copied, and derived class-specific members are sliced off (lost).
62. What is the diamond problem in C++ inheritance?
The diamond problem occurs in multiple inheritance when two base classes inherit from the same
grandparent class, and a derived class inherits from both. This can lead to ambiguity and duplicate
copies of the base class.
It is resolved using virtual inheritance:
cpp
CopyEdit
class A { };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { }; // D has only one copy of A
63. What is a template in C++?
A template is a feature in C++ that allows functions and classes to operate with generic types. It
enables writing code that works with any data type.
64. What are the types of templates?
Function templates – For creating functions with generic types.
Class templates – For creating classes with generic types.
65. Give an example of a function template.
cpp
CopyEdit
template <typename T>
T add(T a, T b) {
return a + b;
}
This function works for int, float, double, etc.
65. What is template specialization?
Template specialization allows customizing the template behavior for a specific data type.
cpp
CopyEdit
template <>
void display<int>(int value) {
cout << "Integer: " << value;
}
66. Can templates be overloaded?
Yes. You can overload a function template with another template or a regular function.
67. What is a namespace in C++?
A namespace is a declarative region that provides a scope to identifiers (like variables, functions) to
avoid name conflicts in large projects.
68. How do you define and use a namespace?
cpp
CopyEdit
namespace MySpace {
int x = 10;
}
int main() {
cout << MySpace::x;
}
69. What is the using keyword in namespaces?
using namespace std; or using MySpace::x; allows you to use elements from the
namespace without specifying the full scope every time.
70. What re unnamed or anonymous namespaces?
Anonymous names paces are used to restrict access to functions or variables to the current file only
(like static in C).
cpp
CopyEdit
namespace {
int secret = 42;
}
71. Can you nest namespaces?
Yes, namespaces can be nested:
cpp
CopyEdit
namespace A {
namespace B {
void func();
}
}
72. What is exception handling in C++?
Exception handling in C++ is used to manage errors or unusual situations during program execution
using try, catch, and throw.
73. What is the syntax of exception handling in C++?
cpp
CopyEdit
try {
// code that might throw
throw 10;
} catch (int e) {
cout << "Caught exception: " << e;
}
74. What can you throw in C++?
You can throw any data type: int, char, string, objects, or even custom exceptions.
75. What is a catch-all handler?
cpp
CopyEdit
catch (...) {
cout << "Caught an unknown exception";
}
It catches all types of exceptions.
76. What is stack unwinding?
Stack unwinding is the process of cleaning up the stack when an exception is thrown. Destructors of
all local objects are called in reverse order.
77. What is file handling in C++?
File handling in C++ allows programs to read from and write to files on the disk using file streams
(ifstream, ofstream, fstream) provided by the <fstream> header.
78. Which header file is required for file operations?
<fstream> is the standard header file for file handling in C++. It includes ifstream, ofstream,
and fstream classes.
79. What are the different file stream classes in C++?
ifstream – For reading from files (input file stream).
ofstream – For writing to files (output file stream).
fstream – For both reading and writing.
80. How do you open a file in C++?
cpp
CopyEdit
ifstream fin("input.txt");
ofstream fout("output.txt");
Or using the .open() function:
cpp
CopyEdit
fin.open("input.txt");
81. How do you write to a file?
cpp
CopyEdit
ofstream fout("data.txt");
fout << "Hello, File!";
fout.close();
82. How do you read from a file?
Answer:
cpp
CopyEdit
ifstream fin("data.txt");
string line;
while (getline(fin, line)) {
cout << line << endl;
}
fin.close();
83. What is the purpose of ios::in, ios::out, and ios::app?
ios::in – Open for reading.
ios::out – Open for writing.
ios::app – Append to end of file. Other flags: ios::trunc, ios::binary, etc.
84. What is the difference between text mode and binary mode?
Text mode: Data is stored in human-readable form.
Binary mode: Data is stored in raw byte format (more efficient, used for structures/objects).
85. How do you check if a file was opened successfully?
cpp
CopyEdit
ifstream fin("file.txt");
if (!fin) {
cout << "File not opened!";
}
86. What is the difference between << and .write()?
<< is used for text (formatted) output.
.write() is used for binary (unformatted) output:
cpp
CopyEdit
fout.write((char*)&obj, sizeof(obj));
87. What does .eof() mean?
.eof() returns true when the end of the file has been reached during a read operation.
88. How do you close a file?
cpp
CopyEdit
fin.close();
fout.close();
It is important to close files to flush buffers and free system resources.
89. Can you read and write to the same file simultaneously?
Yes, by using fstream with both ios::in | ios::out flags:
cpp
CopyEdit
fstream file("data.txt", ios::in | ios::out);
90. What are the functions seekg() and seekp() used for?
seekg() – Sets the position of the get pointer (read).
seekp() – Sets the position of the put pointer (write).
Example:
cpp
CopyEdit
file.seekg(0, ios::beg); // move to beginning