Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
cppPyProperty
//g++ 7.4.0 //cppPyProperty:C++ equivalent of PyProperty program //this code is created by Rezaul Hoque on February 12,2022;contact: jewelmrh@yahoo.com,Dhaka, Bangladesh //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 <string> //PyProperty(created on January 03,2022) program looks like: /*** class Fruit: def __init__(self, fruit, price,qty): self.fruit = fruit self.set_price(price) self.set_qty(qty) def set_price(self, price): if price <= 0: raise ValueError('Price must be positive') self._price = price def set_qty(self,qty): if qty<= 0: raise ValueError('Quantity must be positive') self._qty = qty def get_price(self): return self._price def get_qty(self): return self._qty a = Fruit('Guava',10,1) print(a.fruit) print(a.get_price()) print(a.get_qty()) ***/ //C++ equivalent of above program is: class Fruit{ protected: std::string fruit; int price,qty; public: Fruit(){} Fruit(std::string f){ fruit=f;} Fruit(Fruit & c){ fruit=c.fruit;} ~Fruit(){} void setP(int p){ if (p<=0) std::cout<<"Price must be positive\n"; else price=p;} void setQ(int q){ if (q<=0) std::cout<<"Quantity must be positive.\n"; else qty=q;} virtual std::string getF() const { return fruit;} virtual int getP() const { return price;} virtual int getQ() const { return qty;} }; int main() { Fruit a("Guava"); a.setP(10); a.setQ(1); std::cout<<a.getF()<<"\n"; std::cout<<a.getP()<<"\n"; std::cout<<a.getQ()<<"\n"; return 0; }
run
|
edit
|
history
|
help
0
Shultz_Lab1.CPP
maximum nights you can accommodate
doubly
memcpy
Backpack with recursion
Pointer to class members
Sort an array of 0s, 1s and 2s
1568-1644 Solar eclipses, make 1617(8) and 1620(12) Lunar eclipses the same as Chinese records
PyramidTransitionMatrix_recursive
23