C++ Worksheet 6
C++ Worksheet 6
Student’s UID-20BCS7755
Class/Group-22/A
Semester-2nd
Practical 6.1: WAP to calculate and display cube of an integer and float variable using function
overloading.
Flowchart/Algorithm:
1. Start
2. Enter integer, float,
3. Value of a and b.
4. Int cube
5. Return.
6. Float cube
7. Return.
Program Code:
#include <iostream>
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;
return y*y*y;
No
Program Explanation:
Output:
Experiment-6.2
Practical 6.2: 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
1. Start
2. Class test : private ,public.
3. Void display.
4. Int main
5. Result
Program code:
#include <iostream>
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 t(num);
++num;
return t;
}
};
int main() {
++T1; // increment T1
T1.display(); // display T1
T2++; // increment T2
T2.display(); // display T2
T3.display(); // display T3
T2.display(); // display T2
T3.display(); // display T3
return 0;
No
Program Explanation:
1. Number-12
2. Number-12
3. Number-0
4. Number-13
5. Number-12
Output:
THANK YOU !