Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////racecar.cpp///////////////
- #include <iostream>
- using std::cout;
- using std::endl;
- #include <cassert>
- #include <cstring>
- #include "racecar.h"
- racecar::racecar( const char *n, const char *c, const char *s )
- /* write code to call base-class constructor */
- : car(n, c)
- {
- /* write code to allocate memory for the sponsor, s, and
- copy it into private data member sponsor */
- sponsor = s;
- }
- racecar::~racecar()
- {
- delete [] sponsor;
- }
- void racecar::setGearbox( int gears )
- {
- gearbox = ( ( gears <= 10 && gears >= 0 ) ? gears : 6 );
- }
- void racecar::useParachute( bool p )
- {
- parachuteDeployed = p;
- }
- void racecar::print() const
- {
- /* call base class method print here */
- car::print();
- cout << car::name << " also has " << gearbox
- << " gears and is sponsored by " << sponsor << ". " << endl;
- if ( this->parachuteDeployed)
- cout << car::name << " has used its parachute." << endl;
- else
- cout << car::name << " has not used its parachute." << endl;
- }
- //////////////////racecar.h/////////////////////
- #ifndef RACECAR_H
- #define RACECAR_H
- #include "car.h"
- /* write class header for racecar, which inherits publicly
- from car */
- class racecar : public car
- {
- public:
- racecar( const char *, const char *, const char * );
- ~racecar();
- void setGearbox( int );
- void useParachute( bool );
- void print() const;
- private:
- int gearbox; // the number of gears in a car (i.e., 5-speed)
- char *sponsor;
- bool parachuteDeployed;
- };
- #endif
- //////////////////////car.cpp///////////////////////
- #include <iostream>
- using std::cout;
- using std::endl;
- #include <cassert>
- #include <cstring>
- #include "car.h"
- /* write the constructor for car, which takes the car’s name and
- color and assigns them to private data members name and color;
- initialize maxSpeed to 95 and engineValves to 4 */
- car::car( const char *name, const char *color )
- {
- name = name;
- color = color;
- maxSpeed = 95;
- engineValves = 4;
- }
- car::~car()
- {
- delete [] name;
- delete [] color;
- }
- void car::setMaxSpeed( int s )
- {
- maxSpeed = ( ( s >= 0 && s < 250 ) ? s : 95 );
- }
- void car::setEngineValves( int v )
- {
- engineValves = ( ( v >= 0 && v < 50 ) ? v : 4 );
- }
- void car::print() const
- {
- cout << "car: " << name << " is " << color << " and has a "
- << engineValves << "-valve engine. MAX SPEED = "
- << maxSpeed << " mph. " << endl;
- }
- //////////////////car.h/////////////////////////
- #ifndef CAR_H
- #define CAR_H
- class car
- {
- public:
- car( const char *name, const char *color );
- ~car();
- void setMaxSpeed( int );
- void setEngineValves( int );
- void print() const;
- protected:
- int maxSpeed;
- int engineValves;
- char *color;
- char *name;
- };
- #endif
- ////////////////////Main.cpp////////////////////////////////
- #include <iostream>
- using std::cout;
- using std::endl;
- #include "car.h"
- #include "racecar.h"
- int main()
- {
- car chevy( "Chevrolette", "black" );
- cout << "chevy: \n";
- /* write code to print car object */
- chevy.print();
- racecar f1( "Ferrari", "red", "Deitel" );
- f1.setEngineValves( 40 );
- f1.setMaxSpeed( 220 );
- f1.setGearbox( 7 );
- f1.useParachute( true );
- cout << "\n\nf1: \n";
- f1.print();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment