LAXMI C++ journal
LAXMI C++ journal
BELAGAVI
LAB MANUAL
NAME: LAXMI B
MAGADUM
USN : 2VX22CBO23
SEM : III
C++ having Subject Code BCS306B during the academic year 2023-24.
Source code:
#include<iostream>
int main()
double x1,x2,x3;
cin>>x1>>x2>>x3;
else
return 0;
}
OUTPUT 1:
24
56
OUTPUT 2:
99
78
int main()
int num[100],n;
int i,j,temp;
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]){
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
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]){
temp=num[i];
num[i]=num[j];
num[j]=temp;
cout<<" Descending:"<<endl;
for(i=0;i<n;i++){
cout<<" "<<num[i]<<endl;
return 0;
OUTPUT 1:
Enter value
67
Enter value
19
Enter value
88
Enter value
98
Ascending:
19
67
88
98
Descending:
98
88
67
19
OUTPUT 2:
Enter value
98
Enter value
Enter value
23
Ascending:
23
98
Descending:
98
23
5
3. Develop a C++ program using classes to display student name, roll
number, marks obtained in two subjects and total score of student
Aim: Display student name, roll number, marks obtained
in two subject and total score of student using classes.
Algorithm:
Start
Define a class Student
Declare private member variables for name,
roll number, marks of two subjects, and total
score
Define public member functions to set and get
values for name, roll number, marks, and
calculate total score
Read student details (name, roll number,
marks of two subjects) from user input
Create an object of the Student class
Set the values for name, roll number, and
marks using the public member functions
Calculate the total score
Print student details (name, roll number,
marks, total score)
Stop
Source code:
#include<iostream>
class Student
{
public:
string name;
int roll_no;
int marks1;
int marks2;
int total_score()
{
return marks1 + marks2;
};
int main()
{
Student s1;
cout<<"Enter the name of the student:";
cin>>s1.name;
cout<<"Enter the roll number of student:";
cin>>s1.rno;
cout<<"Enter the marks of the student in subject 1:";
cin>>s1.marks1;
cout<<"Enter the marks of the student in subject 2:";
cin>>s1.marks2;
cout<<"The name of the student is:"<<s1.name<<endl;
cout<<"The roll number of the student is:"<<s1.roll_no<<endl;
cout<<"The marks of the student in subject 1
are:"<<s1.marks1<<endl;
cout<<"The marks of the student in subject 2
are:”<<s1.marks2<<endl;
cout<<"The total score of the student is:"<<s1.total_score()<<endl;
return 0;
}
OUTPUT 1:
Enter the name of the student :Abc
OUTPUT 2:
#include <iostream>
#include <string>
class BankEmployee {
private:
string name;
int accountNo;
double balance;
public:
BankEmployee() {}
void setDetails() {
cout << "Enter name: ";
cin>>name;
cout << "Enter account number: ";
cin >> accountNo;
cout << "Enter balance: ";
cin >> balance;
}
void printDetails() {
cout << "Name: " << name << endl;
cout << "Account No.: " << accountNo << endl;
cout << "Balance: " << balance << endl;
}
return 0;
}
OUTPUT 1:
Enter name: xyz
Enter account number: 666554
Enter balance: 7000
Name: xyz
Account No.: 666554
Balance: 7000
Enter withdrawal amount: 500
Withdrawal successful. Balance after withdrawal: 6500
Enter deposit amount: 600
Deposit successful. Balance after deposit: 7100
OUTPUT 2:
Enter name: ABC
Enter account number: 666754
Enter balance: 700
Name: ABC
Account No.: 666754
Balance: 700
Enter withdrawal amount: 500
Withdrawal successful. Balance after withdrawal: 200
Enter deposit amount: 600
Deposit successful. Balance after deposit: 1300
5. Develop a C++ program to demonstrate function overloading for the
following prototypes.
Add(int a, int b)
add(double a, double b)
Aim: Demonstrate function overloading.
Algorithm:
Start. Define a function Add with parameters (int a,
int b) that returns the sum of a and b
Define another function Double with parameters
(double a, double b) that returns the product of a
and b
Call Add with integer arguments and store the
result
Call Double with double arguments and store the
result
Print the results and stop.
Source code:
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
Output :
sum = 12
sum = 11.5
Source code:
#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 1:
Original Number:
Value: 5
Number after Unary Minus Overloading:
Value: -5
OUTPUT 2:
Original Number:
Value: 8
Number after Unary Minus Overloading:
Value: -8
7. Develop a C++ program to implement Multiple inheritance for
performing arithmetic operation of two numbers
{
public:
int multiply(int a, int b)
{
return a * b;
}
int main()
{
Arithmetic calculator;
int num1 = 9;
int num2 = 5;
int sum = calculator.add(num1, num2); s
cout << "Sum: " << sum << endl;
int difference = calculator.subtract(num1, num2);
cout << "Difference: " << difference << endl;
int product = calculator.multiply(num1, num2);
cout << "Product: " << product << endl;
int quotient = calculator.divide(num1, num2);
cout << "Quotient: " << quotient << endl;
return 0;
}
OUTPUT:
Sum: 14
Difference: 4
Product: 45
Quotient: 1
8. Develop a C++ program using Constructor in Derived classes to
initialize alpha, beta and gamma and display corresponding values.
Aim: Constructor in Derived classes to initialize alpha, beta and gamma and
display corresponding values.
Algorithm:
Start
Base class has a member variable alpha and a constructor to initialize it.
Derived A is derived from Base and has an additional member variable beta.
It initializes both alpha and beta using its constructor.
Stop
Source code:
#include <iostream>
class Alpha
{
protected:
int alpha;
public:
Alpha(int a) : alpha(a)
{
}
void displayAlpha()
{
cout << "Alpha: " << alpha << endl;
}
};
class Beta : public Alpha
{
private:
int beta;
public:
Beta(int a, int b) : Alpha(a), beta(b)
{
}
void displayBeta()
{
displayAlpha();
cout << "Beta: " << beta << endl;
}
};
class Gamma : public Alpha
{
private:
int gamma;
public:
Gamma(int a, int g) : Alpha(a), gamma(g)
{
}
void displayGamma()
{
displayAlpha();
cout << "Gamma: " << gamma << endl;
}
};
int main() {
Beta objBeta(11, 12);
objBeta.displayBeta();
Gamma objGamma(13, 14);
objGamma.displayGamma();
return 0;
OUTPUT:
Alpha: 11
Beta: 12
Alpha: 13
Gamma: 14
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.
Algorithm:
Define a function to create a text file and write some text into
it.
In the main() function, call the function to create and write text
into the file.
Print the text read from the file onto the console.
Source code:
#include <iostream>
#include <fstream>
#include <string>
int main() {
ofstream outputFile("sample.txt");
if (!outputFile.is_open())
cerr << "Error: File could not be created." << endl; return
1;
}
string textToWrite = "There is some text written in the file.";
outputFile.close();
ifstream inputFile("sample.txt");
if (!inputFile.is_open())
cerr << "Error: File could not be opened for reading." << endl;
return 1;
string textRead;
cout << "Text from the file: " << textRead << endl;
inputFile.close();
return 0;
}
OUTPUT:
Text from the file: There is some text written in the file.
10.Develop a C++ program to write and read time in/from binary file using
fstream
Aim: to write and read time in/from binary file using fstream
Algorithm:
Start
Include necessary header files like iostream, fstream, and
ctime for time-related functions.
Define a structure to hold the time information. In this
case, we can use the tm structure from ctime.
Define a function to write the current time to a binary file.
Define another function to read the time from the binary
file.
In the main() function, call the function to write the
current time to the file.
Then call the function to read the time from the file.
Print the time read from the file onto the console.
End
Source code:
#include <iostream>
#include <fstream>
#include <ctime>
int main()
std::ofstream outFile("time_data.bin",
std::ios::binary); 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) {
cerr << "Failed to open the file for reading." << endl; return 1;
tm readTimeInfo;
inFile.read(reinterpret_cast<char*>(&readTimeInfo), sizeof(tm));
inFile.close();
return 0;
OUPUT:
if (denominator == 0)
{
throw std::runtime_error("Division by zero is not allowed.");
}
return numerator / denominator;
}
int main() {
int numerator, denominator;
OUTPUT 2:
Enter numerator: 77
Enter denominator: 11
Result of division: 7
12. Develop a C++ program that handles array out of bounds exception using
C++.
Aim: program that handles array out of bounds exception using C++.
Algorithm:
Start
Declare an array of size n
Declare a variable index
Read the value of index from user input
If index is within the bounds of the array (0 <= index < n)
Access arr[index]
Print arr[index]
Else
Print "Array index out of bounds"
Stop
Source code:
#include<iostream>
#include <vector>
#include <stdexcept>
int main()
try {
index;
cout << "Element at index " << index << " is: " << element << endl;
return 0;
OUTPUT: