You are on page 1of 16

EXPERIMENT NUMBER – 2.

2
STUDENT’S NAME – Akankshya Jena
STUDENT’S UID –20BCS9510
CLASS AND GROUP –13B
SEMESTER –2

Q 1. WAP to calculate and display cube of an integer and float


variable using function overloading.

ALGORITHM

1. Including the bits/stdc++.h header file in our code to use its functions.
2. Include the std namespace in our program to use its classes without
calling it.
3. Then define the function int cube which will be used to find the cube of the
integer value and return it.
4. Then define the function float cube which will be used to find the cube of
the float value and return it.
5. Now create main function and then take integer and float value as an input
from the user.
6. Now call the functions and print the cubes of both int and float value as
outputs.
CODE:

#include <bits/stdc++.h>
using namespace std;
int cube(int x)
{
return x*x*x;
}
float cube(float y)
{
return y*y*y;
}
int main()
{
int a;
float b;
cout<<"ENTER THE VALUE FOR INTEGER NUMBER"<<endl;
cin>>a;
cout<<"ENTER THE VALUE FOR FLOAT NUMBER"<<endl;
cin>>b;
cout<< "Cube of integer number " << a << " is " << cube(a) <<endl;
cout<< "Cube of float number " << b << " is " << cube(b) <<endl;
return 0;
}

OUTPUT:
Q.2 Program to demonstrate the unary operator overloading for
operator ++. Make a class test. Create a default constructor to
initialize the variable. 1) Overload operator ++ (Pre) with definition to
pre-decrement the value of a variable 2) Overload operator ++ (post)
with definition to post-decrement the value of variable.

ALGORITHM:

 Including the bits/stdc++.h header file in our code to use its functions.
 Include the std namespace in our program to use its classes without
calling it.
 Create a class one.
 Create a variable x.
 Create a constructor and initalize x to 50.
 Create a function void operator ++(int)with definition of pre.

 Create a function void operator ++()with definition of post.


 Now create a display function to display the value of x.
 Now create main function and create objects one for post and one for pre.
 Now perform post and pre increment and then display them using the
display function.
CODE:

#include<bits/stdc++.h>

using namespace std;

class one

int x;

public:

one()

x=50;

void operator ++(int)

x=x+1;

void operator ++()

x=x+1;

int display()
{

return(x);

};

int main()

one a, b;

cout<<"Before A: "<<a.display();

cout<<"\nBefore B: "<<b.display();

a++,++b;

cout<<"\n\n(Pre) After A: "<<a.display();

cout<<"\n(Post) After B: "<<b.display();

return 0;

OUTPUT
Q.3 WAP for creating a matrix class which can handle integer
matrices of different dimensions. Overload the operator (+) for
addition and (==) comparison of matrices.

ALGORITHM:

 Including the bits/stdc++.h header file in our code to use its functions.
 Include the std namespace in our program to use its classes without
calling it.
 Define MAXROWS and MAXCOLS to 50.
 Create a constructor with parameters as
(int r, int c, int mat[MAXROWS][MAXCOLS]).Its an Overloaded constructor
to initialize the Matrix with dimensions

 Now create an function named “void display” to display the matrix.


 Now create an function named “Matrix operator+(Matrix x)”. This function
for + operator overloading.
 Create int mat[MAXROWS][MAXCOLS] to store the sum of matrices.
 Now we will traverse the matrix and add the corresponding elements of
matrix and store there sum in “mat”.
 Now create a Function for == operator overloading. This function is
created to check wether both the matrix provided by the user are equal or
not.
 Create main function.
 Now take input from the user the number of rows and columns for both
matrix one and two
 Now ask the user to input the elements for both the matrix.
 Now display both the Matrix.
 In mat4 store the sum and then display that sum by calling the “display”
function created above.
 Now check whether if one of the matrix is equal to the other and display it.
CODE:

#include <iostream>

#define MAXROWS 50

#define MAXCOLS 50

using namespace std;

class Matrix {

public:

int arr[MAXROWS][MAXCOLS];

int rows, cols;

Matrix()

rows = cols = 2;

Matrix(int r, int c, int mat[MAXROWS][MAXCOLS])

rows = r;

cols = c;

for (int i = 0; i< rows; i++)

{
for (int j = 0; j < cols; j++)

arr[i][j] = mat[i][j];

void display()

for (int i = 0; i< rows; i++)

cout<< " [ ";

for (int j = 0; j < cols; j++) {

cout<<arr[i][j] << ", ";

cout<< "]" <<endl;

cout<<endl;

Matrix operator+(Matrix x) {

if (x.rows != rows || x.cols != cols || (rows == 0 && cols == 0)) {


return Matrix();

int mat[MAXROWS][MAXCOLS];

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

for (int j = 0; j < cols; j++) {

mat[i][j] = arr[i][j] + x.arr[i][j];

return Matrix(rows, cols, mat);

int operator==(Matrix x) {

if (x.rows != rows || x.cols != cols) {

return 0;

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

for (int j = 0; j < cols; j++) {

if (arr[i][j] != x.arr[i][j]) {

return 0;

}
}

return 1;

};

int main()

int arr1[MAXROWS][MAXCOLS], arr2[MAXROWS][MAXCOLS];

arr1[0][0] = 1;

arr1[0][1] = 2;

arr1[1][0] = 3;

arr1[1][1] = 4;

arr2[0][0] = 4;

arr2[0][1] = 3;

arr2[1][0] = 2;

arr2[1][1] = 1;

Matrix mat1(2, 2, arr1);

Matrix mat2(2, 2, arr1);

Matrix mat3(2, 2, arr2);

Matrix mat4;

cout<< "Elements of Matrix 1:" <<endl;

mat1.display();
cout<< "Elements of Matrix 2:" <<endl;

mat2.display();

cout<< "Elements of Matrix 3:" <<endl;

mat3.display();

mat4 = mat1 + mat3;

cout<< "Elements of Matrix after addition of Matrix 1 and Matrix 3:" <<endl;

mat4.display();

if (mat1 == mat2) {

cout<< "Matrix 1 is equals to Matrix 2" <<endl;

else {

cout<<"1 is not equals to Matrix 2" <<endl;

if (mat1 == mat3) {

cout<< "Matrix 1 is equals to Matrix 3" <<endl;

else {

cout<< "Matrix 1 is not equals to Matrix 3" <<endl;

return 0;

}
OUTPUT:
Q.4 WAP to create a class Pairs. Objects of type Pairs can be used
in any situation where ordered pairs are needed. Our Task is to
overload operator >> and << so that objects of class Pairs are to be
input and output in the form (5,3) (5,-6) (-5,6) or (-5,-3).There is no
need to implement any constructor/method .

ALGORITHM:

 Including the bits/stdc++.h header file in our code to use its functions.
 Include the std namespace in our program to use its classes without
calling it.
 Create an char array named “numpair[20]”.
 Create a friend function to overload the output operator

friend ostream&operator<<(ostream&output, const Pairs &p)

 Create a friend function to overload the input operator

friend istream&operator>>(istream&input, Pairs &p)


 Create char pair[20] for inputting the pair value and then getting the
length of the inputed string.
 In int len store the length of string as per given format "(x,y)" the min
length of string should be 5 and also the string should contain the first
character "(", the last character ")"and a comma in between the string.
 Check all the conditions in the if statement.
 If the all conditions are satisfied then print the value as if not tell the user
that its an invalid input.
 Create main function and then create object.
 Take pairs as input from the user.
CODE:

#include <bits/stdc++.h>
using namespace std;
class Pairs {
private:
char numpair[20];
public:
friend ostream&operator<<(ostream&output, const Pairs &p) {
output<<p.numpair;
return output;
}
friend istream&operator>>(istream&input, Pairs &p) {
char pair[20];
input>> pair;
int len = strlen(pair);
if (len< 5 || pair[0] != '(' || pair[len - 1] != ')' || !strstr(pair, ",")) {
cout<< "Invalid pair value found!" <<endl;
strcpy(p.numpair, "");
}
else {
strcpy(p.numpair, pair);
}
return input;
}
};
int main() {
Pairs p;
cout<< "Enter the value of pair object: ";
cin>> p;
cout<< "Entered pair value is: " << p <<endl;
return 0;
}
OUTPUT:
LEARNING OUTCOMES
 Identify situations where computational methods would be useful.
 Approach the programming tasks using techniques learnt and write
pseudo-code.
 Choose the right data representation formats based on the
requirements of theproblem.
 Use the comparisons and limitations of the various programming
constructs andchoose the right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only)

Sr. No. Parameters Maximum Marks


Marks Obtaine
d
1. Worksheet Completion including 10
writinglearning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in 5
Simulation/Performance/ Pre
Lab Questions
4. Total Marks 20

You might also like