Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Product {
  6. protected: //can be used in inherited classes
  7. string name;
  8. float price;
  9. bool isKidsSafe;
  10. public:
  11. Product() {
  12. name = "";
  13. price = 0;
  14. isKidsSafe = false;
  15. //cout << endl << "Product constructor - job done"<<endl;
  16. }
  17. Product(string name, float price = 0, bool isSafe = false) { //if we write "=false" we dont have to pass it when creating an object if we want to declare it false
  18. this->price = price;
  19. this->name = name;
  20. this->isKidsSafe = isSafe;
  21. }
  22. string getName() {
  23. return this->name;
  24. }
  25. string getDescription() {
  26. string description = "The product name is " + this->name + "\n";
  27. description += ("The price is " + to_string(this->price) + "\n");
  28. if (this->isKidsSafe) {
  29. description += "Is kids safe \n";
  30. }
  31. else {
  32. description += "Don't give it to children under 3 years old \n";
  33. }
  34. return description;
  35. }
  36.  
  37. float getPrice() {
  38. return this->price;
  39. }
  40.  
  41. ~Product() {
  42. cout << endl << "Product destructor";
  43. }
  44. };
  45.  
  46. //inheritance
  47. class DiscountedProduct: public Product {
  48. private:
  49. float discountPercentage;
  50. int maxItems;
  51. public:
  52. DiscountedProduct(string name = "", float price = 0.0, float discount = 0) : Product(name,price) {
  53. //cout << "DiscountedProduct constructor - starting"<<endl;
  54. this->discountPercentage = discount;
  55. this->maxItems = 0;
  56. /*this->name = name;
  57. this->price = price;
  58. this->isKidsSafe = false;*/
  59. }
  60.  
  61. //overriding the base class method
  62. string getDescription() {
  63.  
  64.  
  65. string description = this->Product::getDescription();
  66.  
  67. description += "The discount is " + to_string(this->discountPercentage) + "%\n";
  68. description += "The maximum items is " + to_string(this->maxItems) + "\n";
  69. return description;
  70. }
  71.  
  72. ~DiscountedProduct() {
  73. cout << endl << "DiscountedProduct destructor";
  74. }
  75.  
  76. };
  77.  
  78.  
  79. int main() {
  80. Product product;
  81. Product toy1("Remote Controlled Car", 230);
  82. Product toy2("Doll", 34);
  83. Product toy3("Big Ball", 12, true);
  84.  
  85. cout << toy1.getDescription()<<endl;
  86. cout << toy2.getDescription()<<endl;
  87. cout << toy3.getDescription()<<endl;
  88.  
  89. DiscountedProduct promotion_1;
  90. cout<<promotion_1.getDescription()<<endl;
  91.  
  92. DiscountedProduct promotion_2("Lego",100,50);
  93. cout << promotion_2.getDescription();
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement