Professional Documents
Culture Documents
WORKSHEET For Practical 2.2
WORKSHEET For Practical 2.2
WORKSHEET For Practical 2.2
Experiment - 6.1
AIM:
Write a Program to calculate and display cube of an integer and float variable using
function overloading.
FLOWCHART/ALGORITHM:
Step 1: Start
Step 2: Make a function
Step 3: Enter any Number
Step 4: Make a function of cube.
Step 5: Take integer variable number
Step 6: Take the float number also
Step 7: Take integer variable cube
Step 8: Take the cube variable
Step 9: Multiply N three times
Step 10: Display result as Cube
Step 11: End
cout<< "Cube of Integer Number " << a << " is " << cube(a) <<endl;
cout<< "Cube of Float Number " << b << " is " << cube(b) <<endl;
return 0;
}
int cube(int x)
{
return x*x*x;
}
float cube(float y)
{
return y*y*y;
}
PROGRAM EXPLANATION:
Function overloading is a feature where two or more functions can have the same name
but different parameters. In this program we have used function overloading to
calculate cube of an integer and float number and display them.
Experiment - 6.2
AIM:
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.
FLOWCHART/ALGORITHM:
Step 1: Start
Step 2: Declare the class
Step 3: Declare the default constructor of initial variables
Step 4: Define the operator ++ as operator loading to increment values
Step 5: Define the display function
Step 6: Declare the class objects by passing arguments.
Step 7: Call the function to display the results
Step 8: End
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test()
{
num = 0;
}
Test(int n)
{
num = n;
}
void display()
{
cout<< "Number: " <<num<<endl;
}
Test operator++ ()
{
++num;
return Test(num);
}
Test operator++(int)
{
Test t(num);
++num;
return t;
}
};
PROGRAM EXPLANATION:
In this program, we have first declared a class test then the variables and its member
function. We defined Overload operator ++ with definition to pre-decrement the value
of a variable and overload operator ++ with definition to post-decrement the value of
variable. Ending the program by defining the display function.
Experiment - 6.3
AIM:
WAP for creating a matrix class which can handle integer matrices of different
dimensions. Overload the operator (+) for addition and (==) comparison of matrices.
FLOWCHART/ALGORITHM:
Step 1: Start
Step 2: Declare the class Matrix
Step 3 Declare the default constructor of initial variables
Step 4: Define the variables for putting in matrix.
Step 5: Declare the class objects by passing arguments.
Step 6: Make the objects for matrix
Step 7: Call that to display the results
Step 8: End
#include <iostream>
#define MAXROWS 50
#define MAXCOLS 50
using namespace std;
class Matrix
{
public:
int arr[MAXROWS][MAXCOLS];
int rows, columns;
Matrix()
{
rows = columns = 2;
}
Matrix(int r, int c, int mat[MAXROWS][MAXCOLS])
{
rows = r;
columns = c;
for (int i = 0; i< rows; i++)
{
for (int j = 0; j < columns; j++)
{
arr[i][j] = mat[i][j];
}
}
}
void display()
{
for (int i = 0; i< rows; i++)
{
cout<< " [ ";
for (int j = 0; j < columns; j++)
{
cout<<arr[i][j] << ", ";
}
int mat[MAXROWS][MAXCOLS];
for (int i = 0; i< rows; i++)
{
for (int j = 0; j < columns; j++)
{
mat[i][j] = arr[i][j] + x.arr[i][j];
}
}
return Matrix(rows, columns, mat);
}
int operator==(Matrix x)
{
if (x.rows != rows || x.columns != columns)
{
return 0;
}
for (int i = 0; i< rows; i++)
{
for (int j = 0; j < columns; 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;
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
arr1[1][1] = 4;
arr2[0][0] = 4;
arr2[0][1] = 3;
arr2[1][0] = 2;
arr2[1][1] = 1;
if (mat1 == mat2)
{
cout<< "Matrix 1 is equals to Matrix 2 " <<endl;
}
else
{
cout<< "Matrix 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;
}
PROGRAM EXPLANATION:
In this program we have created a matrix class to handle the integer matrices of
different dimension and used the overload operator + for addition and == for
comparison of matrices.
OUTPUT:
Experiment - 6.4
AIM:
Write a program 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.
FLOWCHART/ALGORITHM:
Step 1: Start
Step 2: Declare the class Pairs.
Step 3: Define the variables
Step 4: Declare the class objects by passing arguments.
Step 5: Make the objects
Step 6: Call that to display the results
Step 7: End
#include <iostream>
#include <cstring>
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;
}
PROGRAM EXPLANATION:
Start the program by declaring the class Pairs and defining the required variables.
Declare the class objects by passing arguments and later create objects. The objects
created are used to call during the displaying of the result thereby, ending the program.
OUTPUT: