nth Power of a number using Operator Overloading cpp
In this program, we are going see how to find and display nth Power of a number using Function Overloading in C++ Programming Language.

 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <math.h>
#include <conio.h>

class Power
{
    int n1, n2, res;

public:
    void setn1(int num1)
    {
        n1 = num1;
    }
    void setn2(int num2)
    {
        n2 = num2;
    }
    Power operator*(Power n)
    {
        Power m;
        m.res = pow(n1, n.n2);
        return m;
    }
    void display()
    {
        cout << "Result of Power: " << res;
    }
};

void main()
{
    clrscr();
    int n, p;
    cout << "Enter the number: ";
    cin >> n;
    cout << "Enter the power: ";
    cin >> p;
    Power p1;
    p1.setn1(n);
    Power p2;
    p2.setn2(p);
    Power p3;
    p3 = p1 * p2;
    p3.display();
    getch();
}

//.......Coded by SAHIL SHAIK

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <math.h>
using namespace std;

class Power
{
    int n1, n2, res;

    public:
    void setn1(int num1)
    {
        n1 = num1;
    }
    void setn2(int num2)
    {
        n2 = num2;
    }
    Power operator*(Power n)
    {
        Power m;
        m.res = pow(n1, n.n2);
        return m;
    }
    void display()
    {
        cout << "Result of Power: " << res;
    }
};

int main()
{
    int n, p;
    cout << "Enter the number: ";
    cin >> n;
    cout << "Enter the power: ";
    cin >> p;
    Power p1;
    p1.setn1(n);
    Power p2;
    p2.setn2(p);
    Power p3;
    p3 = p1 * p2;
    p3.display();
    return 0;
}

//.......Coded by SAHIL SHAIKH

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post