Operator Overloading in C++
This is a simple program in c++ in which the operators will be overloaded. In this program add,sub mul and div operator is overloaded !
The Program:
Any Suggestions ?
Put them here !
Screenshots:
The Program:
#include<iostream> using namespace std; class mat { public: int a; mat(int val) { a=val; } mat() { } mat operator +(mat &b) { return mat(a+b.a); } mat operator -(mat &b) { return mat(a-b.a); } mat operator /(mat &b) { return mat(a/b.a); } mat operator *(mat &b) { return mat(a*b.a); } void disp() { cout<<a<<endl; } }; class inc { public: int x; inc (int val) { x=val; } friend inc operator ++(inc &,int); void disp() { cout<<x<<endl; } }; inc operator ++(inc &a,int) { return inc(a.x+1); } int main() { mat a1(4),a2(6),a3; a3=a2+a1; a3.disp(); a3=a2-a1; a3.disp(); a3=a2*a1; a3.disp(); a3=a2/a1; a3.disp(); inc i1(5); i1++; i1.disp(); }
Any Suggestions ?
Put them here !
Comments
Post a Comment