Matrix operations using classes So I recently had a practical exam in which I was told to execute a function of a menu oriented matrix operation program using classes. The code I made was supposedly wrong as I hadn't used classes the way they have to be used. Can anybody help me rectify the error here. I'm a noob (less than 2 months into C++) so please pardon any nooby mistakes.

 #include &lt;iostream&gt;
    using namespace std;
    class matrix3
    {
        int a[10][10],b[10][10],c[10][10],d[10][10],e[10][10],f[10][10],x,y,i,j;
        public :
            void values();
            void transpose();
            void sum();
            void diff();
    };
    void matrix3::values()
    {
        cout &lt;&lt; "Enter the rows   "; cin &gt;&gt; x;
        cout &lt;&lt; "Enter the columns   "; cin &gt;&gt; y;
        cout &lt;&lt; "Enter elements of first matrix\n\n";
        for (i=1; i&lt;=x; i++)
        {
            for ( j=1; j&lt;=y; j++)
            {
                cin &gt;&gt; a[i][j];
            }
        }
        cout &lt;&lt; "Enter elements of second matrix\n\n";
        for (i=1; i&lt;=x; i++)
        {
            for (j=1; j&lt;=y; j++)
            {
                cin &gt;&gt; c[i][j];
            }
        }
    }
    void matrix3::sum()
    {
        cout &lt;&lt; "Sum of Matrices 1 and 2 is\n";
        for (i=1; i&lt;=x; i++)
        {
            for ( j=1; j&lt;=y; j++)
            {
                e[i][j]=a[i][j]+c[i][j];
                cout &lt;&lt; e[i][j] &lt;&lt; "";
            }
            cout &lt;&lt; endl;
        }

    }
    void matrix3::diff()

    {
    cout &lt;&lt; "Difference of Matrices 1 and 2 (1-2) is\n";
        for (i=1; i&lt;=x; i++)
        {
            for ( j=1; j&lt;=y; j++)
            {
                f[i][j] = a[i][j]-c[i][j];
                cout &lt;&lt; f[i][j] &lt;&lt; "";
            }
            cout &lt;&lt; endl;
        }   
    }
    void matrix3::transpose()
    {
        cout &lt;&lt; "transpose of the matrix is\n";
        for ( i=1; i&lt;=x; i++)
        {
            for ( j=1; j&lt;=y; j++)
            {
                b[i][j] = a[j][i];
                cout &lt;&lt; b[i][j] &lt;&lt; "";
            }
            cout &lt;&lt; endl;
        }
        cout &lt;&lt; "Transpose of the second matrix is\n";
        for ( i=1; i&lt;=x; i++)
        {
            for ( j=1; j&lt;=y; j++)
            {
                d[i][j] = c[j][i];
                cout &lt;&lt; b[i][j] &lt;&lt; "";
            }
            cout &lt;&lt; endl;
        }
    }
    int main()
    {
        int input;
        char ch;    
        matrix3 m;
        m.values();
        do
         {
        cout &lt;&lt; "Enter your choice\n";
        cout &lt;&lt; " 1. Sum of 1 and 2\n" &lt;&lt; " 2. Difference of 1 and 2\n" &lt;&lt; " 3. Transpose of both 1 amd 2\n";
        cin &gt;&gt; input;
        switch (input)
        {
            case 1:
                m.sum();
                break;

            case 2:
                m.diff();
                break;

            case 3:
                m.transpose();
                break;
        }
        cout &lt;&lt; "\nDo another y/n?";
        cin &gt;&gt; ch;
        }
        while (ch!= 'n');
        cout &lt;&lt; "\n";
        system ("pause");
    return 0;
    }
Partial Answer This does not cover all aspects of your code, so feel free to wait for other responses.

In OOP, it is important to identify the scope of an object. In your case, you have two input matrices and possibly a result matrix for the operation that the user selects. The key point is, that each matrix should be a distinguished instance of your matrix class. What you have is something like a matrix manager, that contains multiple matrices in a single instance.

When you re-design your matrix class to contain a single matrix, you should possibly externalize your user input logic. Also, the sum, diff and transpose functions could be external functions that take two matrices as parameters and return a resulting matrix. However, I guess it would also be ok to have them as member function that only takes the second matrix as parameter.

Consider the following class definition:

#include &lt;vector&gt;

class matrix {
private:
    // matrix with m columns and n rows
    int _m, _n;
    // values, organized as an outer vector of rows
    // and an inner vector of columns
    std::vector&lt;std::vector&lt;int&gt;&gt; _values;

public:
    // initialize matrix by its size
    matrix(int m, int n)
        : _m(m), _n(n)
    {
        // initialize the matrix size
        _values.resize(n);
        for (int i = 0; i &lt; n; ++i)
        {
            _values[i].resize(m);
        }
    }

    // TODO:
    // - other constructors
    // - copy operator
    // - other desired functions
    // - assessors to the matrix elements
};


Maybe you can start from there.