C++ Program to find square and cube of a number using class and object


In this program, You will learn how to find square and cube of a number using class and object in C++.


9 = 3 * 3

27 = 3 * 3 * 3


Example: How to find square and cube of a number using class and object in C++

Run it Now
#include<iostream>
using namespace std;

class Test {
public:
    int num, square, cube;

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

    void findSquare() {
        square = num * num;
    }

    void findCube() {
        cube = num * num * num;
    }

    void display() {
        cout << "\nSquare is : " << square;
        cout << "\nCube is : " << cube;

    }
};

int main() {

    Test obj;

    obj.input();
    obj.findSquare();
    obj.findCube();
    obj.display();

    return 0;
}

Output:

Enter a number : 3
Square is : 9
Cube is : 27