Professional Documents
Culture Documents
Oop Lab Manual 2023-24
Oop Lab Manual 2023-24
Oop Lab Manual 2023-24
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi , Pune – 411044.
Code: 204197
S.E. E&TC
1
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
INDEX
1
Write a program in C++ to sort the numbers in an array using
separate functions for read,
display, sort and swap. The objective of this assignment is to
learn the concepts of input,
output, functions, call by reference in C++.
4
Write a program in C++ to overload unary operators for
complex classes.
2
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
6
Write a program in C++ to implement string class. Write
constructors, destructor, Accepts function and Display function
9
Write a C++ program which use try and catch for exception
handling.
3
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Experiment No: 01
Program:
1. Hello World!
2. Call By Value and call By Reference
3. Scope Resolution
4. Constructor
Experiment No: 01
AIM: Write a program in C++ to sort the numbers in an array using separate functions for read,
display, sort and swap. The objective of this assignment is to learn the concepts of input, output,
functions, call by reference in C++.
THEORY:
Sorting Algorithms:
⮚ Sorting means arranging a set of data in some order.
⮚ There are different methods that are used to sort the data in ascending or descending order.
⮚ There are different simple sorting methods available which are,
✔ Selection sort,
✔ Bubble sort,
✔ Insertion Sort.
⮚ Also different complex sorting methods available which are,
⮚ Count Sort
⮚ Shell Sort
⮚ Heap Sort
⮚ Merge Sort
⮚ Quick Sort
1) Bubble Sort :
⮚ In this method, to arrange elements in ascending order, to begin with 0th element is compared with 1st element. If it
4
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Example:
2) Selection Sort :
⮚ This is the simplest method of sorting. In this method, to sort data in ascending order in first iteration the 0th element
is compared with all other elements.
⮚ If the 0th element is found to be greater than the compared element then they are exchanged. So, after first iteration
the smallest element is placed at 0th position.
⮚ The same procedure is repeated for the 1st element and so on…
⮚ If there are “n” elements then maximum (n-1) iterations needs to be performed.
Example with Explanation:
⮚ Suppose, an array consists of 5 numbers, the selection sort algorithm works as follows.
⮚ In first iteration 0th element 25 is compared with 1st element 17 and since 25 is greater than 17 they are exchanged.
⮚ Now the 0th element 17 is compared with 2nd element 31, but 17 is being less than 31, hence they are not
interchanged. ⮚ This process is repeated till 0th element is compared with rest of the elements. During the comparison
if 0th element is found to be greater than the compared element, then they are interchanged, otherwise not.
⮚ At the end of first iteration, the 0th element holds the smallest number.
⮚ Now, the second iteration starts with 1st element 25. The above process of comparison and swapping is
repeated. ⮚ So if there are “n” numbers then after (n-1) iterations array is sorted.
5
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
3) Insertion sort:
✔ Insertion sort is implemented by inserting a particular element at the appropriate position.
✔ In this method, the first iteration starts with comparison of 1st element with 0th element.
✔ In second iteration, 2nd element is compared with 0th and 1st element.
✔ In general, in every iteration an element is compared with all elements before it.
✔ During comparison if it is found that the element in question(list/array) can be inserted at suitable position then
space is created for it by shifting the other elements one position to the right and inserting the element at suitable
position. Example:
Explanation:
⮚ In the 1st iteration the 1st element 17 is compared with 0th element 25. Since 17 is smaller than 25, the 0th element
is shifted one position to the right and 17 is inserted at 0th position.
⮚ In the second iteration, the 2nd element 31 & 0th elements 17 are compared. Since 31 is greater than 17, nothing is
6
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
done. Then 2nd element is compared with the 1st element 25. Again no action is taken as 25 is less than 31. ⮚ In the
third iteration, the 3rd element 13 is compared with 0th element 17. Since, 13 is smaller than 17, 13 is inserted at 0th
position in the array and all the element from 0th till 2nd position are shifted to right by one position. ⮚ In the fourth
iteration the 4th element 2 is compared with 0th element 13, since 2 is smaller than 13, the 4th element is inserted at 0th
place in the array and all the elements from 0th till 3rd are shifted right by one position. ⮚ As a result, the array now
becomes a sorted array.
What Is a Reference?
A reference is an alias; when you create a reference, you initialize it with the name of another object, the target. From that
moment on, the reference acts as an alternative name for the target, and anything you do to the reference is really done to the
target.You create a reference by writing the type of the target object, followed by the reference operator (&), followed by the
name of the reference, followed by an equal sign, followed by the name of the target object. If you have an integer variable
named someInt, you can make a reference to that variable by writing the following: int &rSomeRef = someInt;
PROGRAM:
OUTPUT:
CONCLUSION:
Experiment No: 02
7
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
AIM: Write a C++ program that illustrates the concept of Function overloading
THEORY:
When we create two or more members of a class having the same name but different in number or type of parameters,
it is known as C++ overloading. In C++, we can overload: methods, constructors, and indexed properties
In this way of function overloading, we define two functions with the same names but a different number of parameters
of the same type. For example, in the below-mentioned program, we have made two add() functions to return the sum
of two and three integers.
// first function definition
Here add() function is said to be overloaded, as it has two definitions, one which accepts two arguments and another
which accepts three arguments. Which add() function will be called, depends on the number of arguments.
int main()
{
add(10,20) ; // add() with two parameter will be called
add(10,20,30) ; // add with three parameter will be called
}
#include <iostream>
using nanamespace std;mespace std;
int add(int a, int b)
{
cout << a+b <<endl;
return 0;
}
8
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
int main()
{
add(20, 40);
PROGRAM:
OUTPUT:
CONCLUSION:
9
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Experiment No: 03
AIM:
Write a program in C++ to perform following operations on complex numbers Add, Subtract,
Multiply, Divide, Complex conjugate. Design the class for complex number representation and the
operations to be performed. The objective of this assignment is to learn the concepts classes and
objects
THEORY:
Class: A class in C++ is the building block, that leads to Object-Oriented programming. It is a user-defined data type,
which holds its own data members and member functions, which can be accessed and used by creating an instance of that
class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will
share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the
class and wheels, speed limits, mileage are their properties.
• A Class is a user defined data-type which has data members and member functions.
• Data members are the data variables and member functions are the functions used to manipulate these variables
and together these data members and member functions defines the properties and behavior of the objects in a
Class.
10
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is
allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
Accessing data members and member functions: The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object. For example if the name of object is obj and you want to access the member function with
the name printName() then you will have to write obj.printName() .
Problem Description
We have to input the real and imaginary parts of two complex numbers separately and add them using classes and objects
in C++. Problem Solution
In this problem, we are going to create a function that adds two complex numbers. First the user has to provide real and
imaginary parts as input for both the complex numbers. After that the real part of first complex number will be added with
the real part of second one, similarly the imaginary part of first complex number will be added with the imaginary part of
second complex number. Program/Source Code
Here is source code of the C++ Program to add two Complex Numbers using Classes and Objects. The program is
successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown
below
. /* C++ Program to add two Complex Numbers */
#include<iostream>
using namespace std;
class Complex{
public:
int real;
int imag;
/* Function to set the values of
* real and imaginary part of each complex number
*/
void setvalue()
{
cin>>real;
11
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
cin>>imag;
}
/* Function to display the sum of two complex numbers */
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
/* Function to add two complex numbers */
1. Program Explanation
2. Here in this program we have created a few functions.
1. setvalue()
This function allows us to take input for real and imaginary parts of both the complex numbers. To take in real and
imaginary part as an input for first complex number we have called this function using an object c1. So, this function sets
the real and imag values for first complex number. Similarly to set the values of real and imaginary part of second complex
number, we can call it using a new object c2.
2. sum()
This function takes in two complex numbers as a parameter. Using this function we have added the real parts of both the
complex numbers together and stored them into variable real which is the real part of the resultant complex number.
Similarly we have added the imaginary parts of both the complex numbers and stored the result into variable imag which is
the imaginary part of the resultant complex number.
3. display()
We have called this function using an object for the resultant complex number, to display the real and imaginary parts of the
resultant complex number obtained after addition.
Input and Out Put:
1. Enter real and imaginary part of first complex number
56
Enter real and imaginary part of second complex number
13
Sum of two complex numbers is
6+9i
2. Enter real and imaginary part of first complex number
-4 6
Enter real and imaginary part of second complex number
12
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
5 -3
Sum of two complex numbers is
1+3i
3. Enter real and imaginary part of first complex number
-5 -6
Enter real and imaginary part of second complex number
-3 -5
Sum of two complex numbers is
-8+-11i
PROGRAM:
OUTPUT:
CONCLUSION:
Experiment No. :4
AIM: Write a program in C++ to implement Stack. Design the class for the stack and the operations to be
performed on the stack. Use Constructors and destructors.
THEORY:
Stacks: Concept, Array representation of stack
✓ A stack is a data structure in which addition of new element or deletion of an existing element always takes place at
the same end. This end is often known as top of stack.
✓ When an item is added to a stack, the operation is called as push.
✓ And when an item is removed from stack the operation is called as pop.
✓ Stack is called Last-In-First-Out (LIFO) list.
✓ Stack can be made by using either array (or array of structure) or linked list.
✓ If the elements are continuously added to the stack it grows at one end.
13
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Stack as ADT
✓A stack is generally implemented with two basic operations- push and pop.
✓ And there are various operations that may be implemented with stack. These are listed below.
Operation Description
push() Allows adding an element at the top of the stack.
Constructors:
➢ A constructor is a special member function whose task is to initialize the object of
its class. ➢ It is special because its name is same as that of class name.
➢ The constructor is invoked whenever an object of its associated class is created.
➢ It is called constructor because it constructs the values of data members of the class.
➢ The constructor function has some special characteristics. These are
➢ They should be declared in public section.
➢ They are invoked automatically when the objects are created.
➢ They do not have return types not even void and therefore they cannot return values.
14
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
➢ They cannot be inherited, though a derived class can called base class constructor.
➢ Like other c++ functions, they can have default arguments.
➢ Constructors cannot be virtual.
➢ We cannot refer to their addresses.
➢ They make implicit calls to the operator new and delete when memory allocation is required ➢
When constructor is declared for a class, initialization of the class object becomes mandatory. ➢
Constructor can be of different types: default constructor, parameterized constructor and copy
constructor. ➢ Example,
Class code
{
int id;
E & TC- SE-SEM-II-OOP (Object Oriented Programming-2020-21
public:
code(){}
code(int a)
{
id=a;
}
code(code &x)
{
Id=x.id;
}
};
1. Copy constructor is used to declare & initialize an object from another object. The process of initializing through a
copy constructor is known as copy initialization.
Destructors:
A destructor is used to destroy the objects that have been created by a constructor.
Like constructor destructor is a member function whose name is same as that of class name but proceeded by a tilde
(~). A destructor never takes any argument nor it does return any value.
It will be invoked implicitly by the compiler upon exit from a program ( or block or function) to clean up storage that is no
longer accessible.
PROGRAM:
OUTPUT:
CONCLUSION:
15
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Experiment No. :5
AIM:
Write a program in C++ to perform following operations on complex numbers Add, Subtract,
Multiply, Divide. Use operator overloading for these operations.
THEORY:
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called
function overloading and operator overloading respectively.An overloaded declaration is a declaration that is declared with
the same name as a previously declared declaration in the same scope, except that both declarations have different
arguments and obviously different definition (implementation).
When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by
comparing the argument types you have used to call the function or operator with the parameter types specified in the
definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
Operators Overloading in C++
You can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with
user defined types as well.Overloaded operators are functions with special names: the keyword "operator" followed by the
symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter
list. Box operator+(const Box&);
Declares the addition operator that can be used to add two Box objects and returns final Box object. Most overloaded
operators may be defined as ordinary non-member functions or as class member functions. In case we define above function
as non-
member function of a class then we would have to pass two arguments for each operand
as follows − Box operator+(const Box&, const Box&);
Following is the example to show the concept of operator over loading using a member function. Here an object is
passed as an argument whose properties will be accessed using this object, the object which will call this operator
can be accessed using this operator as explained below –
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
16
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Volume of Box1 : 210
17
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Overloadable/Non-overloadableOperators
Following is the list of operators which can be overloaded −
+ - * / % ^
& | ~ ! , =
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
The unary operators operate on a single operand and following are the examples of Unary operators −
PROGRAM:
OUTPUT:
CONCLUSION:
18
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Experiment No: 06
AIM:
THEORY:
Class: A class in C++ is the building block that leads to Object-Oriented programming. It is
a user-defined data type, which holds its own data members and member functions, which
can be accessed and used by creating an instance of that class. A C++ class is like a blueprint
for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and
brand but all of them will share some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are
their properties.
· A Class is a user defined data-type which has data members and member functions.
· Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions
defines the properties and behavior of the objects in a Class.
· In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can be apply brakes, increase speed etc.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when
it is instantiated (i.e. an object is created) memory is allocated.
Defining Class and Declaring ObjectsA class is defined in C++ using keyword class followed
by the name of class. The body of class is defined inside the curly brackets and terminated by
a
19
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Constructors are special class functions which performs initialization of every object. The
Compiler calls the Constructor whenever an object is created. Constructors initialize values
to object members after storage is allocated to the object.
Whereas, Destructor on the other hand is used to destroy the class object.
Accept function:
Now that we have the class, we can create objects from it, but before we create the object,
let’s first define our functions. We can define functions either within the class (inline member
functions) or outside the class (non-inline member functions).
For example, in the class person, the function void get_details(void) is inline as the function
20
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
The function prototype void display_details(void); can be defined outside the class using the
scope resolution operator::, that specifies the class to which the member being declared
belongs:
void person::display_details(void);
Display:
int main()
{
person p;
p.get_details();
p.display_details();
return 0;
}
PROGRAM:
OUTPUT:
21
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
Conclusion:
22
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
AIM:
Theory:
Simulation:
Result
23
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
24
Dr. D. Y. Patil Institute of Engineering, Management & Research, Akurdi, Pune - 44
Department of Electronics and Telecommunication Engineering
AIM:
Theory:
Simulation:
Result
25