Posts

Showing posts with the label oveloading

Constructor overloading in C++

Image
                          Well hello guys, I am back again with a key concept of C++ programming language. This program is all about constructor overloading concept. Even if it is a very simple concept, one should have knowledge on it. Have a look at the concept explained below. Concept:                            Constructor is a special feature in C++ whose task is to initialize objects of its class. It is called constructor because it constructs the values of data members of the class. Constructors carry the same name of the class. When we initialize any object of the class, then we can pass values through the constructor during initialization only. Constructor overloading is that thing when in one class we use constructors with different no. of parameters. i.e. the when creating objects of the class, the number of arguments it will have, accor...

Operator Overloading in C++

Image
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 ! 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 he...