0% found this document useful (0 votes)
72 views14 pages

C++ Practical 6

The document describes three experiments related to object-oriented programming in C++. The first experiment involves function overloading to calculate the cube of integer and float variables. The second experiment demonstrates unary operator overloading for the prefix and postfix increment operators. The third experiment creates a matrix class that can handle integer matrices of different dimensions, and overloads the addition and equality operators for matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views14 pages

C++ Practical 6

The document describes three experiments related to object-oriented programming in C++. The first experiment involves function overloading to calculate the cube of integer and float variables. The second experiment demonstrates unary operator overloading for the prefix and postfix increment operators. The third experiment creates a matrix class that can handle integer matrices of different dimensions, and overloads the addition and equality operators for matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXPERIMENT NUMBER –Practical 6.

1
STUDENT’S NAME –MOHD ALI

STUDENT’S UID – 21BCS7757

CLASS AND GROUP –417-B

SEMESTER –2ND

TOPIC OF EXPERIMENT – POLYMORPHISM

AIM OF THE EXPERIMENT - WAP to calculate and display cube of an

integer and float variable using function overloading.

PROGRAM CODE

#include<iostream>
using namespace std;

float cube(float a)
{
float u;
u=a*a*a;
return(u);
}
int cube(int b)
{
int y=b*b*b;
return y;
}
int main()
{

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
//function prototype
float a,cu;
int p,y;
cout<<"\nEnter any number :: ";
cin>>a>>p;

cu=cube(a);
y=cube(p); //function calling

cout<<"\nThe Cube of Number [ "<<a<<" and "<<p<<" ] is :: "<<cu << " AND " << y <<"\n";

return 0;
}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION
(Kindly jot down the compile time errors encountered)

NONE

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 6.2
STUDENT’S NAME –MOHD ALI

STUDENT’S UID – 21BCS7757

CLASS AND GROUP –417-B

SEMESTER –2ND

TOPIC OF EXPERIMENT – AIM

OF THE EXPERIMENT

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.

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;}// 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
++num;// return object with increment valuereturn
Test(num);}// overloaded postfix ++ operator
Test operator++( int ) {
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
T3.display(); // display T3
return 0;}

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION
(Kindly jot down the compile time errors encountered)

NONE

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 6.3
STUDENT’S NAME –MOHD ALI

STUDENT’S UID – 21BCS7757

CLASS AND GROUP –417-B

SEMESTER –2ND

TOPIC OF EXPERIMENT – AIM

OF THE EXPERIMENT

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
using namespace std;
class Matrix {
public:int arr[MAXROWS][MAXCOLS];
int rows, cols;
Matrix() {
rows = cols = 2;
}
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];
}
}
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
}
void display()
{
for(int i = 0; i< rows; i++)
{cout<< " [ ";
for (int j = 0; j < cols; j++)
{cout<<arr[i][j] << ", ";}
cout<< "]" <<endl;
}
cout<<endl;
}
Matrix operator+(Matrix x)
{
if (x.rows != rows || x.cols != cols || (rows == 0 &&
cols == 0))
{
return Matrix();
}
int mat[MAXROWS][MAXCOLS];
for (int i = 0; i< rows; i++)
{
for (int j = 0; j < cols; j++)
{
mat[i][j] = arr[i][j] + x.arr[i][j];
}
}
return Matrix(rows, cols, mat);
}
int operator==(Matrix x){

if (x.rows != rows || x.cols != cols)


{return 0;
}
for (int i = 0; i< rows; i++)
{for (int j = 0; j < cols; 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;
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);
SUBJECT NAME- OBJECT ORIENTED PROGRAMMING
USING C++ LAB
SUBJECT CODE-CSP-152
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
(Kindly jot down the compile time errors encountered)

NONE

OUTPUT

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
EXPERIMENT NUMBER –Practical 6.4
STUDENT’S NAME –MOHD ALI

STUDENT’S UID – 21BCS7757

CLASS AND GROUP –417-B

SEMESTER –2ND

TOPIC OF EXPERIMENT – AIM

OF THE EXPERIMENT

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>
using namespace std;
class Pairs {
private:
char numpair[20];
public:
friend ostream&operator<<(ostream&output, const Pairs &p)
{
output<<p.numpair;

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
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
(Kindly jot down the compile time errors encountered)

NONE

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

SUBJECT NAME- OBJECT ORIENTED PROGRAMMING


USING C++ LAB
SUBJECT CODE-CSP-152
Sr. No. Parameters Maximum Marks
Marks Obtained
1. Worksheet Completion including writing 10
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