0% found this document useful (0 votes)
17 views29 pages

Angad OOPS

The document is a practical file for an OOP lab at Greater Noida Institute of Technology, detailing various programming experiments. It includes problem statements and code implementations for tasks such as matrix multiplication, complex number addition using constructor overloading, and string manipulation through operator overloading. Each experiment is accompanied by code examples demonstrating the application of object-oriented programming principles.

Uploaded by

Blakk Boy
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)
17 views29 pages

Angad OOPS

The document is a practical file for an OOP lab at Greater Noida Institute of Technology, detailing various programming experiments. It includes problem statements and code implementations for tasks such as matrix multiplication, complex number addition using constructor overloading, and string manipulation through operator overloading. Each experiment is accompanied by code examples demonstrating the application of object-oriented programming principles.

Uploaded by

Blakk Boy
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/ 29

Greater Noida Institute of Technology

Greater Noida (College Code-272)


Affiliated to GGSIP University, New Delhi

OOPS Lab
PracticaL FiLe
cODe : : cic-257

Submitted By: Submitted To:


Name: Angad Singh Heer Mrs.Bhanu
Branch: CSE
Enrollment No: 00927202723
Year: 2nd Year (3rd Semester)
INDEX
S. No. PROBLEM STATEMENT PAGE No.

1 Write a program for multiplication of two matrices using OOP. 1-3

2 Write a program to 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 imag parts
to equal values and third which takes two argument is used to initialized real and imag to 4-5
two different values.

3 Write a program to find the greatest of two given numbers in two different classes using 6-7
friend function.

4 Implement a class string containing the following functions: 8-10

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.

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

Write a program for multiplication of two matrices using OOP.


#include <iostream>
#include <iomanip>
using namespace std;

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;

cout<<"\n\n Enter the rows and columns of first matrix - ";


cin>>row1>>col1;
m1.initialize_matrix(row1,col1);

cout<<"\n Enter the rows and columns of second matrix - ";


cin>>row2>>col2;
m2.initialize_matrix(row2,col2);

cout<<"\n Enter the elements of first matrix - ";


m1.enter_matrix();

cout<<"\n Enter the elements of second matrix - ";


m2.enter_matrix();

cout<<"\n First Matrix - \n\n";


m1.display_matrix();

cout<<"\n Second Matrix - \n\n";


m2.display_matrix();

cout<<"\n\n Multiplying both matrices...";


m3.initialize_matrix(row1,col2);

if(m3.multiply_matrix(m1,m2))
{
cout<<"\n\n Resultant matrix - \n\n";
m3.display_matrix();
}

return 0;
Output
Experiment 2

Write a program to 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 imag parts to equal values and third
which takes two argument is used to initialized real and imag to two
different values.

#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 First complex number is given by - ";


c1.display();

cout<<"\n\n Enter different values for real and imaginary parts of second
complex number : ";
cin>>real>>imag;
complexno c2(real,imag);

cout<<"\n Second complex number is given by - ";


c2.display();

complexno c3;
cout<<"\n\n Initially third complex number is - ";
c3.display();

cout<<"\n\n Storing the result of addition of first and second complex


number into third...";
c3.add(c1,c2);

cout<<"\n\n Now third complex number is given by - ";


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

void greatest(a a1,b b1)


{
if(a1.number>b1.number)
{
cout<<"\n Number in class A is greatest i.e. "<<a1.number;
}
else if(a1.number<b1.number)
{
cout<<"\n Number in class B is greatest i.e. "<<b1.number;
}
else
{
cout<<"\n Number in both classes are equal";
}
}

