0% found this document useful (0 votes)
71 views6 pages

C++ Worksheet 6

The document describes two programming experiments conducted by a student to learn polymorphism and operator overloading in C++. The first experiment involves overloading the cube function for integer and float types. The second experiment overloads the unary ++ operator (pre-increment and post-increment) for a class. Both programs execute without errors and produce the expected output.

Uploaded by

Ishu Rattan
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)
71 views6 pages

C++ Worksheet 6

The document describes two programming experiments conducted by a student to learn polymorphism and operator overloading in C++. The first experiment involves overloading the cube function for integer and float types. The second experiment overloads the unary ++ operator (pre-increment and post-increment) for a class. Both programs execute without errors and produce the expected output.

Uploaded by

Ishu Rattan
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/ 6

Experiment-6

Aim of the experiments-


To learn the concept of polymorphism.

Student’s name -ISHU RATTAN

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>

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;

Error encountered during programs execution:

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>

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

           ++num;

           // return object with increment value

return Test(num);

       }

       // overloaded postfix ++ operator

       Test operator++( int ) {

           // save the orignal 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

T3.display();   // display T3

return 0;

Error encountered during programs execution:

No

Program Explanation:

1. Number-12
2. Number-12
3. Number-0
4. Number-13
5. Number-12
Output:

THANK YOU !

You might also like