Matrix Class with Constructor, Destructor, Copy constructor and Assignment operator overloading
Aim:
            To implement Matrix class with dynamic memory allocation with constructors, destructor, and overloading of assignment operator.
Algorithm:
- Start the      process.
 - Create a      class matrix.
 - Declare and      define default, parameterized, copy constructor.
 - Define      member functions getmatrix and showmatrix.
 - Define      destructor.
 - Get the      size of the matrix and the elements of the matrix.
 - Depends on      the size of the matrix, the memory is allotted for the matrix dynamically.
 - Get the      elements of the matrix. 
 - Call the      copy constructor and copy the values of object m1 to m2.
 - With the      help of assignment operator, we are assigning the values of m1 to m3.
 - Display the      result.
 - Stop the      process.
 
Program :
#include<iostream.h>
#include<conio.h>
class matrix
{
            int **m;
            int row, col;
public:
            matrix()
            {
                        row=col=0;
                        m=NULL;
            }
            matrix(int r ,int c);
            ~matrix();
            void getmatrix();
            void showmatrix();
            matrix(matrix &m2); //copy constructor
            matrix& operator=(matrix &m2);
};
matrix::~matrix()
{
            for(int i=0;i<row;i++)
            delete m[i];
            delete m;
}
matrix::matrix(int r ,int c)
{
            row=r;
            col=c;
            m=new int*[row];
            for(int i=0;i<row;i++)
            m[i]=new int[col];
}
matrix::matrix(matrix &m2)
{
            cout<<"\nCopy constructor invoked\n";
            row=m2.row;
            col=m2.col;
            m=new int*[row];
            for(int i=0;i<=row;i++)
            m[i]=new int[col];
            for(i=0;i<=row;i++)
            for(int j=0;j<=row;j++)
            m[i][j]=m2.m[i][j];
}
matrix& matrix::operator=(matrix &m2)
{
            cout<<"\nAssignment Operator Overloading\n";
            row = m2.row;
            col = m2.col;
            m = new int*[row];
            for(int i=0;i<=row;i++)
            m[i]=new int[col];
            for(i=0;i<=row;i++)
            for(int j=0;j<=row;j++)
            m[i][j]=m2.m[i][j];
            return *this;
}
void matrix::getmatrix()
{
            for(int i=0;i<row;i++)
            for(int j=0; j<col; j++)
                        cin>>m[i][j];
}
void matrix::showmatrix()
{
            for(int i=0;i<row;i++)
            {
                        for(int j=0;j<col;j++)
                                    cout<<"\t"<<m[i][j];
                                    cout<<"\n";
            }
}
void main()
{
            int r,c;
            clrscr();
            cout<<"\nEnter rows and columns of the matrix:\n";
            cin>>r>>c;
            matrix m1(r,c);
            cout<<"\nEnter the matrix elements one by one:\n";
            m1.getmatrix();
            cout<<"\nEntered matrix is:\n";
            m1.showmatrix();
            //invoking copy constructor
            matrix m2=m1;
            cout<<"\nResult of the copy constructor is:\n";
            m2.showmatrix();
            matrix m3;
            m3=m1;
            cout<<"\nResult of assignment operator overloading:\n";
            m3.showmatrix();
            getch();
}
OUTPUT 1:
Enter rows and columns of the matrix:
3
2
Enter the matrix elements one by one:
2
3
1
4
5
7
Entered matrix is:
        2       3
        1       4
        5       7
Copy constructor invoked
Result of the copy constructor is:
        2       3
        1       4
        5       7
Assignment Operator Overloading
Result of assignment operator overloading:
        2       3
        1       4
        5       7
Output 2: 
Enter rows and columns of the matrix:
3 3
Enter the matrix elements one by one:
12 23 34 45 56 67 78 89 90
Entered matrix is:
        12      23      34
        45      56      67
        78      89      90
Copy constructor invoked
Result of the copy constructor is:
        12      23      34
        45      56      67
        78      89      90
Assignment Operator Overloading
Result of assignment operator overloading:
        12      23      34
        45      56      67
        78      89      90
Result:
Thus the program for constructor, destructor, copy constructor and assignment operator overloading was executed.
0 comments:
Post a Comment