C++ lab manual
C++ lab manual
LAB MANUAL
DEPARTMENT VISION
To produce technocrats with creative technical knowledge and intellectual skills to sustain and
excel in the highly demanding world with confidence.
DEPARTMENT MISSION
M1. To Produce the Best Computer Science Professionals with intellectual skills.
M2. To Provide a Vibrant Ambiance that promotes Creativity, Technology Competent and
Innovations for the new Era.
M3. To Pursue Professional Excellence with Ethical and Moral Values to sustain in the highly
demanding world.
THE OXFORD COLLEGE OF
ENGINEERING
Bommanahalli, Hosur Road, Bangalore – 560068
(Affiliated To Visvesvaraya Technological University, Belagavi)
deposit.
add(double a, double b
Develop a C++ program to write and read time in/from binary file
10
using fstream
#include <iostream>
int main() {
else
return 0;
OUTPUT:
56
273
int main(){
int num[100],n;
int i,j,man;
cin>>n;
for(i=0;i<n;i++){
cout<<"enter number"<<endl;
cin>>num[i];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(num[i]<num[j]){
man=num[i];
num[i]=num[j];
num[j]=man;
cout<<"Ascending: "<<endl;
for(i=0;i<n;i++){
cout<<" "<<num[i]<<endl;;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(num[i]>num[j]){
man=num[i];
num[i]=num[j];
num[j]=man;
cout<<" Descending:"<<endl;
for(i=0;i<n;i++){
cout<<" "<<num[i]<<endl;
return 0;
OUTPUT:
enter value
56
enter value
90
enter value
78
Ascending:
56
78
90
Descending:
90
78
56
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>
class Student {
public:
void setStudentInfo(const std::string& name, int rollNumber, double
marksSubject1, double marksSubject2) {
studentName = name;
studentRollNumber = rollNumber;
marks1 = marksSubject1;
marks2 = marksSubject2;
calculateTotal();
}
void displayStudentInfo() {
private:
std::string studentName;
int studentRollNumber;
double marks1;
double marks2;
double totalScore;
void calculateTotal() {
};
int main() {
Student student;
student.displayStudentInfo();
return 0;
OUTPUT:
Marks in Subject 2: 92
#include <iostream>
#include <string>
class BankEmployee {
public:
void setEmployeeInfo(const std::string& name, int accountNo, double
balance) {
employeeName = name;
accountNumber = accountNo;
currentBalance = balance;
}
void displayEmployeeInfo() {
std::cout << "Employee Name: " << employeeName << std::endl;
std::cout << "Account Number: " << accountNumber << std::endl;
std::cout << "Balance: " << currentBalance << std::endl;
private:
std::string employeeName;
int accountNumber;
double currentBalance;
};
int main() {
BankEmployee employee;
employee.setEmployeeInfo("John Doe", 12345, 1000.0);
employee.displayEmployeeInfo();
employee.withdraw(300.0);
employee.deposit(200.0);
return 0;
}
OUTPUT:
Employee Name: John Doe
Account Number: 12345
Balance: 1000
Withdrawn: 300
Updated Balance: 700
Deposited: 200
Updated Balance: 900
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>
int add(int a, int b) {
return a + b;
}
int main() {
int intResult;
double doubleResult;
return 0;
}
OUTPUT:
Integer Addition Result: 15
Double Addition Result: 6.2
6. Develop a C++ program using Operator Overloading for overloading
Unary minus operator.
#include <iostream>
class MyNumber {
private:
int value;
public:
MyNumber(int val) : value(val) {}
MyNumber operator-() {
return MyNumber(-value);
}
void display() {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
MyNumber num1(5);
MyNumber num2 = -num1;
std::cout << "Original Number:" << std::endl;
num1.display();
std::cout << "Number after Unary Minus Overloading:" << std::endl;
num2.display();
return 0;
}
OUTPUT:
Original Number:
Value: 5
Number after Unary Minus Overloading:
Value: -5
7. Develop a C++ program to implement Multiple inheritance for
performing arithmetic operation of two numbers
#include <iostream>
class Addition {
public:
int add(int a, int b) {
return a + b;
}
};
class Subtraction {
public:
int subtract(int a, int b) {
return a - b;
}
};
public:
int multiply(int a, int b) {
return a * b;
}
int main() {
Arithmetic calculator;
int num1 = 10;
int num2 = 5;
int sum = calculator.add(num1, num2);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
OUTPUT:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
8. Develop a C++ program using Constructor in Derived classes to initialize
alpha, beta and gamma and display corresponding values.
#include <iostream>
class Alpha {
protected:
int alpha;
public:
Alpha(int a) : alpha(a) {
}
void displayAlpha() {
std::cout << "Alpha: " << alpha << std::endl;
}
};
public:
Beta(int a, int b) : Alpha(a), beta(b) {
}
void displayBeta() {
displayAlpha();
std::cout << "Beta: " << beta << std::endl;
}
};
public:
Gamma(int a, int g) : Alpha(a), gamma(g) {
}
void displayGamma() {
displayAlpha();
std::cout << "Gamma: " << gamma << std::endl;
}
};
int main() {
Beta objBeta(10, 20);
objBeta.displayBeta();
Gamma objGamma(30, 40);
objGamma.displayGamma();
return 0;
}
OUTPUT:
Alpha: 10
Beta: 20
Alpha: 30
Gamma: 40
9. Develop a C++ program using Constructor in Derived classes to initialize
alpha, beta and gamma and display corresponding values.
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ofstream outputFile("sample.txt");
if (!outputFile.is_open()) {
return 1;
outputFile.close();
std::ifstream inputFile("sample.txt");
if (!inputFile.is_open()) {
std::cerr << "Error: File could not be opened for reading." << std::endl;
return 1;
std::string textRead;
std::cout << "Text from the file: " << textRead << std::endl;
}
inputFile.close();
return 0;
OUTPUT:
Text from the file: This is some text written to the file.
10.Develop a C++ program to write and read time in/from binary file using
fstream
#include <iostream>
#include <fstream>
#include <ctime>
int main() {
if (!outFile) {
std::cerr << "Failed to open the file for writing." << std::endl;
return 1;
outFile.write(reinterpret_cast<char*>(timeInfo), sizeof(tm));
outFile.close();
if (!inFile) {
std::cerr << "Failed to open the file for reading." << std::endl;
return 1;
tm readTimeInfo;
inFile.read(reinterpret_cast<char*>(&readTimeInfo), sizeof(tm));
inFile.close();
std::cout << "Original Time: " << asctime(timeInfo);
return 0;
OUTPUT:
#include <iostream>
if (denominator == 0) {
throw std::runtime_error("Division by zero is not allowed.");
}
return numerator / denominator;
}
int main() {
int numerator, denominator;
try {
int result = divide(numerator, denominator);
std::cout << "Result of division: " << result << std::endl;
} catch (const std::runtime_error &ex) {
std::cerr << "Exception caught: " << ex.what() << std::endl;
}
return 0;
}
OUTPUT:
Enter numerator: 78
Enter denominator: 2
Result of division: 39
12.Develop a C++ program that handles array out of bounds exception
using C++.
#include <iostream>
#include <vector>
#include <stdexcept>
int main() {
try {
int index;
std::cout << "Element at index " << index << " is: " << element << std::endl;
return 0;
OUTPUT: