BCS306B Manual
BCS306B Manual
www.brindavancollege.com
III Semester
OBJECT ORIENTED PROGRAMMING WITH C++ LAB
BCS306B
ACADEMIC YEAR
2023 – 2024
LABORATORY MANUAL
PREPARED BY
Prof. Nazia Nusrath Ul Ain
Brindavan College of Engineering
Department of IOT & Cyber Security including BCT
III Semester
BCS306B
ACADEMIC YEAR
2023 – 2024
BATCH :
PREPARED BY
I
Brindavan College of Engineering
Department of IOT & Cyber Security including BCT
LABORATORY CERTIFICATE
MARKS
Date:
II
Brindavan College of Engineering
Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru – 560063
Affiliated to VTU Belagavi, Approved by AICTE, New Delhi, India
Accredited B++ level by NAAC
DEPARTMENT VISION
DEPARTMENT MISSION
III
Brindavan College of Engineering
Dwarakanagar, Bagalur Main Road, Yelahanka, Bengaluru – 560063
Affiliated to VTU Belagavi, Approved by AICTE, New Delhi, India
Accredited B++ level by NAAC
DOs
Be on time and students should carry observation and completed records in all aspects.
Dress code & wearing ID card is compulsory.
Electronic gadgets are not allowed inside the lab.
Students should be at their concerned desktop.
After execution the students should get it verified by the concerned faculty.
The executed results should be noted in their observations and get it verified by the
concerned faculty.
Observe good housekeeping practices. Keep the equipment’s in proper place after the
conduction.
Students must ensure that all the switches are in the OFF position; desktop is shutdown
properly after completion of the assignments.
For circuits lab the components must returned properly.
For power electronics lab wearing shoes is compulsory.
DON’Ts
PREFACE
IV
In the modern world of information technology the object oriented programming has become
the most preferred approach for software development .It offers a powerful way to cope up
with complexity of real world problems. Among the OOP languages available C++ is the
primitive language which develops fundamental understanding of OOP concepts .This course
enable students to develop programs in “C++ using Object Oriented Programming approach”.
V
OBJECT ORIENTED PROGRAMMING WITH C++ LAB
Course objectives:
To understand object-oriented programming using C++and Gain
knowledge about the capability to store information together in an object.
To illustrate the capability of a class to rely upon another class and
functions.
To create and process data in files using file I/O functions
To understand the generic programming features of C++ including
Exception handling
Laboratory Experiments
VI
add(double a, double b)
Course outcomes:
On the completion of this laboratory course, the students will be able to:
VII
1. Develop a C++ program to find the largest of three numbers
#include<iostream.h>
int main() {
double n1, n2, n3;
cout << "Enter 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 three numbers:
10
20
15
Largest number is: 20
1
2. Develop a C++ program to sort the elements in ascending and descending
order
#include <iostream.h>
int main(){
int num[100],n;
int i,j,temp;
cout<<"Enter n for the numbers you want to sort"<<”\n”;
cin>>n;
for(i=0;i<n;i++){
cout<<"Enter numbers:"<<”\n”;
cin>>num[i];}
for(i=0;i<n;i++){
for(j=i+1 ;j<n;j++){
if(num[i]> num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}}
cout<<"Ascending order :”<<”\n”;
for(i=0;i<n;i++)
{
cout<<" "<<num[i] <<”\n”;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(num[i]<num[j]){
temp=num[i]; num[i]=num[j];
num[j]=temp;}}}
cout<<" Descending order : ”<<”\n”;
for(i=0;i<n;i++)
{
cout<<" "<<num[i] <<”\n”;
2
}
return 0;
}
Output
Enter n for the numbers you want to sort
5
Enter numbers
10
21
13
14
11
Ascending order:
10
11
13
14
21
Descending order :
21
14
13
11
10
3
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.h>
#include <conio.h>
#include <string.h>
class Student {
private:
char name[50];
int rollNumber;
int marksSubject1;
int marksSubject2;
public:
void setDetails(char* studentName, int roll, int marks1, int marks2) {
strcpy(name, studentName);
rollNumber = roll;
marksSubject1 = marks1;
marksSubject2 = marks2;
}
void displayDetails() {
clrscr(); // Clear the screen (Turbo C++ specific)
cout << "Student Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Marks in Subject 1: " << marksSubject1 << endl;
cout << "Marks in Subject 2: " << marksSubject2 << endl;
cout << "Total Score: " << (marksSubject1 + marksSubject2) << endl;
}
};
int main() {
Student student;
char name[50];
4
int roll, marks1, marks2;
Output:
Enter student name: Samuel
Enter roll number: 0123
Enter marks obtained in subject 1: 35
Enter marks obtained in subject 2: 45
Student Name: Samuel
Roll Number: 0123
Marks in Subject 1: 35
Marks in Subject 2: 45
Total Score: 80
5
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.h>
#include<conio.h>
#include<string.h>
class BankEmployee{
private:
char name[50];
long accountNumber;
float balance;
public:
BankEmployee(const char ename[],long accno,float inibal)
{
strcpy(name,ename);
accountNumber=accno;
if(inibal<500){
cout<<"Invalid balance enter initial balance above 500"<<endl;
cin>>balance
}
else{
balance=inibal;
}
}
void displayDetails(){
cout<<"Employee Name: "<<name<<endl;
cout<<"Account Number: "<<accountNumber<<endl;
cout<<"Balance: "<<balance<<endl;
}
void withdraw(float amount){
if (balance >=amount){
balance - =amount;
cout<<"Balance after withdrawal: "<<balance<<endl;
}
6
else{
cout<<"Insufficient Balance"<<endl;
}
}
void deposit(float amount){
balance +=amount;
cout<<"Balance after deposit:"<<balance<<endl;
}
};
int main(){
char name[50];
long accno;
float inibal, wamount, damount;
clrscr();
cout<<"Enter Employee Name:";
cin.getline(name, sizeof(name));
cout<<"Enter Account Number:";
cin>>accnor;
cout<<"Enter initial balance:";
cin>>inibal;
BankEmployee employee(name,accountNumber, inibal);
cout<<endl;
cout<<"Employee Details:"<<endl;
employee.displayDetails();
cout<<"Enter the amount to withdraw: ";
cin>>wamount;
employee.withdraw(wamount);
cout<<"Enter the amount to deposit: ";
cin>>damount;
employee.deposit(damount);
getch();
return 0;
}
7
Output:
Enter Employee Name: Rakesh S
Enter Account Number: 2010268
Enter initial balance: 1000
Employee Details:
Employee Name: Rakesh S
8
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.h>
#include<conio.h>
int add(int a,int b)
{
return a+b;
}
int main()
{
clrscr();
int intResult=add(5,3);
double doubleResult=add(2.5,3.7);
cout<<"Result of adding two integers: "<<intResult<<endl;
cout<<"Result of adding two double: "<<doubleResult<<endl;
getch();
return 0;
}
Output:
9
6. Develop a C++ program using Operator Overloading for overloading Unary
minus operator
#include<iostream.h>
#include<conio.h>
class Number{
private:
int value;
public:
Number():value(0){}
Number(int val):value(val){}
Number operator-() const{
Number result(-value);
return result;
}
void display() const{
cout<<"Value: "<<value<<endl;
}
};
int main(){
clrscr();
Number num1(10);
Number num2=-num1;
cout<<"Original number: ";
num1.display();
cout<<"After Unary Minus: ";
num2.display();
getch();
return 0;
}
Output:
Original number: Value : 10
After Unary Minus: Value : -10
10
7. Develop a C++ program to implement Multiple inheritance for performing
arithmetic operation of two numbers
#include<iostream.h>
#include<conio.h>
class addition
{
public:
int add(int a,int b){
return a+b;
}
};
class subtraction{
cout<<"Sum= "<<add(a,b)<<endl;
cout<<"Difference= "<<substract(a,b)<<endl;
}
};
int main()
{
clrscr();
int num1,num2;
cout<<"Enter two number: "<<endl;
cin>>num1>>num2;
arithmetic obj;
11
obj.display(num1,num2);
getch();
return 0;
}
Output:
Enter two number: 10 20
Sum= 30
Difference = -10
12
8. Develop a C++ program using Constructor in Derived classes to initialize
alpha, beta and gamma and display corresponding values.
#include<iostream.h>
#include<conio.h>
class Alpha{
protected: int alpha;
public: Alpha(int a):alpha(a){}
};
class Beta:public Alpha{
protected: int beta;
public: Beta(int a,int b):Alpha(a),beta(b){}
};
class Gamma:public Beta
{
private: int gamma;
public: Gamma(int a,int b,int c):Beta(a,b),gamma(c)
{}
void displayvalues(){
cout<<"Alpha: "<<alpha<<endl;
cout<<"Beta: "<<beta<<endl;
cout<<"Gamma: "<<gamma<<endl;
}
};
int main(){
clrscr();
Gamma obj(10,20,30);
obj.displayvalues();
getch();
return 0;}
Output:
Alpha: 10
Beta: 20
Gamma:30
13
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.h>
#include<conio.h>
#include<fstream.h>
int main(){
clrscr();
ofstream outFile("sample.txt");
if(!outFile){
cout<<"Error creating the file!"<<endl;
getch();
return 1;
}
outFile<<"Hello this is some text written to the file.\n";
outFile<<"This is another line in the file.";
outFile.close();
ifstream inFile("sample.txt");
if(!inFile){
cout<<"Error opening the file!"<<endl;
getch();
return 1;
}
cout<<"Contents of the file:"<<endl;
char ch;
while(inFile.get(ch)){
cout<<ch;
}
inFile.close();
getch();
return 0;}
Output:
Contents of the file:
Hello this is some text written to the file
This is another line in the file.
14
10.Develop a C++ program to write and read time in/from binary file using
fstream.
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h> //for setfill() and setw()
char str[10];
fstream file;
file.open(FILE_NAME, ios::out|ios::binary);
if(!file){
cout<<"Error in creating file!!!"<<endl;
return;
}
15
void readTime(int *h,int *m, int *s){
char str[10];
int inH,inM,inS;
fstream finC;
finC.open(FILE_NAME,ios::in|ios::binary);
if(!finC){
cout<<"Error in file opening..."<<endl;
return;
}
if(finC.read((char*)str,sizeof(str))){
//extract time values from the file
sscanf(str,"%02d:%02d:%02d",&inH,&inM,&inS);
//assign time into variables, which are passing in function
*h=inH;
*m=inM;
*s=inS;
}
finC.close();
}
int main(){
int m,h,s;
cout<<"Enter time:\n";
cout<<"Enter hour: "; cin>>h;
cout<<"Enter minute: "; cin>>m;
cout<<"Enter second: "; cin>>s;
cout<<"The time is
"<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<":"<<setw(2)<<setfill('0')
<<s<<endl;
return 0;
}
Output:
Enter time:
Enter hour: 10
Enter minute: 22
Enter second: 45
Time 10:22:45 has been written into file.
The time is 10:22:45
17
11.Develop a function which throws a division by zero exception and catch it in
the catch block. Write a C++ program to demonstrate usage of try , catch and
throw to handle exception.
#include <iostream.h>
#include <stdexcept.h>
int main() {
int numerator, denominator, result;
cout << "Enter First Number: ";
cin >> numerator;
cout << "Enter Second Number: ";
cin >> denominator;
try {
if (denominator == 0) {
throw runtime_error("Division by zero error");
}
result = numerator / denominator;
cout << "Result: " << result << endl;
} catch (const runtime_error& ex) {
cerr << "Exception caught: " << ex.what() << endl;
return 1; // Return an error code, if desired
}
return 0;
}
Output:
Enter First Number: 20
Enter Second Number:0
Exception caught:
Division by zero error
18
12.Develop a C++ program that handles Array Out of Bounds exception using
C++
include <iostream.h>
int main() {
const int arraySize = 5;
int myArray[arraySize];
try {
int indexOutOfRange = myArray[arraySize];
cout << "This line won't be executed." << endl;
} catch (...) {
cerr << "Exception caught: Array index out of bounds." << endl;
}
return 0;
}
Output:
19