int main()
{
cout<<"\n\n Program to find greatest of two numbers in two different
classes using friend function";
int num;

cout<<"\n\n Enter number for class A - ";


cin>>num;
a a1(num);

cout<<"\n Enter number for class B - ";


cin>>num;
b b1(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>

using namespace std;


class String
{
char *s;
int size;
public:
String(char *c)
{
size = strlen(c);
s = new char[size];
strcpy(s,c);
}
char* operator +(char *s1)
{
size = strlen(s)+strlen(s1);
char *s2 = new char[strlen(s)];
strcpy(s2,s);
s = new char[size];
strcpy(s,s2);
strcat(s,s1);
return s;
}

char* operator =(char *s1)


{
size = strlen(s1);
s = new char[size];
strcpy(s,s1);
return s;
}

bool operator <=(char *s1)


{
return strcmp(s,s1);
}

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

cout<<"\n\n Enter length of string - ";


cin>>l1;
fflush(stdin);
s1 = new char[l1];
cout<<"\n Enter string to be stored in class - ";
gets(s1);
String s(s1);
while(1)
{
char *d;
int length;
system("cls");
cout<<"\n\n Menu\n ^^^^ \n 1. String concatenation \n
2. String copy \n
3. String comparison \n
4. Display String \n
5. Display length of string \n
6. Convert string to lowercase \n
7. Convert string to uppercase \n
8. Exit ";
cin>>choice;
if(choice==1)
{
s=s1;
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s = s+d;
cout<<"\n After concatenation....";
s.display();
}
else if(choice==2)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
s=d;
cout<<"\n After copying....";
s.display();
}
else if(choice==3)
{
cout<<"\n\n Enter the length of new string - ";
cin>>length;
d = new char[length];
fflush(stdin);
cout<<"\n Enter the string - ";
gets(d);
if(!(s<=d))
{
cout<<"\n Strings are equal";
}
else
{
cout<<"\n Strings are not equal";
}
}
else if(choice==4)
{
s.display();
}
else if(choice==5)
{
s.displaylength();
}
else if(choice==6)
{
s.Tolower();
}
else if(choice==7)
{
s.Toupper();
}
else if(choice==8)
{
exit(0);
}
else
{
cout<<"\n\n Wrong choice";
}
getch();
}
return 0;
}

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>

using namespace std;

class LIST {

public:

virtual void store(int value) = 0;

virtual int retrieve() = 0;

};

class Stack : public LIST {

private:

std::stack<int> s;

public:

void store(int value) {

s.push(value);

int retrieve() {

if (s.empty()) {

cout << "Stack is empty." << endl;

return -1;

int value = s.top();


s.pop();

return value;

};

class Queue : public LIST {

private:

std::queue<int> q;

public:

void store(int value) {

q.push(value);

int retrieve() {

if (q.empty()) {

cout << "Queue is empty." << endl;

return -1;

int value = q.front();

q.pop();

return value;

};

int main() {

Stack myStack;

Queue myQueue;

myStack.store(10);

myStack.store(20);

cout << "Stack retrieved: " << myStack.retrieve() << endl;


myQueue.store(30);

myQueue.store(40);

cout << "Queue retrieved: " << myQueue.retrieve() << endl;

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>

using namespace std;

template <class T>

T square(T num)

return num * num;

int main()

int int_num;

float float_num;

cout << "Enter a integer number:\t";

cin >> int_num;

cout << "Squared integer number:\t" << square(int_num) << endl;

cout << "Enter a floating-point number:\t";

cin >> float_num;

cout << "Squared floating-point number:\t" << square(float_num) << endl;

}
Output
Experiment 7

Write a program to demonstrate the use of special functions, constructor


and destructor in the class template
#include <iostream>

using namespace std;

template <typename T>

class Box {

private:

T data;

public:

Box(T value) : data(value) {

cout << "Constructor called, data initialized to " << data << endl;

Box(const Box& other) {

data = other.data;

cout << "Copy Constructor called, data copied with value " << data << endl;

Box(Box&& other) noexcept : data(other.data) {

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;

void setData(T value) {

data = value;

};

int main() {

cout << "Creating box1..." << endl;

Box<int> box1(10);

cout << "Creating box2 as a copy of box1..." << endl;

Box<int> box2 = box1;

cout << "Creating box3 by moving box1..." << endl;

Box<int> box3 = move(box1);

cout << "Setting new data for box2..." << endl;

box2.setData(20);

cout << "box2 data: " << box2.getData() << endl;

cout << "box3 data: " << box3.getData() << endl;

return 0;

}
Output
Experiment 8

Write a program to perform the deletion of white spaces such as horizontal


tab, vertical tab, space, line feed newline and carriage return from a text file
and store the contents of the file without the white spaces on another file

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

cout << "Whitespace removed successfully!" << endl;

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>

using namespace std;

class Student {

private:

string name;

int age;

string sex;

double height;

double weight;

public:

void read() {

cout << "Enter name: ";

getline(cin, name);

cout << "Enter age: ";

cin >> age;

cin.ignore();

cout << "Enter sex: ";

getline(cin, sex);

cout << "Enter height: ";

cin >> height;

cout << "Enter weight: ";


cin >> weight;

cin.ignore();

void write(ofstream &file) const {

file << name << endl;

file <<< age << endl;

file << sex << endl;

file << height << endl;

file <<< weight << endl;

void readFromFile(ifstream &file) {

getline(file, name);

file >> age;

file.ignore();

getline(file, sex);

file >>> height:

file >> weight;

file.ignore();

void display() const {

cout << "Name: << name << "\nAge: << age << "\nSex: << sex

<< "\nHeight: <<< height << "\nWeight: " << weight << endl;

};

int main() {

Student student;

cout << "Enter student information:\n";


student.read();

ofstream outFile("student_info.txt");

if (outFile.is_open()) {

student.write(outFile);

outFile.close();

cout << "Student information written to file.\n";

} else {

cout << "Error opening file for writing.\n";

return 1;

Student studentFromFile:

ifstream inFile("student_info.txt");

if (inFile.is_open()) {

studentFromFile.readFromFile(inFile);

inFile.close();

cout << "\nStudent information read from file:\n"; studentFromFile.display();

} else {

cout << "Error opening file for reading.\n";

return 1;

return 0;

}
Output
Experiment 10

Write a program to raise an exception if any attempt is made to refer to an


element whose index is beyond the array size.

#include <iostream>

#include <stdexcept>

using namespace std;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr = new int[size];

cout << "Enter" << size << " elements:\n";

for (int i = 0; i < size; ++i) {

cin >> arr[i];

int index;

cout << "Enter the index you want to access:

cin >> index;

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

cout<<"Exception: Index out of bounds.";

}else{

cout << "Element at index " << index << " is " << arr[index] << endl;

delete[] arr;

return 0;

}
Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy