WORKSHEET For Practical 2.2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Student Name: abhinav pandey Branch: CSE (General)

UID: 20BCS9676 Class/Group: CSE 32C


Date of Performance: 13/04/2021 Subject Name: Object Oriented
Semester: Second Programming Using C++ Lab

TOPIC OF EXPERIMENT: Inheritance, Polymorphism, Pointers and


Virtual Functions

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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAM CODE:
#include <iostream>
using namespace std;
int cube(int);
float cube(float);
int main()
{
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:


Following were the errors encountered during the program:
 Missed out semicolons and brackets
 Misplacing the insertion operator

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.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
Student Name: Saanjh Sengupta Branch: CSE (General)
UID: 20BCS1503 Class/Group: CSE 4(A)
Date of Performance: 09/04/2021 Subject Name: Object Oriented
Semester: Second Programming Using C++ Lab

TOPIC OF EXPERIMENT: Inheritance, Polymorphism, Pointers and


Virtual Functions

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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAM CODE:

#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;
}
};

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
int main()
{
Test T1(11), T2(11), T3;
++T1;
T1.display ();
T2++;
T2.display ();
T3.display ();
T3 = T2++;
T2.display ();
T3.display ();
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION:


Following were the errors encountered during the program:

 Missed the namespace std at the beginning of the program


 Absence of semicolons for terminating every code line

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.

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
OUTPUT:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
Student Name: Saanjh Sengupta Branch: CSE (General)
UID: 20BCS1503 Class/Group: CSE 4(A)
Date of Performance: 09/04/2021 Subject Name: Object Oriented
Semester: Second Programming Using C++ Lab

TOPIC OF EXPERIMENT: Inheritance, Polymorphism, Pointers and


Virtual Functions

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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAM CODE:

#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] << ", ";
}

cout<< "]" <<endl;


}
cout<<endl;
}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
Matrix operator+(Matrix x)
{
if (x.rows != rows || x.columns != columns || (rows == 0 && columns == 0))
{
return Matrix();
}

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;

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<< "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;
}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION:
Following were the errors encountered during the program:

 Missed the namespace std at the beginning of the program


 Absence of semicolons for terminating every code line
 Missed the dot operator for calling

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:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
Student Name: Saanjh Sengupta Branch: CSE (General)
UID: 20BCS1503 Class/Group: CSE 4(A)
Date of Performance: 09/04/2021 Subject Name: Object Oriented
Semester: Second Programming Using C++ Lab

TOPIC OF EXPERIMENT: Inheritance, Polymorphism, Pointers and


Virtual Functions

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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
PROGRAM CODE:

#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;
}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION:
Following were the errors encountered during the program:

 Missed the namespace std at the beginning of the program


 Absence of semicolons for terminating every code line

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:

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
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 including 10
writing learning objective/ Outcome
2. Post Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre Lab Questions
4. Total Marks 20

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152

You might also like