Experiment Number - 2.2 (A) : Learn How To Perform Function Overloading
Experiment Number - 2.2 (A) : Learn How To Perform Function Overloading
2(A)
SEMESTER – 2nd
TITLE:
WAP to calculate and display cube of an integer and float
variable using function overloading.
3. Create two functions called cube, one that takes an integer parameter and one that takes a float parameter.
4. Define the cube function for an integer parameter by multiplying the parameter by itself three times (i.e., parameter *
parameter * parameter), and store the result in a variable.
5. Define the cube function for a float parameter in the same way, by multiplying the parameter by itself three times and
storing the result in a variable.
6. Write a main function that calls the cube function, passing the user's input variable as the parameter.
PROGRAM CODE –
#include <iostream>
int cube(int );
float cube(float);
int main() {
float b = 5.5;
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;
return y*y*y;
1. No error occurred.
OUTPUT –
2. Viva 8
3. Conduct 12
Total Marks 30
SEMESTER – 2nd
TITLE:
#include <iostream>
class Test {
private:
int num;
public:
// required constructors
Test() {
num = 0;
Test(int n) {
num = n;
void display() {
Test operator++ () {
return Test(num);
Test t(num);
++num;
return t;
};
int main() {
++T1; // increment T1
T2++; // increment T2
return 0;
1. No errors occurred.
PROGRAMS’ EXPLANATION (in brief) –
The program creates a class "Test" and overloads the unary operator "++" for both pre-increment and
post-increment operations. A default constructor initializes the variable.
OUTPUT -
2. Viva 8
3. Conduct 12
Total Marks 30
SEMESTER – 2nd
TITLE:
WAP for creating a matrix class which can handle integer matrices of different
dimensions. Overload the operator (+) for addition and (==) comparison of matrices..
PROGRAM CODE –
#include <iostream>
#define MAXROWS 50
#define MAXCOLS 50
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
using namespace std;
class Matrix {
public:
intarr[MAXROWS][MAXCOLS];
Matrix() {
rows = cols = 2;
rows = r;
cols = c;
arr[i][j] = mat[i][j];
void display() {
cout<<endl;
Matrix operator+(Matrix x) {
return Matrix();
int mat[MAXROWS][MAXCOLS];
int operator==(Matrix x) {
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
if (x.rows != rows || x.cols != cols) {
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;
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
arr2[0][1] = 3;
arr2[1][0] = 2;
arr2[1][1] = 1;
// Declare Matrices
Matrix mat4;
mat1.display ();
mat2.display ();
mat3.display ();
cout<< "Elements of Matrix after addition of Matrix 1 and Matrix 3:" <<endl;
mat4.display ();
if (mat1 == mat2) {
}
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
else {
if (mat1 == mat3) {
else {
return 0;
1. No error occurred.
2. Viva 8
3. Conduct 12
Total Marks 30
SEMESTER – 2nd
TITLE:
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 .
PROGRAM CODE –
#include <iostream>
#include <cstring>
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
using namespace std;
class Pairs {
private:
char numpair[20];
public:
output<<p.numpair;
return output;
char pair[20];
input>> pair;
intlen = strlen(pair);
// 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 ")"
strcpy(p.numpair, "");
}
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
else {
// in case of valid value copy the input value to the object member datq
strcpy(p.numpair, pair);
return input;
};
int main() {
Pairs p;
cin>> p;
return 0;
OUTPUT -
2. Viva 8
3. Conduct 12
Total Marks 30