Professional Documents
Culture Documents
2
STUDENT’S NAME – Akankshya Jena
STUDENT’S UID –20BCS9510
CLASS AND GROUP –13B
SEMESTER –2
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.
#include<bits/stdc++.h>
class one
int x;
public:
one()
x=50;
x=x+1;
x=x+1;
int display()
{
return(x);
};
int main()
one a, b;
cout<<"Before A: "<<a.display();
cout<<"\nBefore B: "<<b.display();
a++,++b;
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
#include <iostream>
#define MAXROWS 50
#define MAXCOLS 50
class Matrix {
public:
int arr[MAXROWS][MAXCOLS];
Matrix()
rows = cols = 2;
rows = r;
cols = c;
{
for (int j = 0; j < cols; j++)
arr[i][j] = mat[i][j];
void display()
cout<<endl;
Matrix operator+(Matrix x) {
int mat[MAXROWS][MAXCOLS];
int operator==(Matrix x) {
return 0;
if (arr[i][j] != x.arr[i][j]) {
return 0;
}
}
return 1;
};
int main()
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 mat4;
mat1.display();
cout<< "Elements of Matrix 2:" <<endl;
mat2.display();
mat3.display();
cout<< "Elements of Matrix after addition of Matrix 1 and Matrix 3:" <<endl;
mat4.display();
if (mat1 == mat2) {
else {
if (mat1 == mat3) {
else {
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
#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.
3. Student engagement in 5
Simulation/Performance/ Pre
Lab Questions
4. Total Marks 20