Operator overloading - c++ program, C/C++ Programming

Operator overloading - c++ program:

Write a program in c to define operator overloading.

class matrix{

 

                private :

                int x[2][2];

 

                public:

                void show();

                matrix(int a,int b,int c,int d);

 

                matrix() //default constructor

                {

                for (int i= 0;i<2;i++)

                for(int j=0;j<2;j++)

                x[i][j]=0;

                }

 

                friend matrix operator + (matrix a,matrix b);

                };

 

                matrix operator +(matrix a,matrix b) //optr over loading +

                {

                matrix c;

                for (int i= 0;i<2;i++)

                                for(int j=0;j<2;j++)

                                                c.x[i][j]=a.x[i][j] + b.x[i][j];

 

                 return c;

                 }

                 matrix operator -(matrix a,matrix b) //optr over loading +

                {

                matrix c;

                for (int i= 0;i<2;i++)

                                for(int j=0;j<2;j++)

                                                c.x[i][j]=a.x[i][j] - b.x[i][j];

 

                 return c;

                 }

 

                 void matrix ::show()

                 {

                 for (int i= 0;i<2;i++)

                                for(int j=0;j<2;j++)

                                                 cout <<"\n"<

 

                  }

                  matrix:: matrix (int a,int b,int c,int d)//constructor definition

                                   {

 

                                                   x[0][0]=a;

                                                   x[0][1]=b;

                                                   x[1][0]=c;

                                                   x[1][1]=d;

                                   }

 

       void main()

                 {

                  matrix a,b(2,2,2,2),c;

                  a=matrix(1,1,1,1);

                  clrscr();

                  c=a+b;

 

                  c.show();

                  c=a-b;

                  c.show();

                  getch();

                  }

Posted Date: 10/19/2012 2:59:19 AM | Location : United States







Related Discussions:- Operator overloading - c++ program, Assignment Help, Ask Question on Operator overloading - c++ program, Get Answer, Expert's Help, Operator overloading - c++ program Discussions

Write discussion on Operator overloading - c++ program
Your posts are moderated
Related Questions
a program that declares and prompts the user to enter two integer and performs the five arithmetic operations

Differentiate between functions getch () and getche (). - Both functions accept a character input value from user. - When getch () is used, key that was pressed won't appear

Project Description: Not just like that though. I want a similar program that can scan through poker lobbys (pokerstars, full tilt, ongame and ipoker) and seat me to the left of

Define Multi File Program? While writing large programs we must divide programs up into modules. These would be separate source files and the main() function would be in one fi

Although we can handle most I/O routines with getc and putc , there are file versions of fgets, fputs, fprintf and fscanf . The syntax is       fgets(stringname,n,filename);   Th

Get Reverse Engineer to Linux game routine analysis Project Description: I want a fast and efficient reverse engineer with good knowledge of assembly x86 language, strong rev

Program is to check whether the string entered is a palindrome or not: Program is to check whether the string entered is a palindrome or not invoke function from main int p


Ravi is a newbie to the programming and while learning the programming language he came to know the following rules: · Each program must start with ''{'' and end with '

Write a fully modular C program that reads in a string of a defined size from the keyboard and reports how many times each letter of the alphabet occurs within that string (ignorin