0% found this document useful (0 votes)
57 views19 pages

Experiment Number - 2.2 (A) : Learn How To Perform Function Overloading

The document describes an experiment to demonstrate function overloading in C++. It defines two functions named 'cube' - one that takes an integer parameter and calculates the cube of an integer, and one that takes a float parameter and calculates the cube of a float. The main function calls the cube function, passing an integer and float variable as parameters. It displays the output, specifying whether it is an integer or float cube value. The program successfully demonstrates function overloading without any errors.

Uploaded by

RW AKSHAT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views19 pages

Experiment Number - 2.2 (A) : Learn How To Perform Function Overloading

The document describes an experiment to demonstrate function overloading in C++. It defines two functions named 'cube' - one that takes an integer parameter and calculates the cube of an integer, and one that takes a float parameter and calculates the cube of a float. The main function calls the cube function, passing an integer and float variable as parameters. It displays the output, specifying whether it is an integer or float cube value. The program successfully demonstrates function overloading without any errors.

Uploaded by

RW AKSHAT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

EXPERIMENT NUMBER – 2.

2(A)

STUDENT’S NAME – AKSHAT KUMAR

STUDENT’S UID – 22BAI70817

CLASS AND GROUP – BE-CSE (AIML) - 102 / B

SEMESTER – 2nd

AIM OF THE EXPERIMENT (A) –

Learn how to perform Function Overloading

TITLE:
WAP to calculate and display cube of an integer and float
variable using function overloading.

FLOW CHART/ ALGORITHM -


1. Ask the user to enter a number.

2. Read the input as a variable.

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.

7. Display the result on the screen, specifying whether it is an integer or a float.

PROGRAM CODE –

#include <iostream>

using namespace std;

int cube(int );

float cube(float);

int main() {

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


int a = 5;

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;

float cube(float y){

return y*y*y;

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –

1. No error occurred.

PROGRAMS’ EXPLANATION (in brief) –


Function overloading enables you to define two or more functions with the same name in
a program. In this case, we will define two functions named 'cube' - one for the integer
variable and another for the float variable.

OUTPUT –

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


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 the problem.
• Use the comparisons and limitations of the various programming constructs and choose the
right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only) –

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


Experiment 2.2(B)

STUDENT’S NAME – Akshat Kumar

STUDENT’S UID – 22BAI70817

CLASS AND GROUP – BE-CSE (AIML) - 102 / B

SEMESTER – 2nd

AIM OF THE EXPERIMENT (B)–

Learn how to perform Function Overloading

TITLE:

1) 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-
increment the value of a variable 2) Overload operator
++ (post) with definition to post- increment the value of
variable
FLOW CHART/ ALGORITHM –

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


PROGRAM CODE –

#include <iostream>

using namespace std;

