C++ Program to find cube of a number using friend function


In this program, You will learn how to find a cube of a number using friend function in C++.


friend void findCube(Test t);

Example: How to find a cube of a number using friend function in C++.

#include<iostream>
using namespace std;

class Test {
private:
    int n;
public:

    void input() {
        cout << "Enter a number:";
        cin>>n;
    }

    friend void findCube(Test t);
};

void findCube(Test t) {
    int c;
    c = t.n * t.n * t.n;
    cout << "Cube is:" << c;
}

int main() {

    Test t;
    t.input();
    findCube(t);

    return 0;
}

Output:

Enter a number:5
Cube is:125