Lab Manual
Lab Manual
int main() {
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
2. Develop a C++ program to sort the elements in ascending and descending order.
#include <iostream>
using namespace std;
int main() {
// Array declaration
int arr[MAX];
int n, i, j;
int temp;
// Read n elements
for (i = 0; i < n; i++) {
cout << "Enter element [" << i + 1 << "]: ";
cin >> arr[i];
}
}
// Print sorted array elements in Descending Order
cout << "Sorted (Descending Order) Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;
return 0;
}
Output
3. Develop a C++ program using classes to display student name, roll number, marks obtained in two
Subjects and total score of student.
#include <iostream>
#include <string>
using namespace std;
public:
// Method to input student details
void inputDetails() {
cout << "Enter student name: ";
getline(cin, name);
cout << "Enter roll number: ";
cin >> rollNumber;
cout << "Enter marks for subject 1: ";
cin >> marks1;
cout << "Enter marks for subject 2: ";
cin >> marks2;
cin.ignore(); // To ignore the newline character left in the buffer
}
int main() {
Student student;
// Input student details
student.inputDetails();
// Display student details
student.displayDetails();
return 0;
}
Output
Student Details:
Name: John Doe
Roll Number: 12345
Marks in Subject 1: 85
Marks in Subject 2: 90
Total Score: 175
4. Develop a C++ program for a bank employee to print name of the employee, account_no. & balance.
Print invalid balance if amount<500, Display the same, also display the balance after withdraw and
deposit.
#include <iostream>
#include <string>
using namespace std;
public:
// Constructor to initialize the bank account with details
BankAccount(string name, int accNo, float initialBalance) {
employeeName = name;
accountNo = accNo;
if (initialBalance < 500) {
cout << "Invalid initial balance. Setting balance to 500." << endl;
balance = 500;
} else {
balance = initialBalance;
}
}
int main() {
string name;
int accNo;
float initialBalance;
float depositAmount, withdrawAmount;
// Deposit money
cout << "\nEnter amount to deposit: ";
cin >> depositAmount;
account.deposit(depositAmount);
account.displayDetails();
// Withdraw money
cout << "\nEnter amount to withdraw: ";
cin >> withdrawAmount;
account.withdraw(withdrawAmount);
account.displayDetails();
return 0;
}
Output
Enter employee name: Alice Smith
Enter account number: 98765
Enter initial balance: 450
Invalid initial balance. Setting balance to 500.
Employee Details:
Name: Alice Smith
Account Number: 98765
Balance: $500
Deposited: $200
Employee Details:
Name: Alice Smith
Account Number: 98765
Balance: $700
Employee Details:
Name: Alice Smith
Account Number: 98765
Balance: $600
5. Develop a C++ program to demonstrate function overloading for the following prototypes.
add(int a, int b)
add(double a, double b)
#include <iostream>
using namespace std;
int main() {
int int1, int2;
double double1, double2;
// Input integers
cout << "Enter the first integer: ";
cin >> int1;
cout << "Enter the second integer: ";
cin >> int2;
// Input doubles
cout << "Enter the first double: ";
cin >> double1;
cout << "Enter the second double: ";
cin >> double2;
// Display results
cout << "\nSum of integers: " << add(int1, int2) << endl;
cout << "Sum of doubles: " << add(double1, double2) << endl;
return 0;
}
Output
Enter the first integer: 5
Enter the second integer: 10
Enter the first double: 3.5
Enter the second double: 2.1
Sum of integers: 15
Sum of doubles: 5.6
6. Develop a C++ program using Operator Overloading for overloading Unary minus operator.
#include<iostream>
using namespace std;
class NUM
{
private:
int n;
public:
//function to get number
void getNum(int x)
{
n=x;
}
//function to display number
void dispNum(void)
{
cout << "value of n is: " << n;
}
//unary - operator overloading
void operator - (void)
{
n=-n;
}
};
int main()
{
NUM num;
num.getNum(10);
-num;
num.dispNum();
cout << endl;
return 0;
}
Output:
value of n is: -10
7. Develop a C++ program to implement Multiple inheritance for performing arithmetic operation of
two numbers.
#include <iostream>
using namespace std;
int main() {
ArithmeticOperations ops;
int num1, num2;
double num1D, num2D;
// Input integers
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;
return 0;
}
Output
Enter the first integer: 10
Enter the second integer: 5
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 5.25
8. Develop a C++ program using Constructor in Derived classes to initialize alpha, beta and gamma and
display corresponding values.
#include <iostream>
using namespace std;
// Base class
class Base {
protected:
int alpha;
public:
// Constructor for Base class
Base(int a) : alpha(a) {
// Initialization of alpha
}
};
// Derived class
class Derived : public Base {
private:
int beta;
int gamma;
public:
// Constructor for Derived class
Derived(int a, int b, int c) : Base(a), beta(b), gamma(c) {
// Initialization of alpha (from Base class), beta, and gamma
}
int main() {
int alpha, beta, gamma;
// Input values
cout << "Enter value for alpha: ";
cin >> alpha;
cout << "Enter value for beta: ";
cin >> beta;
cout << "Enter value for gamma: ";
cin >> gamma;
return 0;
}
Values:
Alpha: 10
Beta: 20
Gamma: 30
9. Develop a C++ program to create a text file, check file created or not, if created it will write some
text into the file and then read the text from the file.
#include <iostream>
#include <fstream> // For file handling
#include <string>
using namespace std;
int main() {
string filename = "example.txt";
string textToWrite;
string line;
return 0;
}
Output:
10. Develop a C++ program to write and read time in/from binary file using fstream
#include <iostream>
#include <fstream> // For file handling
using namespace std;
int main() {
string filename = "time.bin";
Time time;
return 0;
}
Output:
11. Develop a function which throws a division by zero exception and catch it in catch block. Write a
C++ program to demonstrate usage of try, catch and throw to handle exception
#include <iostream>
using namespace std;
int divide(int a, int b) {
if (b == 0) {
throw "Division by zero error!"; // Throw an exception
}
return a / b;
}
int main() {
int num1, num2, result;
try {
cout << "Enter two numbers: ";
cin >> num1 >> num2;
result = divide(num1, num2);
cout << "Result: " << result << endl;
} catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}
12. Develop a C++ program that handles array out of bounds exception using C++.
#include <iostream>
using namespace std;
int main() {
int arr[5];
int index;
cout << "Enter 5 elements for the array: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Enter an index to access: ";
cin >> index;
try {
if (index < 0 || index >= 5) {
throw "Array index out of bounds!";
}
cout << "Element at index " << index << ": " << arr[index] << endl;
} catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}
Output
Enter 5 elements for the array: 1 2 3 4 5
Enter an index to access: 6
Exception caught: Array index out of bounds!