CPP Internal
Q1. Define Constructor and Destructor.
1. Constructor :
Definition : In C++, a constructor is a special function that is automatically
called when an object of a class is created. It is used to initialize the
object's members and allocate memory for it.
Characteristics :
It has the same name as the class
It does not have a return value
It can be public, protected, or private
It can have default arguments
It can have member initialization lists
2. Destructor :
Definition : In C++, a destructor is a special function that is automatically
called when an object is destroyed. It's responsible for releasing resources
and performing cleanup tasks for the object.
Characteristics :
Destructors take no arguments and have no return type.
Their address cannot be taken.
Destructors cannot be declared const, volatile, const volatile, or static.
A destructor can be declared virtual or pure virtual
CPP Internal 1
Q2. What is compile time ( static ) runtime
polymorphism?
Definition : Polymorphism in programming allows a function or an object to
take multiple forms. It is mainly classified into Compile-Time Polymorphism
and Run-Time Polymorphism.
1. Compile time : This type of polymorphism is resolved during compilation. It is
usually achieved through function overloading and operator overloading.
2. Runtime : This type of polymorphism is resolved at runtime. It is achieved
using method overriding through inheritance and virtual functions.
Q3. what is function overloading
Function overloading is a feature of object-oriented programming where two
or more functions can have the same name but different parameters.
When a function name is overloaded with different jobs it is called Function
Overloading.
In Function Overloading “Function” name should be the same and the
arguments should be different.
Example ,
add(int a, int b)
add(double a, double b)
Q4. explain exception handling
Definition : Exception handling is a programming technique that allows a
program to handle errors and continue running. Exception handling helps
ensure that a program is robust, user-friendly, and maintainable. It allows a
program to recover from errors instead of terminating unexpectedly
1. try in C++
CPP Internal 2
The try keyword represents a block of code that may throw an exception
placed inside the try block. It’s followed by one or more catch blocks. If an
exception occurs, try block throws that exception.
2. catch in C++
The catch statement represents a block of code that is executed when a
particular exception is thrown from the try block. The code to handle the
exception is written inside the catch block.
3. throw in C++
An exception in C++ can be thrown using the throw keyword. When a program
encounters a throw statement, then it immediately terminates the current
function and starts finding a matching catch block to handle the thrown
exception.
Q5. define friend function and characteristics of friend
function
Definition :
A friend function is a function that is not a member of a class but is given
access to the private and protected members of that class. It is declared
inside the class with the keyword friend but defined outside the class.
Characteristics :
Declared Inside, Defined Outside → It is declared inside the class with
friend but is not a member of the class.
Accesses Private Members → Can access private and protected
members of the class.
Not Called Using Objects → It is called like a normal function, not using an
object.
Can Be a Global Function → It does not belong to the class, so it can exist
outside the class.
Can Have Multiple Friends → A class can have multiple friend functions.
CPP Internal 3
Uses Object as Argument → It typically takes objects of the class as
arguments.
Cannot Use this Pointer → Since it is not a member of the class, it does
not have access to this pointer.
Q6. Enlist manipulators in CPP
Manipulator Description
endl Inserts a newline ( \n ) and flushes the output buffer.
setw(n) Sets the field width to n for the next output.
setprecision(n) Sets the number of decimal places for floating-point numbers.
fixed Displays floating-point numbers in fixed-point notation.
scientific Displays floating-point numbers in scientific notation.
left Left-aligns the output.
right Right-aligns the output.
showpoint Forces the display of decimal points in floating-point numbers.
noshowpoint Hides the decimal point when not necessary.
showpos Displays a + sign for positive numbers.
noshowpos Hides the + sign for positive numbers.
hex Converts integer output to hexadecimal format.
dec Converts integer output to decimal format.
oct Converts integer output to octal format.
CPP Internal 4
uppercase Displays letters in uppercase (for hex and scientific notation).
nouppercase Displays letters in lowercase (default behavior).
boolalpha Prints true or false instead of 1 or 0 for boolean values.
noboolalpha Prints 1 or 0 instead of true or false .
Q7. Explain use of scope resolution operator with
syntax
Definition :
The
scope resolution operator ( :: ) is used to access global or class-specific
members when there is ambiguity. It helps in defining functions outside a
class, accessing static members, and distinguishing between global and local
variables
Example :
#include <iostream>
using namespace std;
int x = 100; // Global variable
int main() {
int x = 50; // Local variable
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x << endl; // Using scope resolution operator
return 0;
}
Q8. What is inheritance and types of inheritance
CPP Internal 5
Definition :
Inheritance is a feature in C++ that allows a new class (child) to acquire
properties and behavior from an existing class (parent). It helps in code
reusability and reducing redundancy.
Types :
Single Inheritance → One child class inherits from one parent class.
Multiple Inheritance → One child class inherits from multiple parent
classes.
Multilevel Inheritance → A child class inherits from another child class
(forms a chain).
Hierarchical Inheritance → Multiple child classes inherit from one parent
class.
Hybrid Inheritance → Combination of two or more types of inheritance.
Q9. what is abstraction and encapsulation
1. Abstraction :
Hiding implementation details and showing only essential features to the
user.
In C++, abstraction is achieved using abstract classes and interfaces
(pure virtual functions).
2. Encapsulation :
Wrapping data and methods together in a single unit (class) and
restricting direct access to the data.
Encapsulation is implemented using private/protected access specifiers
in C++.
Example ,
A bank account class
CPP Internal 6
hides balance details but provides methods to deposit or withdraw money
securely.
Q10. List different types of constructors and explain
them
1. Default Constructor
A constructor that takes no arguments and initializes default values.
If no constructor is defined, C++ provides a default constructor
automatically.
2. Parameterized Constructor
A constructor that takes arguments to initialize an object with specific
values.
3. Copy Constructor
A constructor that creates a new object as a copy of an existing object.
If not defined, C++ provides a default copy constructor.
Q11. Explain call by value and call by reference
1. Call by Value
A copy of the variable is sent to the function, so the original value
remains unchanged.
Changes made inside the function do not affect the original variable.
Example :
#include <iostream>
using namespace std;
void modify(int num) { // Function gets a copy of 'a'
CPP Internal 7
num = 50; // Changes only the copy
}
int main() {
int a = 10;
modify(a); // Call by value
cout << "Value of a: " << a << endl; // Original value remains unchange
}
2. Call by Reference
Passes the actual variable (memory address), so changes affect the original
value.
Uses pointers (* ) or reference ( & ).
Example:
#include <iostream>
using namespace std;
void modify(int &num) { // Function gets actual reference of 'a'
num = 50; // Changes original value
}
int main() {
int a = 10;
modify(a); // Call by reference
cout << "Value of a: " << a << endl; // Original value is changed
}
Q12. Enlist the different file opening modes and
explain them
ios::in Opens file for reading only.
CPP Internal 8
ios::out Opens file for writing only (deletes existing content).
ios::app Opens file in append mode (adds data at the end).
ios::ate Opens file and moves the pointer to the end.
ios::trunc Deletes existing content if the file exists.
ios::binary Opens file in binary mode instead of text mode.
Q13. What is abstract class
An abstract class is a class that cannot be instantiated (you cannot create
objects of it).
It is used as a blueprint for other classes.
It contains at least one pure virtual function
Syntax :
class AbstractClass {
public:
virtual void show() = 0; // Pure virtual function (must be overridden)
};
Q14. What is new operator
The new operator is used to dynamically allocate memory in C++.
It returns a pointer to the allocated memory.
Memory remains allocated until manually freed using delete .
Syntax :
datatype* pointer = new datatype;
CPP Internal 9
Q15. Program to copy contents of a file into another file
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open source file for reading
ifstream sourceFile("source.txt", ios::in);
// Check if source file exists
if (!sourceFile) {
cout << "Error: Source file not found!" << endl;
return 1; // Exit program
}
// Open destination file for writing
ofstream destFile("destination.txt", ios::out);
// Copy contents
char ch;
while (sourceFile.get(ch)) { // Read character by character
destFile.put(ch); // Write to destination file
}
// Close files
sourceFile.close();
destFile.close();
cout << "File copied successfully!" << endl;
return 0;
}
CPP Internal 10
Q16. Program to find min max between 2 numbers
using function template
#include <iostream>
using namespace std;
// Function template to find minimum
template <typename T>
T findMin(T a, T b) {
return (a < b) ? a : b;
}
// Function template to find maximum
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
// Testing with integers
int x = 10, y = 20;
cout << "Min: " << findMin(x, y) << endl;
cout << "Max: " << findMax(x, y) << endl;
// Testing with floating-point numbers
double p = 12.5, q = 8.7;
cout << "Min: " << findMin(p, q) << endl;
cout << "Max: " << findMax(p, q) << endl;
return 0;
}
CPP Internal 11
Q17. What is a Virtual Function?
A virtual function is a function in a base class that is meant to be overridden in
a derived class.
It allows achieving runtime polymorphism (method overriding).
Ensures the correct function is called for an object, even if it’s accessed
through a base class pointer.
Need for virtual function :
Method overriding
to achieve runtime polymorphism
CPP Internal 12