Feb 19, 2013 at 12:06am Feb 19, 2013 at 12:06am UTC
Edit : Nevermind everything works except the parachute I get the same statement printed even if I change it to false in the main.cpp.
I am using a class car and also another class racecar which inherits from car.
Everything seems to work fine except the race car print function. The program compiles but when the racecar print function is suppose to print the program freezes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// driver for race car and car
#include <iostream>
using std::cout;
using std::endl;
#include "Car.h"
#include "Racecar.h"
int main()
{
Car chevy( "Chevrolet" , "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" );
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
//Car class header
#ifndef Car_H
#define Car_H
#include <iostream>
using namespace std;
class Car
{
public :
Car(string name,string color, int max_speed = 95, int valves = 4);
void print();
void setEngineValves(int v);
void setMaxSpeed(int s);
private :
string color;
int max_speed, valves;
protected :
string name;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//Car class cpp
#include <iostream>
#include "Car.h"
using namespace std;
Car::Car(string n,string c, int m, int v)
:name(n),color(c),max_speed(m),valves(v){}
void Car::print()
{
cout << "Car: " << name << " is " << color << " and has a " <<
valves << "-valve engine. MAX SPEED = " << max_speed <<" mph" << endl;
}
void Car::setEngineValves(int v)
{
valves = v;
}
void Car::setMaxSpeed(int s)
{
max_speed = s;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#ifndef Racecar_H
#define Racecar_H
#include <iostream>
//racecar header
#include "Car.h"
using namespace std;
class Racecar : public Car
{
public :
Racecar(string name,string color,string sponser);
void setGearbox(int g);
void useParachute(bool s);
void print();
private :
int gears;
string sponser;
bool parachute;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
//Race car cpp
#include <iostream>
#include "Car.h"
#include "Racecar.h"
using namespace std;
Racecar::Racecar(string name,string color,string sponser)
:Car(name,color), sponser(sponser){}
void Racecar::print()
{
Car::print();
cout << name << " also has " << gears <<" gears and is sponsored by " << sponser << endl;
if (parachute)
cout << name << " has used its parachute" << endl;
else
cout << name << " has not used its parachute" << endl;
}
void Racecar::setGearbox(int g)
{
gears = g;
}
void Racecar::useParachute(bool p)
{
if (p = 1)
parachute = 1;
else
parachute = 0;
}
Last edited on Feb 19, 2013 at 12:17am Feb 19, 2013 at 12:17am UTC
Feb 19, 2013 at 12:16am Feb 19, 2013 at 12:16am UTC
Never mind it does compile and everything works except the parachute.
Am I using bool correct in both the parachute function and the print function?
Feb 19, 2013 at 12:17am Feb 19, 2013 at 12:17am UTC
if (p = 1)
This should be == (comparison) not = (assignment).
Feb 19, 2013 at 12:25am Feb 19, 2013 at 12:25am UTC
Oh right Thank you so much. Solved.