[go: up one dir, main page]

0% found this document useful (0 votes)
605 views23 pages

Oops

This document contains summaries of 6 experiments on object oriented programming concepts in C++. The experiments cover: 1) Finding the largest of three numbers 2) Sorting an array in ascending and descending order 3) Using classes to display student data including name, roll number, marks and total score 4) Creating a bank account class to print account details, deposit, withdraw and balance 5) Demonstrating function overloading for adding integers and doubles 6) Overloading the unary minus operator for a space class

Uploaded by

Janani Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
605 views23 pages

Oops

This document contains summaries of 6 experiments on object oriented programming concepts in C++. The experiments cover: 1) Finding the largest of three numbers 2) Sorting an array in ascending and descending order 3) Using classes to display student data including name, roll number, marks and total score 4) Creating a bank account class to print account details, deposit, withdraw and balance 5) Demonstrating function overloading for adding integers and doubles 6) Overloading the unary minus operator for a space class

Uploaded by

Janani Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

BCS306B Object oriented programming with C++

EXPERIMENT NO : 01
Develop a C++ program to find the largest of three numbers.

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the values for a,b,c:"<<endl;
cin>>a>>b>>c;
if(a>b&&a>c)
{
cout<<"a is bigger"<<endl;
}
if(b>a&&b>c)
{
cout<<"b is bigger"<<endl;
}
else
cout<<"c is bigger"<<endl;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Page | 1
BCS306B Object oriented programming with C++

EXPERIMENT NO : 02
Develop a C++ program to sort the elements in ascending and descending order.

#include<iostream>
using namespace std;
#define MAX 100
void sort(int arr[],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"\nsorted(descending order)array elements:"<<endl;
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];

Department of Computer Science and Engineering


Page | 2
BCS306B Object oriented programming with C++

arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"\nsorted(Ascending order)array elements:"<<endl;
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
int main()
{
int n,i;
int arr[MAX];
cout<<"Enter total number of elements to read:";
cin>>n;
cout<<"Enter array elements:"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,n);
return 0;
}
OUTPUT:

Department of Computer Science and Engineering


Page | 3
BCS306B Object oriented programming with C++

EXPERIMENT NO : 03
Develop a C++ program using classes to display student name, roll number, marks
obtained in two subjects and total score of student.

#include<iostream>
using namespace std;
class student
{
char Rollno[10];
string Name;
int m1,m2;
public:
void read_data();
void display();
};
void student::read_data()
{
cout<<"Enter the student Roll number:";
cin>>Rollno;
cout<<"Enter the student Name:";
cin>>Name;
cout<<"enter the marks in two subject:";
cin>>m1>>m2;
}
void student::display()
{
cout<<"Rollno is:\t"<<Rollno<<endl;
cout<<"Name is:\t"<<Name<<endl;
cout<<"marks1 is:\t"<<m1<<endl;
cout<<"marks2 is:\t"<<m2<<endl;
cout<<"total marks is:\t"<<m1+m2<<endl;

Department of Computer Science and Engineering


Page | 4
BCS306B Object oriented programming with C++

}
int main()
{
student s[10];
int n,i;
cout<<"enter the number of student:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the details of student:"<<i+1<<endl;
s[i].read_data();
}
for(i=0;i<n;i++)
{
cout<<"-------------------------"<<endl;
cout<<"details of student:"<<i+1<<endl;
cout<<"-------------------------"<<endl;
s[i].display();
}
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Page | 5
BCS306B Object oriented programming with C++

EXPERIMENT NO : 04
Develop a C++ program for a bank empolyee 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>
using namespace std;
class Bank
{
int acc_no;
string name;
double balance;
public:Bank(int,string,double);
void deposit();
void withdraw() ;
void DisplayAccount();
};
Bank::Bank(int accno,string Ename,double bal)
{
acc_no=accno;
name=Ename;
balance=bal;
}
void Bank::deposit()
{
int DAmount;
cout<<"\nEnter Deposit Amount:";
cin>>DAmount;
balance+=DAmount;
cout<<"Deposit successful.Current balance:"<< balance<<std::endl;
}

Department of Computer Science and Engineering


Page | 6
BCS306B Object oriented programming with C++

void Bank::withdraw()
{
int WAmount;
cout<<"\nEnter Withdraw Amount:";
cin>>WAmount;
if(WAmount<=balance)
{
balance-=WAmount;
cout<<"Withdrawal successful.Current balance:"<<balance<<endl;
}
else
{
cout<<"Insufficient balance.cannot withdraw."<<endl;
}
}
void Bank::DisplayAccount()
{
cout<<"\nAccount details:"<<endl;
cout<<"-------------------"<<endl;
cout<<"Account Number:"<<acc_no<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Balance:"<<balance<<endl;
if(balance<500)
{
cout<<"Invalid Balance.The balance must be more than 500."<<endl;
exit(0);
}
}
int main()
{
int acc_no;

Department of Computer Science and Engineering


Page | 7
BCS306B Object oriented programming with C++

char name[100];
float bal;
cout<<"Enter Account Details:"<<endl;
cout<<"Accout No:";
cin>>acc_no;
cout<<"Name:";
cin>>name;
cout<<"Balance:";
cin>>bal;
Bank b1(acc_no,name,bal);
b1.DisplayAccount();
b1.deposit();
b1.withdraw();
}
OUTPUT:

Department of Computer Science and Engineering


Page | 8
BCS306B Object oriented programming with C++

EXPERIMENT NO : 05
Develop a C++ program to demonstrate function overloading for the following prototypes.

#include<iostream>
using namespace std;
int add(int a,int b);
double add(double a,double b);
int main()
{
cout<<add(10,20)<<"\n";
cout<<add(2.5,3.7)<<"\n";
return 0;
}
int add(int a,int b)
{
return(a+b);
}
double add(double a,double b)
{
return(a+b);
}

OUTPUT:

Department of Computer Science and Engineering


Page | 9
BCS306B Object oriented programming with C++

EXPERIMENT NO : 06
Develop a C++ program using Operator Overloading for overloading Unary minus operator.

#include<iostream>
using namespace std;
class space
{
int x;
int y;
int z;
public: void getdata(int a,int b,int c);
void display();
void operator-();
};
void space:: getdata(int a,int b, int c)
{
x=a;
y=b;
z=c;
}
void space:: display()
{
std::cout<< x <<" ";
std::cout<< y <<" ";
std::cout<< z <<"\n";
}
void space :: operator-()
{
x=-x;
y=-y;
z=-z;

Department of Computer Science and Engineering


Page | 10
BCS306B Object oriented programming with C++

}
int main()
{
space s;
s.getdata(10,-20,30);
cout<<"S :";
s.display();
-s;
cout<<"S :";
s.display();
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Page | 11
BCS306B Object oriented programming with C++

EXPERIMENT NO : 07
Develop a C++ program to implement Multiple inheritance for performing arithmetic
operation of two numbers.

#include<iostream>
using namespace std;
class add
{
public:
int x=20;
int y=30;
void sum()
{
cout<<"The sum of "<<x<<" and "<<y<<" is "<<x+y<<endl;
}
};
class mul
{
public:
int a=20;
int b=30;
void multi()
{
cout<<"The multiplication of "<<a<<" and "<<b<<" is "<<a*b<<endl;
}
};
class sub
{
public:
int a=50;
int b=30;

Department of Computer Science and Engineering


Page | 12
BCS306B Object oriented programming with C++

void substr()
{
cout<<"The substraction of "<<a<<" and "<<b<<" is "<<a-b<<endl;
}
};
class div
{
public:
int a=150;
int b=30;
void divid()
{
cout<<"the division of "<<a<<" and "<<b<<" is "<<a/b<<endl;
}
};
class derived:public add,public div,public sub,public mul
{
public:
int p=12;
int q=5;
void mod()
{
cout<<"The modulus of "<<p<<" and "<<q<<" is "<<p%q<<endl;
}
};
int main()
{
derived dr;
dr.mod();
dr.sum();
dr.multi();

Department of Computer Science and Engineering


Page | 13
BCS306B Object oriented programming with C++

dr.divid();
dr.substr();
}

OUTPUT:

Department of Computer Science and Engineering


Page | 14
BCS306B Object oriented programming with C++

EXPERIMENT NO : 08
Develop a C++ program using Constructor in Derived classes to initialize alpha, beta and
gamma and display corresponding values.

#include<iostream>
using namespace std;
class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"Alpha Initialized\n";
}
void show_x()
{
cout<<"\n x="<<x ;
}
};
class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"Beta initializes\n";
}
void show_y()
{

Department of Computer Science and Engineering


Page | 15
BCS306B Object oriented programming with C++

cout<<"\n y="<<y;
}
};
class gamma:public beta,public alpha
{
int m,n,c,d;
public:
gamma(int a,float b,int c,int d):alpha(a),beta(b)
{
m=c;
n=d;
cout<<"gamma initialized\n";
}
void show_mn()
{
cout<<"\n m="<<m;
cout<<"\n n="<<n;
}
};
int main()
{
gamma g(5,7.65,30,100);
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

Department of Computer Science and Engineering


Page | 16
BCS306B Object oriented programming with C++

OUTPUT:

EXPERIMENT NO : 09

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>
using namespace std;
int main()
{
fstream newfile;
newfile.open("File1.txt",ios::out);
if(!newfile)
{
cout<<"File is not created\n";
}
else

Department of Computer Science and Engineering


Page | 17
BCS306B Object oriented programming with C++

{
cout<<"File is created\n";
}
newfile<<"Welcome to file handling\n";
newfile.close();
newfile.open("File1.txt",ios::in);
string ch;
while(getline(newfile,ch))
{
cout<<ch;
}

newfile.close();
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Page | 18
BCS306B Object oriented programming with C++

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

#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
using namespace std;
int main()
{
fstream date_time;
date_time.open("current.bin", ios::binary| ios::out);
time_t now = time(0);
char* dt = ctime(&now);
date_time<<dt;
date_time.close();
date_time.open("current.bin", ios::binary| ios::in);
int size=strlen(dt);
char buffer[size-1];
date_time.read(buffer, sizeof(buffer));
date_time.close();
cout<<buffer;
return 0;
}

OUTPUT:

Department of Computer Science and Engineering


Page | 19
BCS306B Object oriented programming with C++

EXPERIMENT NO : 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 main()
{
float numerator, denominator, result;
cout<<"enter the value of numerator\n";
cin>>numerator;
cout<<"enter the value of denomenator\n";
cin>>denominator;
try
{
if (denominator == 0)
throw 0;

result = numerator / denominator;


cout << numerator << " / " << denominator << " = " << result << endl;

}
catch(int x)
{
cout<<"exception caught:divided by "<<x;
}
return 0;
}

Department of Computer Science and Engineering


Page | 20
BCS306B Object oriented programming with C++

OUTPUT:

Department of Computer Science and Engineering


Page | 21
BCS306B Object oriented programming with C++

EXPERIMENT NO : 12
Develop a C++ program that handles array out of bounds exception using C++.

#include <iostream>
#include <stdexcept>
using namespace std;
int safeArrayAccess(int arr[], int size, int index)
{
if (index < 0 || index >= size)
{
throw out_of_range("Index out of bounds");
}
return arr[index];
}
int main()
{
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers)/ sizeof(numbers[0]);
int index;
cout << "Enter an index: ";
cin >> index;
try
{
int result = safeArrayAccess(numbers, size, index);
cout << "Value at index " << index << ": " << result <<endl;
}
catch (exception &e)
{
cout<< "Exception: " << e.what() <<endl;
}
return 0;

Department of Computer Science and Engineering


Page | 22
BCS306B Object oriented programming with C++

OUTPUT:

Department of Computer Science and Engineering


Page | 23

You might also like