Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
OperatorOverload2
//g++ 7.4.0 //Operator Overloading: increment and addition operator //this code is created by Rezaul Hoque on September 03,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <cmath> namespace Garden{ class Vegetable{ public: int l, b,m; Vegetable (){}//default constructor Vegetable (int k){ m=k;} Vegetable (int vl,int vb){ l=vl; b=vb;} //Vegetable(int,int ) constructor Vegetable (const Vegetable& v){l=v.l; b=v.b;}//copy constructor Vegetable operator+(const Vegetable &); const Vegetable & operator++(); int area(){return l*b;} }; Vegetable Vegetable:: operator+(const Vegetable & add) { return Vegetable (l+add.l,b+add.b); } const Vegetable & Vegetable:: operator++() { ++l; ++b; return *this; } } int main() { Garden::Vegetable veg1(2,4),veg2(3,5),veg3; int m=veg1.area(); std::cout<<"\nveg1 area= "<<m<<"\n"; ++veg1;//increment operator is called; now veg1(3,5) veg3=veg1+veg2;//addition operator is called std::cout<<"\nveg3 length = "<<veg3.l<<"\n"; std::cout<<"veg3 breadth = "<<veg3.b<<"\n"; return 0; }
run
|
edit
|
history
|
help
0
BinTree playground
Temp
1234
References Pt 1 C++
Day3
no_error
Funny Writings
abraham
Hi
Kth smallest element