Professional Documents
Culture Documents
1
STUDENT’S NAME – Aryaman Sharma
SEMESTER – 2
TOPIC OF EXPERIMENT –
WAP to calculate and display cube of an integer and float variable using function
overloading.
ALGORITHM –
STEP 1: START
STEP 2: Declare two function int cube(int)and float cube(float) before int main()
STEP 3: In int main() declare two variable and also assign them value
STEP 4: Display the cube of int and float number by using Cout
STEP 6: STOP
In this program first we declare int cube(int) and float cube(float) function before int
main(). Then in int main we declare and assign value to int a and float b, then by using
Cout we display the cube of integer number and float number.
Then after int main() we define int cube() and float cube() function like this,
OUTPUT –
EXPERIMENT NUMBER –Practical 6.1.2
STUDENT’S NAME – Aryaman Sharma
SEMESTER – 2
TOPIC OF 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.
ALGORITHM –
START
Create a class test
Declare int num privately
In the public class define Test
Initialize num with 0
Plug int n into Test, and keep value of num=n
Use unary operator++ in test and perform ++num
Return the value of Test(num)
After incrementing T1 it is printed by calling display function.
After incrementing T2 it is printed by calling display function.
Initial value of T3
After incrementing T2 it is printed by calling display function.
After assignment of Pre-increment object T2 it is printed by calling display
function.
STOP
PROGRAM CODE –
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –
In this program first, we create a default constructor of name test() and then create a
parameterized constructor test(int n). After it, we create a void display() member
function and then define pre increment and post increment overloading like this
Test operator++ () {
++num;
return Test(num);
}
Test operator++( int ) {
Test t(num);
++num;
return t;
}
Then in int main(), We create objects of class test() and by the help of it we call
member function and proceed it like
int main() {
Test T1(20), T2(20), T3;
++T1;
T1.display();
T2++;
T2.display();
T3.display();
T3 = T2++;
T2.display();
T3.display();
return 0;
}
OUTPUT –
EXPERIMENT NUMBER –Practical 6.1.3
STUDENT’S NAME – Aryaman Sharma
SEMESTER – 2
TOPIC OF EXPERIMENT –
WAP for creating a matrix class which can handle integer matrices of different
dimensions. Overload the operator (+) for addition and (==) comparison of matrices.
ALGORITHM –
START
STOP
PROGRAM CODE –
ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION –
In this program , we first define the preprocessor as maxrows to 50 and maxcols to 50.
Then we declare an array to input the values to three matrices. Then using function
overloading of addition operator(+) and compiler performs the addition of matrix 1
and matrix 3. Then using function overloading of comparison operator (==) , each
element of matrix 1 and matrix 2 are matched then the displays as they are equal and
then it compares matrix 1 and matrix 3 then it displays as they are not equal. These
are displayed on the screen . The output is shown below.
OUTPUT –
EXPERIMENT NUMBER –Practical 6.1.4
STUDENT’S NAME – Aryaman Sharma
SEMESTER – 2
TOPIC OF 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 .
ALGORITHM –
START
Create a class named Pairs.
Declare an array of characters.
The user is asked to enter the pair object.
If the pair object entered is in the form of (a,b) then the entered pair is
displayed on screen.
If it is in form of a,b then the cout is Invalid pair value found.
STOP
PROGRAM CODE –
In this program we first create a class named Pairs in which declare an array of
characters. In this the user is asked to enter the pair object. If the pair object entered
is in the form of (a,b) then the entered pair is displayed on screen otherwise if it is in
form of a,b then the out is Invalid pair value found. The output is shown below.
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.