class Test {

private:

int num;

public:

// required constructors

// default constructor to initlize the variable

Test() {

num = 0;

// parameterized constructor to return object after incrementing

Test(int n) {

num = n;

// method to display time

void display() {

cout<< "Number: " <<num<<endl;

// overloaded prefix ++ operator

Test operator++ () {

// increment this object

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


++num;

// return object with increment value

return Test(num);

// overloaded postfix ++ operator

Test operator++( int ) {

// save the original value

Test t(num);

// increment current object

++num;

// return old original value

return t;

};

int main() {

Test T1(11), T2(11), T3;

++T1; // increment T1

T1.display (); // display T1

T2++; // increment T2

T2.display (); // display T2

T3.display (); // display T3

T3 = T2++; // increment T2 again and assign pre-incremented value to T3

T2.display (); // display T2

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


T3.display (); // display T3

return 0;

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –

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 -

SUBJECT NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


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 the problem.
• Use the comparisons and limitations of the various programming constructs and choose
the right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only) –

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


EXPERIMENT NUMBER – 2.2(C)

STUDENT’S NAME – AKSHAT KUMAR

STUDENT’S UID – 22BAI70817

CLASS AND GROUP – BE-CSE (AIML) - 102 / B

SEMESTER – 2nd

AIM OF THE EXPERIMENT (C) –

Learn how to perform Function Overloading

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

FLOW CHART/ ALGORITHM –

PROGRAM CODE –

#include <iostream>

#define MAXROWS 50

#define MAXCOLS 50
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
using namespace std;

// Class for Matrix operator overloading

class Matrix {

public:

// For input Matrix

intarr[MAXROWS][MAXCOLS];

int rows, cols;

Matrix() {

rows = cols = 2;

// Overloaded constructor to initlize the Matrix with dimensions

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];

// Function to display the elements of Matrix

void display() {

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

cout<< " [ ";

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


NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
// Print the element

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

cout<< "]" <<endl;

cout<<endl;

// Function for + operator overloading

Matrix operator+(Matrix x) {

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

return Matrix();

// To store the sum of Matrices

int mat[MAXROWS][MAXCOLS];

// Traverse the Matrix x

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

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

// Add the corresponding blocks of Matrices

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

return Matrix(rows, cols, mat);

// Function for == operator overloading

int operator==(Matrix x) {
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
if (x.rows != rows || x.cols != cols) {

return 0;

// Travarse the Matrix x

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

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

// Add the corresponding blocks of Matrices

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

return 0;

return 1;

};

int main()

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

// inputing values to array 1

arr1[0][0] = 1;

arr1[0][1] = 2;

arr1[1][0] = 3;

arr1[1][1] = 4;

// inputing values to array 2

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 mat1(2, 2, arr1);

Matrix mat2(2, 2, arr1);

Matrix mat3(2, 2, arr2);

Matrix mat4;

// Printing the elements of first matrix

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

mat1.display ();

// Printing the elements of second matrix

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

mat2.display ();

// Printing the elements of third matrix

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

mat3.display ();

// Addition of two matrices using operator overloading

mat4 = mat1 + mat3;

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

mat4.display ();

// Equating two matrices using operator overloading

if (mat1 == mat2) {

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

}
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
else {

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

// Equating two matrices using operator overloading

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;

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –

1. No error occurred.

PROGRAMS’ EXPLANATION (in brief) –


This program creates a matrix class that can handle integer matrices of different dimensions.
It overloads the addition operator (+) to allow adding two matrices and the equality operator
(==) to compare two matrices. The result of addition or comparison can be stored or
displayed.
OUTPUT –

NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


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 the problem.
• Use the comparisons and limitations of the various programming constructs and choose the right
one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only) –

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


Experiment 2.2(D)

STUDENT’S NAME – Akshat Kumar

STUDENT’S UID – 22BAI70817

CLASS AND GROUP – BE-CSE (AIML) - 102 / B

SEMESTER – 2nd

AIM OF THE EXPERIMENT (D)–

Learn how to perform Function Overloading

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 .

FLOW CHART/ ALGORITHM –

PROGRAM CODE –

#include <iostream>

// for string functions

#include <cstring>
NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103
using namespace std;

class Pairs {

private:

char numpair[20];

public:

// friend function to overload the output operator

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

output<<p.numpair;

return output;

// friend function to overload the input operator

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

char pair[20];

// inputting the pair values

input>> pair;

// getting the length of the inputed string

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 ")"

// and a comma in between the string

if (len< 5 || pair[0] != '(' || pair[len - 1] != ')' || !strstr(pair, ",")) {

cout<< "Invalid pair value found!" <<endl;

// in case of Invalid value assing blank string to the pair value

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;

cout<< "Enter the value of pair object: ";

cin>> p;

cout<< "Entered pair value is: " << p <<endl;

return 0;

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –


1. No errors occurred.
PROGRAMS’ EXPLANATION (in brief) –
The program aims to create a class called Pairs that can be used to represent ordered pairs of numbers. The
class should be able to handle any pair of integers and overload the input and output operators >> and <<
respectively. The input and output format for the pairs should be in the form of (x,y), where x and y are
integers. No constructor or method needs to be implemented in this program

OUTPUT -

NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103


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 the problem.
• Use the comparisons and limitations of the various programming constructs and choose
the right one for the task.

EVALUATION COLUMN (To be filled by concerned faculty only) –

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion 10

2. Viva 8

3. Conduct 12

Total Marks 30

NAME- Object Oriented Programming Using C++ SUBJECT CODE-22CSH103

You might also like