Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Normally this is the Car.h file
  6. class Car {
  7. public:
  8. Car();
  9. Car(int hp);
  10. ~Car();
  11. // When the properties cannot be modified inside methods we use const
  12. int getHorsePower() const;
  13. void setHorsePower(int hp);
  14. friend void printHorsePower(Car* myCar);
  15. private:
  16. int horsePower;
  17. int* pointerVariable;
  18. };
  19.  
  20. // Normally this is the Car.cpp file START
  21. // Standard Constructor
  22. Car::Car() : horsePower(180) {
  23. pointerVariable = new int;
  24. *pointerVariable = 0;
  25. std::cout << "Car Class Standard Constructor" << std::endl;
  26. };
  27.  
  28. // Non-Standard Constructor with parameters
  29. Car::Car(int hp) : horsePower(hp) {
  30. pointerVariable = new int;
  31. *pointerVariable = hp;
  32. std::cout << "Car Class Non-Standard Constructor" << std::endl;
  33. };
  34.  
  35. // Destructor
  36. Car::~Car() {
  37. delete pointerVariable;
  38. std::cout << "Car Class Destruktor" << std::endl;
  39. };
  40.  
  41. int Car::getHorsePower() const {
  42. return horsePower;
  43. }
  44.  
  45. void Car::setHorsePower(int hp) {
  46. horsePower = hp;
  47. };
  48. // Normally this is the Car.cpp file END
  49.  
  50. void printHorsePower(Car* myCar) {
  51. cout << "The car has " << myCar->horsePower << endl;
  52. }
  53.  
  54. class Jeep : public Car {
  55. public:
  56. Jeep() {
  57. std::cout << "Jeep Class Standard Constructor" << std::endl;
  58. };
  59. ~Jeep() {
  60. std::cout << "Jeep Class Destruktor" << std::endl;
  61. };
  62. };
  63.  
  64. class Range : public Jeep {
  65. public:
  66. Range() {
  67. std::cout << "Range Class Standard Constructor" << std::endl;
  68. };
  69. ~Range() {
  70. std::cout << "Range Class Destruktor" << std::endl;
  71. };
  72. };
  73.  
  74. int main()
  75. {
  76. std::cout << "START" << std::endl;
  77.  
  78. //// Creating a static object with Standard constructor
  79. //Car mercedes;
  80. //
  81. //// Setting a horse power value of 210 to an object
  82. //mercedes.setHorsePower(210);
  83.  
  84. //// To get the horsePower property from a Car object
  85. //mercedes.getHorsePower();
  86.  
  87. //Car bmw(240);
  88. //bmw.setHorsePower(210);
  89. //bmw.getHorsePower();
  90.  
  91. //// Creating a dynamic object with Standard constructor
  92. //Car* jaguar = new Car();
  93.  
  94. //jaguar->getHorsePower();
  95. //jaguar->setHorsePower(300);
  96.  
  97. //// Creating a dynamic object with Non-Standard constructor
  98. //Car* rolls = new Car(230);
  99.  
  100. //delete jaguar;
  101. //delete rolls;
  102.  
  103. //Range ewok;
  104.  
  105. Car* jaguar = new Car();
  106.  
  107. printHorsePower(jaguar);
  108.  
  109. std::cout << "END" << std::endl;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement