fork download
  1. // Attached: HW_9f-2
  2. // ===========================================================
  3. // File: HW_9f-2
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12.  
  13. class Car
  14. {
  15. private:
  16. string model;
  17. int year;
  18.  
  19. static int carCount;
  20.  
  21. public:
  22. Car()
  23. {
  24. model = "";
  25. year = 0;
  26. carCount++;
  27. }
  28.  
  29. Car(string carModel, int carYear)
  30. {
  31. model = carModel;
  32. year = carYear;
  33. carCount++;
  34. }
  35.  
  36. ~Car()
  37. {
  38. }
  39.  
  40. void setCar(string carModel, int carYear)
  41. {
  42. model = carModel;
  43. year = carYear;
  44. }
  45.  
  46. int getCount()
  47. {
  48. return carCount;
  49. }
  50.  
  51. void displayCar()
  52. {
  53. cout << "Model: " << model << endl;
  54. cout << "Year: " << year << endl;
  55. }
  56.  
  57. friend bool areSame(Car firstCar, Car secondCar);
  58. };
  59.  
  60. int Car::carCount = 0;
  61.  
  62. bool areSame(Car firstCar, Car secondCar)
  63. {
  64. return (firstCar.model == secondCar.model &&
  65. firstCar.year == secondCar.year);
  66. }
  67.  
  68. int main()
  69. {
  70. Car myCar;
  71. Car yourCar("Toyota", 2007);
  72.  
  73. cout << "My Car" << endl;
  74. myCar.displayCar();
  75. cout << endl;
  76.  
  77. cout << "Your Car" << endl;
  78. yourCar.displayCar();
  79. cout << endl;
  80.  
  81. myCar.setCar("Ford", 2002);
  82.  
  83. cout << "My Car" << endl;
  84. myCar.displayCar();
  85. cout << endl;
  86.  
  87. if (areSame(myCar, yourCar))
  88. cout << "The two cars are the same model and year." << endl;
  89. else
  90. cout << "The two cars are not the same model and year." << endl;
  91.  
  92. cout << endl;
  93. cout << myCar.getCount() << " cars have been declared." << endl;
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
My Car
Model:  
Year:   0

Your Car
Model:  Toyota
Year:   2007

My Car
Model:  Ford
Year:   2002

The two cars are not the same model and year.

2 cars have been declared.