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


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


friend void findCube(Test t);


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

Run it Now
#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 << "\nCube is :" << c;
}

int main() {

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

    return 0;
}

Output:

Enter a Number :3
Cube is :27