Lab Manual Etcs204 Oops
Lab Manual Etcs204 Oops
1
The document is for internal circulation only.
All materials on these pages are copyrighted by the Shri Vishwakarma Skill University. All rights reserved.
Reproduction, modification, retransmission, in any form or by any means, electronic, mechanical or
otherwise, for reasons other than personal use, is strictly prohibited without prior written permission.
First Edition
January,2024
Published by:
Shri Vishwakarma Skill University
2
PREFACE
The Department of Computer Science Engineering, Shri Vishwakarma Skill University has
prepared this laboratory manual. It is designed as the faculty an instruction book for purposes
listed in order of importance as follows:
The manual is prepared with the idea that the revisions must be made periodically in order to
have the available text that represents the experiments. It has been tried to maintain the format
with diagrams, tables and illustrations.
It is believed that the information in the manual will enhance the practical skills of the students
along with developing the base of the subject.
Any suggestions and comments for further improvement of this manual will be gratefully
acknowledged.
Faculty
Shri Vishwakarma Skill University
3
Table of Contents
General Rules and Instructions:
Instruction for Students 5
General discipline in the Lab 5
Preparations and Performance 5
Lab Report 6
The lab report must contain the following: 6
List of Experiments
Experimental Setup Details
Experiment Details
Experiment No: 1
Experiment No: 2 13-15
Experiment No: 3 16-18
Experiment No: 4 19-24
Experiment No: 5 25-31
Experiment No: 6 32-34
Experiment No: 7 35-36
Experiment No: 8 37-41
Experiment No: 9 42-45
Experiment No: 10 46-49
4
General Rules and Instructions:
Instruction for Students
To complete all the experiments within time, to understand them completely and effectively, each student
must obey the following points:
Discipline is always given the highest precedence for maintaining the quality standard of your lab. Any
misconduct will be seriously dealt with and immediately responded without prior notification.
To ensure a pleasant, productive and comfortable experience for all of our users, you are directed to adhere
to the following guidelines:
Students will not be allowed after ten minutes from the scheduled time.
Students will not leave the lab till the period is over.
Attendance in the lab class is compulsory.
Students should maintain silence while performing the experiments.
Students should not attend a different lab group/section other than the one assigned at the
beginning of the session.
Please help us to keep the facility (Equipment’s- etc.) clean by discarding trash in the receptacles.
Please respect the sensitivities of your peers and refrain from viewing any inappropriate content.
Any attempt on Hacking information will be reciprocated with serious consequences.
Please abide by the rules and make appropriate use of all facilities. Users found to be damaging computer
configurations, accessing content or systems illegally or attempting to compromise security may be
deprived of the facility or will be fined.
5
Lab Report
Each student is required to write a complete report of the experiments, he has performed and
bring to lab class for evaluation in the next working lab.
Report should be written very clearly and lab record should be maintained neatly.
6
ETCS254: OBJECT ORIENTED PROGRAMMING USING C++ LAB
EVEN 2023-24
LIST OF EXPERIMENTS
Exp. Aim of the Experiment
No
3. Write a program to find the greatest of two given numbers in two different classes using
friend function
4. Implement a class string containing the following functions: - Overload + operator to carry
out the concatenation of strings. - Overload = operator to carry out string copy. - Overload
<= operator to carry out the comparison of strings. - Function to display the length of a
string. - Function tolower( ) to convert upper case letters to lower case. - 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
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 numbers
with different data types.
7. Write a program to demonstrate the use of special functions, constructor and destructor in
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 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
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
index is beyond the array size.
7
Experimental Setup Details
Software Requirements:
TurboC2.0/ Turbo C++3.0+/
Visual Studio code / any Online C++ compiler
Hardware Requirements:
No specific requirements. Any computer Hardware capable of running DOS can be used for this
course
Minimum Recommended: - i3 processor with 4 GB RAM, 80 GB HDD, Windows 8/10, Browser
8
Experiment Details
9
Experiment No: 1
Targeted CO:
CO1- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include<iostream.h>
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]<<"";
}
cout<<"\n";
}
}
10
void enter_matrix()
{
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
cin>>a[i][j];
}
}
}
11
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;
}
Sample Output:
Suggestive Readings:
● www.geeksforgeeks.org/object-oriented-programming-in-cpp/
● https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
12
Experiment No: 2
Title: 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. .
Objective: To study the basic programming using c++ for the complex no
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO1- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
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;
13
}
real = c1.real+c2.real;
imag = c1.imag+c2.imag;
void display()
cout<<real<<"+"<<imag<<"i";
};
#include<iostream.h>
int main()
int real,imag;
cout<<"\n Enter a single value for real and imaginary parts of first complex number : ";
cin>>real;
complexno c1(real);
c1.display();
cout<<"\n\n Enter different values for real and imaginary parts of second complex
number : ";
cin>>real>>imag;
complexno c2(real,imag);
c2.display();
complexno c3;
14
c3.display();
cout<<"\n\n Storing the result of addition of first and second complex number into
third...";
c3.add(c1,c2);
c3.display();
cout<<"\n";
return 0;
Sample Output:
15
Experiment No: 3
Title: Write a program to find the greater of two given numbers in two different classes
using friend function.
Objective: To study the use of friend function
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO3- Solve basic problems using concepts of multiple class use, function in object-
oriented programming.
Code/Algorithm/Theory:
Source Code:
#include<iostream.h>
class a;
class b
int number;
public:
b(int x)
number=x;
};
class a
int number;
public:
a(int x)
number=x;
16
}
};
if(a1.number>b1.number)
else if(a1.number<b1.number)
else
int main()
cout<<"\n\n Program to find greatest of two numbers in two different classes using
friend function";
cout<<"\n “______________________________________________________________________";
int num;
cin>>num;
a a1(num);
cin>>num;
17
b b1(num);
greatest(a1,b1);
cout<<"\n";
return 0;
Sample Output:
Experiment No: 4
18
Title: Implement a class string containing the following functions: - Overload + operator to
carry out the concatenation of strings. - Overload = operator to carry out string copy. -
Overload <= operator to carry out the comparison of strings. - Function to display the length of
a string. - Function tolower( ) to convert upper case letters to lower case. - Function toupper( )
to convert lower case letters to upper case
Objective: To study string operation in C++
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO3- Solve basic problems using concepts of multiple class use, function in object-
oriented programming.
Code/Algorithm/Theory:
Source Code:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
class String
char *s;
int size;
public:
String(char *c)
size = strlen(c);
s = new char[size];
strcpy(s,c);
19
char* operator +(char *s1)
size = strlen(s)+strlen(s1);
strcpy(s2,s);
s = new char[size];
strcpy(s,s2);
strcat(s,s1);
return s;
size = strlen(s1);
s = new char[size];
strcpy(s,s1);
return s;
return strcmp(s,s1);
void display()
void displaylength()
20
{
void Tolower()
for(int i=0;i<size;i++)
if(isupper(s[i]))
s[i] = (char)tolower(s[i]);
display();
void Toupper()
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;
21
cout<<"\n\n Program to perform operations on string";
cout<<"\n _____________________________________________________";
cin>>l1;
fflush(stdin);
s1 = new char[l1];
gets(s1);
String s(s1);
while(1)
char *d;
int length;
system("cls");
cin>>choice;
if(choice==1)
s=s1;
cin>>length;
d = new char[length];
fflush(stdin);
gets(d);
s = s+d;
s.display();
22
else if(choice==2)
cin>>length;
d = new char[length];
fflush(stdin);
gets(d);
s=d;
s.display();
else if(choice==3)
cin>>length;
d = new char[length];
fflush(stdin);
gets(d);
if(!(s<=d))
else
else if(choice==4)
23
{
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
getch();
return 0;
Sample Output:
24
Experiment No: 5
Title: 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. .
Objective: To study the stack, queue and retrieval
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO4- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL,*tail=NULL;
class List
{
public:
void view()
{
node *n = head;
if(head==NULL)
{
cout<<"\n No elements found...";
}
else
{
cout<<" ";
while(n!=NULL)
{
if(n->next==NULL)
{
cout<<n->data;
}
25
else
{
cout<<n->data<<"->";
}
n = n->next;
}
}
}
virtual void store(int n)=0;
virtual int retrive()=0;
};
26
{
tail=NULL;
head=NULL;
}
return n;
}
}
};
int main()
{
27
Stack s1;
int ch;
while(1)
{
system("cls");
cout<<"\n\n Program to implement stack and queue using pure virtual functions
store and retrieve";
cout<<"\n ________________________________________________________________________";
cout<<"\n\n Menu";
cout<<"\n ^^^^";
cout<<"\n\n 1. Stack";
cout<<"\n 2. Queue";
cout<<"\n 3. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch;
if(ch==1)
{
Stack s1;
int ch1;
while(1)
{
system("cls");
cout<<"\n\n Stack Menu";
cout<<"\n ^^^^^ ^^^^";
cout<<"\n 1. Push Element";
cout<<"\n 2. Pop Element";
cout<<"\n 3. View Stack";
cout<<"\n 4. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\n Enter the element you want to push - ";
cin>>element;
s1.store(element);
cout<<"\n Element Pushed";
}
else if(ch1==2)
{
int element=0;
element = s1.retrive();
if(element==-1)
{
cout<<"\n Stack is Empty";
}
else
{
cout<<"\n Element Popped = "<<element;
}
}
else if(ch1==3)
28
{
cout<<"\n Elements in stack from bottom to top:- ";
s1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\n\n Wrong choice";
}
getch();
}
}
else if(ch==2)
{
Queue q1;
int ch1;
while(1)
{
system("cls");
cout<<"\n\n Queue Menu";
cout<<"\n -------------------";
cout<<"\n 1. Push Element";
cout<<"\n 2. Pop Element";
cout<<"\n 3. View Queue";
cout<<"\n 4. Exit";
cout<<"\n\n Enter your choice - ";
cin>>ch1;
if(ch1==1)
{
int element;
cout<<"\n Enter the element you want to push - ";
cin>>element;
q1.store(element);
cout<<"\n Element Pushed";
}
else if(ch1==2)
{
int element=0;
element = q1.retrive();
if(element==-1)
{
cout<<"\n Queue is Empty";
}
else
{
cout<<"\n Element Popped = "<<element;
}
}
else if(ch1==3)
29
{
cout<<"\n Elements in queue from front to rear:- ";
q1.view();
}
else if(ch1==4)
{
break;
}
else
{
cout<<"\n\n Wrong choice";
}
getch();
}
}
else if(ch==3)
{
exit(0);
}
else
{
cout<<"\n\n Wrong Choice";
}
getch();
}
return 0;
}
Sample Output:
30
31
Experiment No: 6
Title: Write a program to define the function template for calculating the square of given
numbers with different data types.
Objective: To study function template in c++
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO4- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include <iostream>
inline T square(T x)
T result;
result = x * x;
return result;
};
int main()
{ int i, ii;
float x, xx;
double y, yy;
i = 2;
x = 2.2;
y = 2.2;
ii = square<int>(i);
32
xx = square<float>(x);
yy = square<double>(y);
yy = square(y);
Another Code 2
#include<iostream>
T square(T num)
{
return num * num;
}
int main()
{
int int_num;
float float_num;
OUTPUT
33
34
Experiment No: 7
Title: Write a program to demonstrate the use of special functions, constructor and
destructor in the class template. The program is used to find the bigger of two entered
numbers..
Objective: To study constructor in C++
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include <iostream>
int main()
35
cout << "\nEnter two floating-point numbers:\n";
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
Output:
36
Experiment No: 8
Title: Write a program to perform the deletion of white spaces such as horizontal tab,
vertical 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.
Objective: To study files
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
There are six types of whitespace characters by default:
1. Space (' ') - Space is the most common whitespace character. It is usually used to
separate two words.
2. Line feed ('\n') - The line feed character moves the cursor to the next line. It
signifies the end of a line of text.
3. Horizontal tab ('\t') - A horizontal tab moves the cursor to the next tab stop. It is
usually used for indentation purposes.
4. Vertical tab ('\v') - A vertical tab moves the cursor to the next vertical tab stop. It is
an outdated whitespace character and is rarely used anymore.
5. Carriage return ('\r') - The carriage return character is used to reset the cursor's
position to the start of a line of text.
6. Form feed ('\f') - A form feed is a page break character. It is used to end the
Source Code:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
int main(){
std::cout << "String s before removing whitespaces: " << s << std::endl << std::endl;
37
// Using the erase, remove_if, and ::isspace functions.
s.end());
return 0;
#include <iostream>
#include <algorithm>
#include <locale>
#include <functional>
#include <string>
int main() {
// Creating a string.
std::endl;
std::placeholders::_1,
std::locale::classic())),
s.end());
38
return 0;
}
OUTPUT :
String s before removing whitespaces:
Whitespace String
String s after removing whitespaces: WhitespaceString
#include <iostream>
#include <string>
#include <algorithm>
return true;
} else {
return false;
int main() {
39
return 0;
#include <iostream>
#include <string>
#include <algorithm>
int main() {
s.erase(std::remove_if(s.begin(), s.end(),
}),
s.end());
return 0;
Output:-
40
we can use the std::regex_replace function to remove whitespace from a
string in C++. This function replaces all matching characters in a string
with the character specified by the user
#include<iostream>
#include <string>
#include <regex>
int main() {
std::string s = "Using \n\t Regex To \f Remove Whitespaces";
std::cout << "String s before removing whitespaces --> " << s <<
std::endl << std::endl;
/* Write a program to perform the deletion of white spaces such as horizontal tab,
vertical 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.
*/ #include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;
if (!isspace(c)) {
outputFile << c;
41
inputFile.close();
outputFile.close();
cout << "White spaces removed and content saved to output.txt" << endl;
else {
} return 0;
Output :
42
Experiment No: 9
Title: 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
Objective: To study how to write class data in files input / read and write operations
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include <iostream>
#include <fstream>
using namespace std;
// Class to define the properties
class Student {
public:
string Name;
int age;
43
string sex;
string height;
int weight;
};
int main(){
Student Stud_1;
Stud_1.Name="RANJEET";
Stud_1.age=43;
Stud_1.sex="Male";
Stud_1.height="5.5inch";
Stud_1.weight=65;
Another Example //C++ program to write and read object using read and write function.
#include <iostream>
#include <fstream>
44
//class student to read and write student details
class student
{
private:
char name[30];
int age;
public:
void getData(void)
{ cout<<"Enter name:"; cin.getline(name,30);
cout<<"Enter age:"; cin>>age;
}
void showData(void)
{
cout<<"Name:"<<name<<",Age:"<<age<<endl;
}
};
int main()
{
student s;
ofstream file;
45
//display data on monitor
s.showData();
//close the file
file1.close();
return 0;
}
46
Experiment No: 10
Title: Write a program to raise an exception if any attempt is made to refer to an element
whose index is beyond the array size.
Objective: To study exception handling in c++
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include <iostream>
#include <array>
#include <exception>
int main()
{
array <int, 6> arr = { 1,2,3,4,5,6 };
PrintArray<int, 6>(arr);
cout <<"if out of range given arr[7] then printing garbage not in range" ;
cout<<arr[7] ;
47
system("Pause");
return 0;
Output:
Another example
#include <iostream>
using namespace std;
int main() {
try {
48
cin >> numerator;
return 0;
49
}
50