BHAGWAN PARSHURAM INSTITUTE OF TECHNOLOGY
OBJECT ORIENTED PROGRAMMING LAB
(ETCS-258)
PRACTICAL FILE
B-TECH(4TH SEMESTER)
SUBMITTED TO:
Dr Nitish Pathak
Associate Professor, Department of Information
Technology
Submitted by:
Palak Bansal
17-IT-19
PALAK BANSAL
01720803119
OBJECT ORIENTED PROGRAMMING
Practical Record
NAME: Palak Bansal
UNIVERSITY ROLL NO.: 01720803119
BRANCH : INFORMATION TECHNOLOGY
S.NO PROGRAM NAME DATE OF DATE OF PAGE REMARKS MARKS
PERFORMANCE CHECKING NO
1 Write a program to 20-3-21 12-4-21 6
determine the factorial
of a number using
recursion
2 Write the program to 20-3-21 12-4-21 8
determine the greatest
of three number using
Macros
3 Using the concept of 20-3-21 12-4-21 10
the fucntion
overloading write the
program to calculate
the area of
Triangle,Circle,and
Rectangle
4 Write a program for 20-3-21 12-4-21 15
multiplication of two
matrix using OOPs.
5 Write a program to 20-3-21 12-4-21 19
enter the name,
address, age and salary
using inline function.
2
PALAK BANSAL
01720803119
6 Write a program to 20-3-21 12-4-21 22
create or delete object
using constructor and
destructor
7 Write a program to 20-3-21 12-4-21 25
show dynamic
allocation of memory
by using new and
delete operation.
8 Write a program to 27-3-21 19-4-21 28
find the factorial of
number using
constructor.
9 Write a program 27-3-21 19-4-21 31
perform addition of
two complex numbers
using constructor
overloading. The first
constructor which
takes no argument is
used to create objects
which are not
initialized, second
which takes one
argument is used to
initialize real and
imaginary part to equal
values and third which
takes two argument is
used to initialized real
and imaginary to two
different values
10 Write a program to 27-3-21 19-4-21 35
generate a Fibonacci
series using copy
constructor
11 Write a program to 3-4-21 19-4-21 39
find greatest of three
numbers using friend
function.
12 Write a program to 3-4-21 19-4-21 42
find the greatest of the
two numbers in two
3
PALAK BANSAL
01720803119
different classes using
friend function
13 Write a program of 10-4-21 46
Single Inheritance
14 Write a program of 10-4-21 48
Multiple Inheritance
15 Write a program of 10-4-21 50
Multi level Inheritance
16 Write a program to 10-4-21 52
overload increment
(++) and decrement (--)
operator
17 Write a program to 17-4-21 54
overload binary
operators
18 Write a program to 17-4-21 57
overload (<) operators
19 Write a program to 17-4-21 59
define the function
template for swapping
two items of various
data type such as int ,
float and character
20 Write a program to 17-4-21 61
define the function
template for
calculating the square
of given numbers of
various data type such
as int , float and
character
21 Write a program to 24-4-21 63
illustrate how template
function can be
overloaded
22 Write a program of 24-4-21 65
exceptional handling
with the using of try
and catch function
23 Write a program of 24-4-21 67
exceptional handling
with the using of
4
PALAK BANSAL
01720803119
multiple try and catch
function
24 Write a program of 8-5-21 69
generic function using
template
25 Write a program of 8-5-21 70
generic class using
template
26 Write a program of 15-5-21 72
Standard Template
Library (STL) using vector
27 Write a program of file 15-5-21 74
handling for
implementation of
ofstream and ifstream
classes
28 Write a program of file 15-5-21 75
handling for
implementation of
fstream
29 Write a program of 15-5-21 76
binary file for reading
and writing data present
in the file
30 Write a program of String 22-5-21 78
to implement the use of
function like strcat,
strlen, strcpy, and strcmp
31 Write a program of STL 22-5-21 80
Vector (push.back, begin,
cbegin)
32 Write a program of 22-5-21 82
Exceptional handling(2).
5
PALAK BANSAL
01720803119
6
PALAK BANSAL
01720803119
7
PALAK BANSAL
01720803119
8
PALAK BANSAL
01720803119
9
PALAK BANSAL
01720803119
10
PALAK BANSAL
01720803119
11
PALAK BANSAL
01720803119
12
PALAK BANSAL
01720803119
13
PALAK BANSAL
01720803119
14
PALAK BANSAL
01720803119
15
PALAK BANSAL
01720803119
16
PALAK BANSAL
01720803119
17
PALAK BANSAL
01720803119
18
PALAK BANSAL
01720803119
19
PALAK BANSAL
01720803119
20
PALAK BANSAL
01720803119
21
PALAK BANSAL
01720803119
22
PALAK BANSAL
01720803119
23
PALAK BANSAL
01720803119
24
PALAK BANSAL
01720803119
25
PALAK BANSAL
01720803119
26
PALAK BANSAL
01720803119
27
PALAK BANSAL
01720803119
28
PALAK BANSAL
01720803119
29
PALAK BANSAL
01720803119
30
PALAK BANSAL
01720803119
31
PALAK BANSAL
01720803119
32
PALAK BANSAL
01720803119
33
PALAK BANSAL
01720803119
34
PALAK BANSAL
01720803119
35
PALAK BANSAL
01720803119
36
PALAK BANSAL
01720803119
37
PALAK BANSAL
01720803119
38
PALAK BANSAL
01720803119
39
PALAK BANSAL
01720803119
40
PALAK BANSAL
01720803119
41
PALAK BANSAL
01720803119
42
PALAK BANSAL
01720803119
43
PALAK BANSAL
01720803119
44
PALAK BANSAL
01720803119
45
PALAK BANSAL
01720803119
Experiment No.-13
Aim: Write a program of Single Inheritance.
#include <iostream>
using namespace std;
class base
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = ";
cin >> x;
}
};
class derive : public base
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = ";
cin >> y;
}
void product()
{
cout<<"\n";
cout << "Product = " << x * y;
}
};
int main()
{
cout<<"\t SINGLE INHERITANCE\n"<<endl;
cout<<"\t PALAK BANSAL 17-IT-19"<<endl;
46
PALAK BANSAL
01720803119
derive a;
a.getdata();
a.readdata();
a.product();
return 0;
}
47
PALAK BANSAL
01720803119
Experiment No.-14
Aim: Write a program of Multiple Inheritance.
#include <iostream>
using namespace std;
class A {
public:
int a = 5;
A() {
cout << "Constructor for class A" << endl;
}
};
class B {
public:
int b = 10;
B() {
cout << "Constructor for class B" << endl;
}
};
class C: public A, public B {
public:
int c = 20;
C() {
cout << "Constructor for class C" << endl;
cout<<"Class C inherits from class A and class B" << endl;
}
};
int main() {
cout<<"\t MUILTIPLE INHERITANCE \n"<<endl;
cout<<"\t PALAK BANSAL 17-IT-19"<<endl;
C obj;
cout<<"a = "<< obj.a <<endl;
cout<<"b = "<< obj.b <<endl;
cout<<"c = "<< obj.c <<endl;
return 0;
}
48
PALAK BANSAL
01720803119
49
PALAK BANSAL
01720803119
Experiment No.-15
Aim: Write a program of Multi level Inheritance.
#include<iostream>
using namespace std;
class A
{
public:
char name[20];
char sex[10];
int rollno;
void get()
{
cout<<"\n Enter the name: ";
gets(name);
cout<<"\n Enter the sex: ";
gets(sex);
cout<<"\n Enter the value of rollno: ";
cin>>rollno;
}
};
class B:public A
{
public:
float height,weight;
void readdata()
{
cout<<"\n Enter the height: ";
cin>>height;
cout<<"\n Enter the weight: ";
cin>>weight;
}
};
class C:public B{
public:
void display()
50
PALAK BANSAL
01720803119
{ cout<<"\n";
cout<<"Name: "<<name<<endl;
cout<<"Sex: "<<sex<<endl;
cout<<"Rollno: "<<rollno<<endl;
cout<<"Height: "<<height<<endl;
cout<<"Weight: "<<weight<<endl;
}
};
int main()
{
cout<<"\t MULTILEVEL INHERITANCE\n"<<endl;
cout<<"\t PALAK BANSAL 17-IT-19"<<endl;
C a;
a.get();
a.readdata();
a.display();
return 0;
}
51
PALAK BANSAL
01720803119
Experiment No.-16
Aim: Write a program to overload increment (++) and decrement (--)
operator.
#include<iostream>
using namespace std;
class overloading
{
public:
int x;
void get()
{
cout<<"\n Enter the x: ";
cin>>x;
}
void display()
{
cout<<"\n Value of x: "<<x<<"\n";
}
overloading operator ++()
{
++x;
}
overloading operator --()
{
--x;
}
};
int main()
{
cout<<"\t OVERLOADING OF UNARY INCREMENT AND DECREMENT OPERATOR\n";
52
PALAK BANSAL
01720803119
cout<<"\t PALAK BANSAL 17-IT-19\n";
overloading obj;
obj.get();
obj.display();
++obj;
cout<<"\n Value after incrementing overloading: ";
obj.display();
cout<<"\n";
--obj;
cout<<"\n Value after decrementing overloading: ";
obj.display();
return 0;
}
53
PALAK BANSAL
01720803119
Experiment No.-17
Aim: Write a program to overload binary operators.
#include<iostream>
using namespace std;
class binary
{
public:
int x;
void get()
{
cout<<"\n Enter the value of x: ";
cin>>x;
}
void display()
{
cout<<"\n Value of x: "<<x<<"\n";
}
friend binary operator +(binary &b,binary &c)
{
binary d;
d.x=b.x + c.x;
return d;
}
friend binary operator -(binary &b,binary &c)
{
binary d;
d.x=b.x - c.x;
return d;
}
friend binary operator *(binary &b,binary &c)
{
binary d;
d.x=b.x * c.x;
54
PALAK BANSAL
01720803119
return d;
}
friend binary operator /(binary &b,binary &c)
{
binary d;
d.x=b.x / c.x;
return d;
}
};
int main()
{
cout<<"\t OVERLOADING OF BINARY OPERATOR \n\n";
cout<<"\t PALAK BANSAL 17-IT-19 \n";
binary b1,b2,b3;
b1.get();
b2.get();
cout<<"\n Using binary (+) operator overloading: ";
b3=b1 + b2;
b3.display();
cout<<"\n Using binary (-) operator overloading: ";
b3=b1 - b2;
b3.display();
cout<<"\n Using binary (*) operator overloading: ";
b3=b1 * b2;
b3.display();
cout<<"\n Using binary (/) operator overloading: ";
b3=b1 / b2;
b3.display();
return 0;
}
55
PALAK BANSAL
01720803119
56
PALAK BANSAL
01720803119
Experiment No.-18
Aim: Write a program to overload (<) operators.
#include<iostream>
using namespace std;
class LessThan
{
public:
int x;
void get(){
cout<<"\n Enter the value of x: ";
cin>>x;
}
LessThan operator <(LessThan &a)
{
if(x > a.x)
{
cout<<endl<<x<<" is greater than "<<a.x;
}else
{
cout<<endl<<x<<" is less than "<<a.x;
}
}
};
int main()
{
cout<<"\t PROGRAM OF OVERLOADING RELATIONAL OPERATORS \n\n";
cout<<"\t PALAK BANSAL 17-IT-19 \n";
LessThan l1,l2;
l1.get();
l2.get();
cout<<"\nUsing Relational Operator Overloading \n";
l1 < l2;
cout<<"\n";
return 0;
}
57
PALAK BANSAL
01720803119
58
PALAK BANSAL
01720803119
Experiment No.-19
Aim: Write a program to define the function template for swapping
two items of various data type such as int , float and character.
#include<iostream>
using namespace std;
template <class T>
void swapp(T&x,T&y)
{
T t;
t=x;
x=y;
y=t;
}
int main()
{
cout<<"\t TEMPLATES FOR SWAPPING TWO ITEMS OF DIFFERENT DATA TYPES\n\n";
cout<<"\t PALAK BANSAL 17-IT-19\n";
char ch1,ch2;
cout<<"\n Enter the two letters: ";
cin>>ch1>>ch2;
swapp(ch1,ch2);
cout<<" Values after swapping: "<<ch1<<" "<<ch2;
int n,m;
cout<<"\n\n Enter the two numbers: ";
cin>>n>>m;
swapp(n,m);
cout<<" Values after swapping: "<<n<<" "<<m;
float a,b;
cout<<"\n\n Enter the two numbers(float): ";
cin>>a>>b;
swapp(a,b);
cout<<" Values after swapping: "<<a<<" "<<b<<"\n";
return 0;
}
59
PALAK BANSAL
01720803119
60
PALAK BANSAL
01720803119
Experiment No.-20
Aim: Write a program to define the function template for calculating
the square of given numbers of various data type such as int , float
and character.
#include<iostream>
using namespace std;
template<class S>
void sqaure(S &a)
{
S s;
s=a * a;
cout<<" Value: "<<s<<endl;
}
int main()
{
cout<<"\t TEMPLATE FOR CALCULATION SQUARE OF NUMBER OF DIFFERENT DATA TYPES\n\n
";
cout<<"\tPALAK BANSAL 17-IT-19\n";
int a;
cout<<"Enter the number (Integer): ";
cin>>a;
sqaure(a);
float b;
cout<<"\n Enter the number (float): ";
cin>>b;
sqaure(b);
double d;
cout<<"\n Enter the number (double): ";
cin>>d;
sqaure(d);
return 0;
}
61
PALAK BANSAL
01720803119
62
PALAK BANSAL
01720803119
Experiment No.-21
Aim: Write a program to illustrate how template function can be
overloaded.
#include<iostream>
using namespace std;
template<class A>
void print(A data)
{
cout<<data<<endl;
}
template<class A>
void print(A data,int n)
{
for(int i=0;i<n;i++)
{
cout<<data<<endl;
}
}
int main()
{
cout<<"\t OVERLOADING OF TEMPLATE FUNCTION\n\n";
cout<<"\t PALAK BANSAL 17-IT-19\n";
print(7);
print("Object Oriented Program",2);
print(3);
print(600,2);
return 0;
}
63
PALAK BANSAL
01720803119
64
PALAK BANSAL
01720803119
Experiment No.-22
Aim: Write a program of exceptional handling with the using of try
and catch function.
#include <iostream>
using namespace std;
float division(int x, int y) {
if( y == 0 ) {
throw "\n Attempted to divide by zero!";
}
return (x/y);
}
int main () {
int i,j;
cout<<"\t TRY ANF CATCH FUNCTION (EXCEPTIONAL HANDLING)\n\n";
cout<<"\t Palak Bansal 17-IT-19\n";
cout<<"Enter the value of i: ";
cin>>i;
cout<<"\nEnter the value of j: ";
cin>>j;
float k = 0;
try {
k = division(i, j);
cout << k << endl;
}catch (const char* e) {
cerr << e << endl;
}
return 0;
}
65
PALAK BANSAL
01720803119
66
PALAK BANSAL
01720803119
Experiment No.-23
Aim: Write a program of exceptional handling with the using of
multiple try and catch function.
#include<iostream>
using namespace std;
void test(int x) {
try {
if (x > 0)
throw x;
else
throw 'x';
} catch (int x) {
cout << "\nCatch a integer and that integer is:" << x;
} catch (char x) {
cout << "\n\nCatch a character and that character is:" << x<<"\n";
}
}
int main() {
cout<<"\t MULTIPLE TRY AND CATCH (EXCEPTIONAL HANDLING)\n";
cout<<"\t Palak Bansal 17-IT-19\n";
test(10);
test(0);
return 0;
}
67
PALAK BANSAL
01720803119
68
PALAK BANSAL
01720803119
Experiment No.-24
Aim: Write a program of generic function using template.
#include <iostream>
using namespace std;
template <typename T>
T myMax(T x, T y)
{
return (x > y) ? x : y;
}
int main()
{
cout<<"\t GENERIC FUNCTION USING TEMPLATE\n\n";
cout<<"\t Palak Bansal 17-IT-19\n";
cout << myMax<int>(3, 7) << endl;
cout << myMax<double>(3.0, 7.0) << endl;
cout << myMax<char>('g', 'e') << endl;
return 0;
}
69
PALAK BANSAL
01720803119
Experiment No.-25
Aim: Write a program of generic class using template.
#include <iostream>
using namespace std;
template <typename T>
class Array {
private:
T* ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
template <typename T>
Array<T>::Array(T arr[], int s)
{
ptr = new T[s];
size = s;
for (int i = 0; i < size; i++)
ptr[i] = arr[i];
}
template <typename T>
void Array<T>::print()
{
for (int i = 0; i < size; i++)
cout << " " << *(ptr + i);
cout << endl;
}
int main()
{
cout<<"\t PROGRAM OF GENERIC CLASS USING TEMPLATE\n";
70
PALAK BANSAL
01720803119
cout<<"\t PALAK BANSAL 17-IT-19\n";
int arr[5] = { 1,4,6,8,2 };
Array<int> a(arr, 5);
a.print();
return 0;
}
71
PALAK BANSAL
01720803119
Experiment No.-26
Aim: Write a program of Standard Template Library (STL) using vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout<<"\t STANDARD TEMPLATE LIBRARY (VECTOR) PROGRAM\n\n";
cout<<"\t Palak Bansal 17-IT-19\n";
vector<int> vec;
int i;
cout << "vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++) {
vec.push_back(i);
}
cout << "extended vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++) {
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout << "value of v = " << *v << endl;
v++;
}
return 0;
}
72
PALAK BANSAL
01720803119
73
PALAK BANSAL
01720803119
Experiment No.-27
Aim: Write a program of file handling for implementation of ofstream
and ifstream classes.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
string line;
fout.open("sample.txt");
while (fout) {
getline(cin, line);
if (line == "-1")
break;
fout << line << endl;
}
fout.close();
ifstream fin;
fin.open("sample.txt");
while (fin) {
getline(fin, line);
cout << line << endl;
}
fin.close();
return 0;
}
74
PALAK BANSAL
01720803119
Experiment No.-28
Aim: Write a program of file handling for implementation of fstream.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fio;
string line;
fio.open("sample.txt", ios::trunc | ios::out | ios::in);
while (fio) {
getline(cin, line);
if (line == "-1")
break;
fio << line << endl;
}
fio.seekg(0, ios::beg);
while (fio) {
getline(fio, line);
cout << line << endl;
}
fio.close();
return 0;
}
75
PALAK BANSAL
01720803119
Experiment No.-29
Aim: Write a program of binary file for reading and writing data
present in the file.
#include<iostream>
#include<fstream>
using namespace std;
struct Student {
int roll_no;
string name;
};
int main() {
cout<<"\t BINARY FILE (READ,WRITE, AND APPEND) PROGRAM\n";
cout<<"\t PALAK BANSAL 17-IT-19\n";
ofstream wf("student.dat", ios::out | ios::binary);
if(!wf) {
cout << "Cannot open file!" << endl;
return 1;
}
Student wstu[3];
wstu[0].roll_no = 1;
wstu[0].name = "Jack";
wstu[1].roll_no = 2;
wstu[1].name = "Scott";
wstu[2].roll_no = 3;
wstu[2].name = "Rachel";
for(int i = 0; i < 3; i++)
wf.write((char *) &wstu[i], sizeof(Student));
wf.close();
if(!wf.good()) {
cout << "Error occurred at writing time!" << endl;
return 1;
}
ifstream rf("student.dat", ios::out | ios::binary);
if(!rf) {
cout << "Cannot open file!" << endl;
return 1;
76
PALAK BANSAL
01720803119
}
Student rstu[3];
for(int i = 0; i < 3; i++)
rf.read((char *) &rstu[i], sizeof(Student));
rf.close();
if(!rf.good()) {
cout << "Error occurred at reading time!" << endl;
return 1;
}
cout<<"Student's Details:"<<endl;
for(int i=0; i < 3; i++) {
cout << "Roll No: " << wstu[i].roll_no << endl;
cout << "Name: " << wstu[i].name << endl;
cout << endl;
}
return 0;
}
77
PALAK BANSAL
01720803119
Experiment No.-30
Aim: Write a program of String to implement the use of function like
strcat, strlen, strcpy, and strcmp.
#include <iostream>
#include <cstring>
using namespace std;
int main () {
char str1[10];
char str2[10];
char str3[10];
int len ;
cout<<"\t PROGRAM OF STRINGS AND ITS FUNCTION (STRCPY,STRCAT,STRLEN,STRCMP) \n";
cout<<"\t Palak Bansal 17-IT-19\n";
cout<<"\n Enter the first String: ";
gets(str1);
cout<<"\n Enter the second String: ";
gets(str2);
strcpy( str3, str1);
cout << "\n strcpy( str3, str1) : " << str3 << endl;
strcat( str1, str2);
cout << "\n strcat( str1, str2): " << str1 << endl;
len = strlen(str3);
cout << "\n strlen(str3) : " << len << endl;
int result = strncmp( str1, str2,2);
if (result == 0) {
cout<<("\n str1 is equal to str2 upto num characters\n");
}
else if (result > 0)
cout<<("\n str1 is greater than str2\n")<<endl;
else
cout<<("\n str2 is greater than str1\n")<<endl;
return 0;
}
78
PALAK BANSAL
01720803119
79
PALAK BANSAL
01720803119
Experiment No.-31
Aim: Write a program of STL Vector (push.back, begin, cbegin).
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout<<"\t PROGRAM ON VECTOR\n\n";
cout<<"\t Palak Bansal-IT-19\n";
vector<int> nums;
for (int a = 1; a <= 5; a++){
nums.push_back(a);
}
cout << "Output from begin and end: \n";
for (auto a = nums.begin(); a != nums.end(); ++a){
cout << *a << " ";
}
cout << "\n\nOutput from cbegin and cend: \n";
for (auto a = nums.cbegin(); a != nums.cend(); ++a){
cout << *a << " ";
}
return 0;
}
80
PALAK BANSAL
01720803119
81
PALAK BANSAL
01720803119
Experiment No.-32
Aim: Write a program of Exceptional handling(2).
#include<iostream>
using namespace std;
int main(){
int x=-1;
cout<<"\t PROGRAM OF EXCEPTION HANDLING(2)"<<endl<<endl;
cout<<"\t Palak Bansal 17-IT-19"<<endl;
cout<<"Before try block "<<endl<<endl;
try{
cout<<"Inside the try block "<<endl<<endl;
if (x<0){
throw x;
cout<<"After throwing the exception"<<endl<<endl;
}
}
catch(int i){
cout<<"Exception has been caught"<<endl<<endl;
}
cout<<"ERROR OCCURED"<<endl<<endl;
}
82
PALAK BANSAL
01720803119
83