0% found this document useful (0 votes)
32 views26 pages

C++ lab manual

oops lab manual

Uploaded by

hemabhooshithal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
32 views26 pages

C++ lab manual

oops lab manual

Uploaded by

hemabhooshithal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 26

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

JNANA SANGAMA, BELAGAVI, KARNATAKA 590018

LAB MANUAL

OBJECT ORIENTED PROGRAMMING with C++


(BCS306B)

The Oxford College of Engineering


Bangalore

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


THE OXFORD COLLEGE OF ENGINEERING
Hosur Road, Bommanahalli, Bangalore-560068
THE OXFORD COLLEGE OF
ENGINEERING
Bommanahalli, Hosur Road, Bangalore – 560068
(Affiliated To Visvesvaraya Technological University, Belagavi)

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)

S.No List of Programs

1 Develop a C++ program to find the largest of three numbers

Develop a C++ program to sort the elements in ascending and


2
descending order

Develop a C++ program using classes to display student name, roll


3
number, marks obtained in two subjects and total score of student

Develop a C++ program for a bank empolyee to print name of the


employee, account_no. & balance.

4 Print invalid balance if amount<500, Display the same, also display


the balance after withdraw and

deposit.

Develop a C++ program to demonstrate function overloading for


the following prototypes.
5
add(int a, int b)

add(double a, double b

Develop a C++ program using Operator Overloading for


6
overloading Unary minus operator.

Develop a C++ program to implement Multiple inheritance for


7
performing arithmetic operation oftwo numbers

Develop a C++ program using Constructor in Derived classes to


8
initialize alpha, beta and gamma and display corresponding values.
Develop a C++ program to create a text file, check file created or
9 not, if created it will write some text into the file and then read the
text from the file.

Develop a C++ program to write and read time in/from binary file
10
using fstream

Develop a function which throws a division by zero exception and


11 catch it in catch block. Write a C++ program to demonstrate usage
of try, catch and throw to handle exception.

Develop a C++ program that handles array out of bounds


12
exception using C++.
1. Develop a C++ program to find the largest of three numbers

#include <iostream>

using namespace std;

int main() {

int n1, n2, n3;

cout << "Enter any three numbers: ";

cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)

cout << "Largest number is: " << n1;

else if(n2 >= n1 && n2 >= n3)

cout << "Largest number is: " << n2;

else

cout << "Largest number is: " << n3;

return 0;

OUTPUT:

Enter any three numbers: 89

56

273

Largest number is: 273


2. Develop a C++ program to sort the elements in ascending and
descending order.

using namespace std;

int main(){

int num[100],n;

int i,j,man;

cout<<"enter n for the numbers you want to sort:"<<endl<<endl;

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 number of values you want to sort:3

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() {

std::cout << "Student Name: " << studentName << std::endl;

std::cout << "Roll Number: " << studentRollNumber << std::endl

std::cout << "Marks in Subject 1: " << marks1 << std::endl;

std::cout << "Marks in Subject 2: " << marks2 << std::endl;

std::cout << "Total Score: " << totalScore << std::endl;

private:
std::string studentName;

int studentRollNumber;

double marks1;

double marks2;
double totalScore;
void calculateTotal() {

totalScore = marks1 + marks2;

};

int main() {

Student student;

student.setStudentInfo("John Doe", 12345, 85.5, 92.0);

student.displayStudentInfo();

return 0;

OUTPUT:

Student Name: John Doe

Roll Number: 12345

Marks in Subject 1: 85.5

Marks in Subject 2: 92

Total Score: 177.5


4. Develop a C++ program for a bank empolyee to print name of the
employee, account_no. & balance. Print invalid balance if amount

#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;

if (currentBalance < 500.0) {


std::cout << "Invalid Balance" << std::endl;
}
}

void withdraw(double amount) {

if (amount > 0 && amount <= currentBalance) {


currentBalance -= amount;
std::cout << "Withdrawn: " << amount << std::endl;
std::cout << "Updated Balance: " << currentBalance << std::endl;
} else {
std::cout << "Invalid Withdrawal Amount" << std::endl;
}
}

void deposit(double amount) {


if (amount > 0) {
currentBalance += amount;
std::cout << "Deposited: " << amount << std::endl;
std::cout << "Updated Balance: " << currentBalance << std::endl;
} else {
std::cout << "Invalid Deposit Amount" << 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;
}

double add(double a, double b) {


return a + b;
}

int main() {
int intResult;
double doubleResult;

intResult = add(5, 10);


doubleResult = add(3.5, 2.7 std::cout << "Integer Addition Result: " <<
intResult << std::endl;
std::cout << "Double Addition Result: " << doubleResult << std::endl;

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;
}
};

class Arithmetic : public Addition, public Subtraction {

public:
int multiply(int a, int b) {
return a * b;
}

int divide(int a, int b) {


if (b != 0) {
return a / b;
} else {
std::cout << "Error: Division by zero" << std::endl;
return 0;
}
}
};

int main() {
Arithmetic calculator;
int num1 = 10;
int num2 = 5;
int sum = calculator.add(num1, num2);
std::cout << "Sum: " << sum << std::endl;

int difference = calculator.subtract(num1, num2);


std::cout << "Difference: " << difference << std::endl;

int product = calculator.multiply(num1, num2);


std::cout << "Product: " << product << std::endl;

int quotient = calculator.divide(num1, num2);


std::cout << "Quotient: " << quotient << 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;
}
};

class Beta : public Alpha {


private:
int beta;

public:
Beta(int a, int b) : Alpha(a), beta(b) {
}

void displayBeta() {
displayAlpha();
std::cout << "Beta: " << beta << std::endl;
}
};

class Gamma : public Alpha {


private:
int gamma;

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()) {

std::cerr << "Error: File could not be created." << std::endl;

return 1;

std::string textToWrite = "This is some text written to the file.";

outputFile << textToWrite << std::endl;

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;

while (std::getline(inputFile, 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() {

time_t currentTime = time(0);

tm* timeInfo = localtime(&currentTime);

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();

std::ifstream inFile("time_data.bin", std::ios::binary);

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);

std::cout << "Read Time: " << asctime(&readTimeInfo);

return 0;

OUTPUT:

Original Time: Wed Nov 8 14:07:24 2023

Read Time: Wed Nov 8 14:07:24 2023


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>

int divide(int numerator, int denominator) {

if (denominator == 0) {
throw std::runtime_error("Division by zero is not allowed.");
}
return numerator / denominator;
}

int main() {
int numerator, denominator;

std::cout << "Enter numerator: ";


std::cin >> numerator;

std::cout << "Enter denominator: ";


std::cin >> 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 {

std::vector<int> numbers = {1, 2, 3, 4, 5};

int index;

std::cout << "Enter an index to access an element in the array: ";

std::cin >> index;

if (index < 0 || index >= numbers.size()) {

throw std::out_of_range("Index is out of bounds.");

int element = numbers.at(index);

std::cout << "Element at index " << index << " is: " << element << std::endl;

} catch (const std::out_of_range &ex) {

std::cerr << "Exception caught: " << ex.what() << std::endl;

return 0;

OUTPUT:

Enter an index to access an element in the array: 3


Element at index 3 is: 4

Enter an index to access an element in the array: 7

Exception caught: Index is out of bounds.

You might also like