Angad OOPS
Angad OOPS
OOPS Lab
PracticaL FiLe
cODe : : cic-257
3 Write a program to find the greatest of two given numbers in two different classes using 6-7
friend function.
5 Create a class called LIST with two pure virtual function store() and retrieve(). To store a 11-13
value call store and to retrieve call retrieve function. Derive two classes stack and queue
from it and override store and retrieve.
6 Write a program to define the function template for calculating the square of given 14
numbers with different data types.
7 Write a program to demonstrate the use of special functions, constructor and destructor in 17-18
the class template. The program is used to find the bigger of two entered numbers.
8 Write a program to perform the deletion of white spaces such as horizontal tab, vertical 19-20
tab, space, line feed new line and carriage return from a text file and store the contents of
the file without the white spaces on another file.
9 Write a program to read the class object of student info such as name, age, sex, height and 21-23
weight from the
keyboard and to store them on a specified file using read() and write() functions. Again the
same file is opened for reading and displaying the contents of the file on the screen.
10 Write a program to raise an exception if any attempt is made to refer to an element whose 24-25
index is beyond the array size.
Experiment 1
class matrix
{
int **a,l1,l2;
public:
void initialize_matrix(int m,int n)
{
l1=m;
l2=n;
a = new int*[m];
for(int p=0;p<m;p++)
{
a[p] = new int[n];
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
}
void display_matrix()
{
for(int i=0;i<l1;i++)
{
cout<<" ";
for(int j=0;j<l2;j++)
{
cout<<a[i][j]<<setw(5);
}
cout<<"\n";
}
}
void enter_matrix()
{
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
cin>>a[i][j];
}
}
}
int multiply_matrix(matrix m1, matrix m2)
{
if(m1.l2!=m2.l1)
{
cout<<"\n\n Matrix multiplication not possible\n";
return 0;
}
else
{
for(int i=0;i<m1.l1;i++)
{
for(int j=0;j<m2.l2;j++)
{
for(int k=0;k<m1.l2;k++)
{
a[i][j]+=m1.a[i][k]*m2.a[k][j];
}
}
}
return 1;
}
}
};
int main()
{
cout<<"\n\n Program for multiplication of two matrices using oop";
cout<<"\n ^^^^^^^ ^^^ ^^^^^^^^^^^^^^ ^^ ^^^ ^^^^^^^^ ^^^^^ ^^^";
matrix m1,m2,m3;
int row1,col1,row2,col2;
if(m3.multiply_matrix(m1,m2))
{
cout<<"\n\n Resultant matrix - \n\n";
m3.display_matrix();
}
return 0;
Output
Experiment 2
#include<iostream>
using namespace std;
class complexno
{
int real,imag;
public:
complexno()
{
real=0;
imag=0;
}
complexno(int i)
{
real=i;
imag=i;
}
complexno(int a,int b)
{
real=a;
imag=b;
}
void add(complexno c1, complexno c2)
{
real = c1.real+c2.real;
imag = c1.imag+c2.imag;
}
void display()
{
cout<<real<<"+"<<imag<<"i";
}
};
int main()
{
cout<<"\n\n Program to perform addition of two complex numbers using
constructor overloading";
cout<<"\n ^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^ ^^ ^^^ ^^^^^^^ ^^^^^^^ ^^^^^
^^^^^^^^^^^ ^^^^^^^^^^^";
int real,imag;
cout<<"\n Enter a single value for real and imaginary parts of first
complex number : ";
cin>>real;
complexno c1(real);
cout<<"\n\n Enter different values for real and imaginary parts of second
complex number : ";
cin>>real>>imag;
complexno c2(real,imag);
complexno c3;
cout<<"\n\n Initially third complex number is - ";
c3.display();
cout<<"\n";
return 0;
}
Output
Experiment 3
Write a program to find the greatest of two given numbers in two different
classes using friend function.
#include<iostream>
using namespace std;
class a;
class b
{
int number;
public:
b(int x)
{
number=x;
}
void friend greatest(a a1,b b1);
};
class a
{
int number;
public:
a(int x)
{
number=x;
}
void friend greatest(a a1,b b1);
};
int main()
{
cout<<"\n\n Program to find greatest of two numbers in two different
classes using friend function";
int num;
greatest(a1,b1);
cout<<"\n";
return 0;
}
Output
Experiment 4
Implement a class string containing the following functions:
a. Overload + operator to carry out the concatenation of strings.
b. Overload = operator to carry out string copy.
C Overload <= operator to carry out the comparison of strings.
d. Function to display the length of a string.
e. Function tolower() to convert upper case letters to lower case.
f. Function toupper() to convert lower case letters to upper case
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display()
{
cout<<"\n\n String stored in class = "<<s;
}
void displaylength()
{
cout<<"\n\n Length of string stored in class = "<<size;
}
void Tolower()
{
cout<<"\n\n String converted to lowercase";
for(int i=0;i<size;i++)
{
if(isupper(s[i]))
s[i] = (char)tolower(s[i]);
}
display();
}
void Toupper()
{
cout<<"\n\n String converted to uppercase";
for(int i=0;i<size;i++)
{
if(islower(s[i]))
s[i] = (char)toupper(s[i]);
}
display();
}
};
int main()
{
char *s1;
int choice,l1;
cout<<"\n\n Program to perform operations on string";
Output
Experiment 5
Create a class called LIST with two pure virtual function store() and
retrieve(). To store a value call store and to retrieve call retrieve function.
Derive two classes stack and queue from it and override store and retrieve.
#include <iostream>
#include <stack>
#include <queue>
class LIST {
public:
};
private:
std::stack<int> s;
public:
s.push(value);
int retrieve() {
if (s.empty()) {
return -1;
return value;
};
private:
std::queue<int> q;
public:
q.push(value);
int retrieve() {
if (q.empty()) {
return -1;
q.pop();
return value;
};
int main() {
Stack myStack;
Queue myQueue;
myStack.store(10);
myStack.store(20);
myQueue.store(40);
return 0;
Output
Experiment 6
Write a program to define the function template for calculating the square
of given numbers with different data types
#include<iostream>
T square(T num)
int main()
int int_num;
float float_num;
}
Output
Experiment 7
class Box {
private:
T data;
public:
cout << "Constructor called, data initialized to " << data << endl;
data = other.data;
cout << "Copy Constructor called, data copied with value " << data << endl;
other.data = T();
cout << "Move Constructor called, data moved with value " << data << endl;
~Box() {
cout << "Destructor called, data with value " << data << " is being destroyed" << endl;
}
T getData() const {
return data;
data = value;
};
int main() {
Box<int> box1(10);
box2.setData(20);
return 0;
}
Output
Experiment 8
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
if (!inputFile.is_open()) {
cout << "Error opening input file." << endl;
return 1;
} if (!outputFile.is_open()) {
cerr << "Error opening output file." << endl;
return 1;
char ch;
while (inputFile.get(ch)) {
If (!isspace(ch)) {
outputFile.put(ch);
} inputFile.close();
outputFile.close();
return 0;
}
Output
Experiment 9
Write a program to read the class object of student info such as name, age, sex, height and
weight from the keyboard and to store them on a specified file using read() and write()
functions. Again the same file is opened for reading and displaying the contents of the file on
the screen
#include <iostream>
#include <fstream>
#include <string>
class Student {
private:
string name;
int age;
string sex;
double height;
double weight;
public:
void read() {
getline(cin, name);
cin.ignore();
getline(cin, sex);
cin.ignore();
getline(file, name);
file.ignore();
getline(file, sex);
file.ignore();
cout << "Name: << name << "\nAge: << age << "\nSex: << sex
<< "\nHeight: <<< height << "\nWeight: " << weight << endl;
};
int main() {
Student student;
ofstream outFile("student_info.txt");
if (outFile.is_open()) {
student.write(outFile);
outFile.close();
} else {
return 1;
Student studentFromFile:
ifstream inFile("student_info.txt");
if (inFile.is_open()) {
studentFromFile.readFromFile(inFile);
inFile.close();
} else {
return 1;
return 0;
}
Output
Experiment 10
#include <iostream>
#include <stdexcept>
int main() {
int size;
int index;
}else{
cout << "Element at index " << index << " is " << arr[index] << endl;
delete[] arr;
return 0;
}
Output