Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Op&&^Overload
//g++ 7.4.0 //operator overloading: exponentiation and inverse exponentiation //this code is created by Rezaul Hoque on September 06,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{ private: double l, b,m; public: Vegetable (){}//default constructor Vegetable (double k){m=k;} Vegetable (double vl, double vb){ l=vl; b=vb;} //Vegetable(double,double ) constructor Vegetable (const Vegetable& v){l=v.l; b=v.b;}//copy constructor Vegetable operator&&(const int & n);//exponentiation operator Vegetable operator^(const int & n);// inverse exponentiation operator Vegetable operator+(const Vegetable &); const Vegetable & operator++(); double area(){return l*b;} double getm() { return m;} double getl(){ return l;} double getb(){return b;} }; Vegetable Vegetable::operator &&(const int& n) { return Vegetable(pow(m,n)); } Vegetable Vegetable:: operator^(const int& n) { return Vegetable(pow(1/m,n)); } 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,veg4; int m=veg1.area(); std::cout<<"veg1 area= "<<m<<"\n"; veg3=veg1+veg2; std::cout<<"\nveg3 length = "<<veg3.getl()<<"\n"; std::cout<<"veg3 breadth = "<<veg3.getb()<<"\n"; Garden::Vegetable p= Garden::Vegetable(2);//this is called veg4 Garden::Vegetable q=p&&3; Garden::Vegetable r= Garden::Vegetable(3);//this is called veg5 Garden::Vegetable o=r^3; std::cout<<"After exponentiation of veg4: "<< q.getm(); std::cout<<"\nInverse exponentiation of veg5: "<<o.getm(); return 0; }
run
|
edit
|
history
|
help
0
Test 7(2020)
designated-inits
YesToDafT
ClassQuiz
MyStringv2
Bin Tree build
list
prime factorization trial division
3 and 7 in a row
325324