Guest User

Untitled

a guest
Feb 17th, 2011
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /////////////////racecar.cpp///////////////
  2. #include <iostream>
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include <cassert>
  7. #include <cstring>
  8. #include "racecar.h"
  9.  
  10. racecar::racecar( const char *n, const char *c, const char *s )
  11. /* write code to call base-class constructor */
  12. : car(n, c)
  13. {
  14. /* write code to allocate memory for the sponsor, s, and
  15. copy it into private data member sponsor */
  16. sponsor = s;
  17. }
  18.  
  19. racecar::~racecar()
  20. {
  21. delete [] sponsor;
  22. }
  23.  
  24. void racecar::setGearbox( int gears )
  25. {
  26. gearbox = ( ( gears <= 10 && gears >= 0 ) ? gears : 6 );
  27. }
  28.  
  29. void racecar::useParachute( bool p )
  30. {
  31. parachuteDeployed = p;
  32. }
  33.  
  34. void racecar::print() const
  35. {
  36. /* call base class method print here */
  37. car::print();
  38.  
  39. cout << car::name << " also has " << gearbox
  40. << " gears and is sponsored by " << sponsor << ". " << endl;
  41.  
  42. if ( this->parachuteDeployed)
  43. cout << car::name << " has used its parachute." << endl;
  44. else
  45. cout << car::name << " has not used its parachute." << endl;
  46. }
  47.  
  48. //////////////////racecar.h/////////////////////
  49. #ifndef RACECAR_H
  50. #define RACECAR_H
  51.  
  52. #include "car.h"
  53.  
  54. /* write class header for racecar, which inherits publicly
  55. from car */
  56. class racecar : public car
  57. {
  58. public:
  59. racecar( const char *, const char *, const char * );
  60. ~racecar();
  61. void setGearbox( int );
  62. void useParachute( bool );
  63. void print() const;
  64.  
  65. private:
  66. int gearbox; // the number of gears in a car (i.e., 5-speed)
  67. char *sponsor;
  68. bool parachuteDeployed;
  69. };
  70.  
  71. #endif
  72.  
  73. //////////////////////car.cpp///////////////////////
  74.  
  75. #include <iostream>
  76. using std::cout;
  77. using std::endl;
  78.  
  79. #include <cassert>
  80. #include <cstring>
  81. #include "car.h"
  82.  
  83. /* write the constructor for car, which takes the car’s name and
  84. color and assigns them to private data members name and color;
  85. initialize maxSpeed to 95 and engineValves to 4 */
  86.  
  87. car::car( const char *name, const char *color )
  88. {
  89. name = name;
  90. color = color;
  91. maxSpeed = 95;
  92. engineValves = 4;
  93. }
  94.  
  95. car::~car()
  96. {
  97. delete [] name;
  98. delete [] color;
  99. }
  100.  
  101. void car::setMaxSpeed( int s )
  102. {
  103. maxSpeed = ( ( s >= 0 && s < 250 ) ? s : 95 );
  104. }
  105.  
  106. void car::setEngineValves( int v )
  107. {
  108. engineValves = ( ( v >= 0 && v < 50 ) ? v : 4 );
  109. }
  110.  
  111. void car::print() const
  112. {
  113. cout << "car: " << name << " is " << color << " and has a "
  114. << engineValves << "-valve engine. MAX SPEED = "
  115. << maxSpeed << " mph. " << endl;
  116. }
  117.  
  118. //////////////////car.h/////////////////////////
  119. #ifndef CAR_H
  120. #define CAR_H
  121.  
  122. class car
  123. {
  124. public:
  125. car( const char *name, const char *color );
  126. ~car();
  127. void setMaxSpeed( int );
  128. void setEngineValves( int );
  129. void print() const;
  130.  
  131. protected:
  132. int maxSpeed;
  133. int engineValves;
  134. char *color;
  135. char *name;
  136. };
  137.  
  138. #endif
  139.  
  140. ////////////////////Main.cpp////////////////////////////////
  141.  
  142. #include <iostream>
  143. using std::cout;
  144. using std::endl;
  145.  
  146. #include "car.h"
  147. #include "racecar.h"
  148.  
  149. int main()
  150. {
  151. car chevy( "Chevrolette", "black" );
  152. cout << "chevy: \n";
  153.  
  154. /* write code to print car object */
  155. chevy.print();
  156.  
  157. racecar f1( "Ferrari", "red", "Deitel" );
  158. f1.setEngineValves( 40 );
  159. f1.setMaxSpeed( 220 );
  160. f1.setGearbox( 7 );
  161. f1.useParachute( true );
  162.  
  163. cout << "\n\nf1: \n";
  164. f1.print();
  165.  
  166. system("pause");
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment