Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

OOP’S Assignment

Q.1. What is exception handling? Illustrate with suitable example.


Ans.
Exception handling is a programming concept that addresses runtime errors in a way that
allows a program to gracefully respond to unexpected situations. It involves the detection,
propagation, and handling of exceptions, which are anomalous conditions or errors that can
occur during the execution of a program.
In most programming languages, including C++, exception handling is typically implemented
using three main keywords: `try`, `catch`, and `throw`. Here's an example in C++:

#include <iostream>
using namespace std;
int main() {
try {
int numerator, denominator, result;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
if (denominator == 0) {
throw runtime_error("Division by zero is not allowed.");
}
result = numerator / denominator;
cout << "Result: " << result << endl;
} catch (const runtime_error& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}

Q.2. What is the difference between Early Binding and Late Binding in C++?
Ans.

Aspect Early Binding (Static Late Binding (Dynamic


Binding) Binding)
Time of Binding Occurs at compile-time. Occurs at runtime.
Function Addresses Addresses are fixed during Addresses are determined
compilation. during program execution.
Polymorphism Type Static polymorphism Dynamic polymorphism
(compile-time). (runtime).
Example Function overloading, Virtual functions, function
operator overloading. pointers.
Flexibility Less flexible, as decisions More flexible, as decisions
are made early in the are deferred until runtime.
process.
Performance Generally faster due to May incur a slight runtime
compile-time optimizations. overhead due to dynamic
resolution.
Code Modification Requires recompilation for Allows changes without
changes. recompilation (more
extensible).

Q.3. What is Template? Explain with suitable example.


Ans.
A template in C++ is a powerful feature that allows you to write generic code that can work
with different data types. It provides a way to create functions or classes that operate on a
wide range of data types without having to rewrite the code for each specific type.
Templates are essential for achieving code reusability and maintaining flexibility in
programming.
Function Template Example:

#include <iostream>
using namespace std;
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << "Maximum of 5 and 7: " << findMax(5, 7) << endl;
cout << "Maximum of 3.14 and 2.71: " << findMax(3.14, 2.71) << endl;
cout << "Maximum of 'A' and 'C': " << findMax('A', 'C') << endl;
return 0;
}

Class Template Example:

#include <iostream>
using namespace std;
template <typename T1, typename T2>
class Pair {
public:
T1 first;
T2 second;
Pair(T1 f, T2 s) : first(f), second(s) {}
};

int main() {
Pair<int, double> myPair(5, 3.14);
cout << "Pair: " << myPair.first << ", " << myPair.second << endl;
Pair<string, char> anotherPair("Hello", 'A');
cout << "Another Pair: " << anotherPair.first << ", " << anotherPair.second << endl;
return 0;
}

Q.4. What do you mean by generic programming? Write a program to swap the Any two
different types of variables using function templates.
Ans.
Generic programming is a programming paradigm that involves writing algorithms and data
structures in a way that allows them to work with various data types. In C++, generic
programming is often implemented using templates, which enable the creation of functions
and classes that can operate on different data types without sacrificing type safety.
Here's a program demonstrating the use of a function template to swap any two different
types of variables:

#include <iostream>
using namespace std;
template <typename T>
void swapValues(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int intVar1 = 5, intVar2 = 10;
cout << "Before swapping: " << intVar1 << ", " << intVar2 << endl;
swapValues(intVar1, intVar2);
cout << "After swapping: " << intVar1 << ", " << intVar2 << endl;

double doubleVar1 = 3.14, doubleVar2 = 2.71;


cout << "Before swapping: " << doubleVar1 << ", " << doubleVar2 << endl;
swapValues(doubleVar1, doubleVar2);
cout << "After swapping: " << doubleVar1 << ", " << doubleVar2 << endl;
char charVar1 = 'A', charVar2 = 'B';
cout << "Before swapping: " << charVar1 << ", " << charVar2 << endl;
swapValues(charVar1, charVar2);
cout << "After swapping: " << charVar1 << ", " << charVar2 << endl;
return 0;
}

Q.5. What is file access mode? Describe the various files mode.
Ans.
File access mode in C++ refers to the way a file is opened and accessed during input and
output operations. It determines the behavior of the file stream, specifying whether the file
should be opened for reading, writing, or both, as well as other characteristics like
appending or binary mode.
Here are the various file access modes in C++:
1. ios::in:
 Open the file for reading.
 If the file does not exist, the opening operation fails.
2. ios::out:
 Open the file for writing.
 If the file already exists, its contents are truncated (cleared).
 If the file does not exist, a new file is created.
3. ios::app:
 Open the file in append mode.
 Data is written to the end of the file, preserving existing content.
 If the file does not exist, a new file is created.
4. ios::ate:
 Open the file and move the file pointer to the end immediately.
 Useful for situations where you want to write data at the end of an existing
file.
5. ios::trunc:
 Open the file for writing.
 If the file already exists, its contents are truncated (cleared).
 If the file does not exist, a new file is created.
6. ios::binary:
 Open the file in binary mode.
 This mode ensures that the file is treated as a binary file rather than a text
file.
 Useful when dealing with non-text data.
Example of opening a file in various modes:

#include <fstream>
using namespace std;
int main() {
ofstream outFile1("example1.txt", ios::out | ios::trunc);

ofstream outFile2("example2.txt", ios::out | ios::app);

fstream inOutFile("example3.txt", ios::in | ios::out | ios::trunc);

fstream binaryFile("example4.bin", ios::in | ios::out | ios::binary);

return 0;
}

You might also like