C++ Assignment
C++ Assignment
#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.
#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;
}
#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;
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);
return 0;
}