Question:
easy c++ question. help 10 points fast?
Don Won
2010-12-07 19:35:07 UTC
how can i put the first part of this in a #include file ????????????????


#include
#include
#include

using namespace std;



class Car
{
private:
int yearModel;
string make;
int speed;
public:
Car(int , string , int); // constructor
void setYearModel(int);
void setMake(string);
void setSpeed(int);
int getYearModel() const;
string getMake() const;
int getSpeed() const;
int accelerate();
int brake();

};

Car::Car(int yr, string mk, int sp )
{
yearModel = yr;
make = mk;
speed = sp;
}

void Car::setYearModel(int m)
{
yearModel = m;
}

void Car::setMake(string mk)
{
make = mk;
}
void Car::setSpeed(int sp)
{
speed = sp;
}

int Car::getYearModel() const
{
return yearModel;
}

string Car::getMake() const
{
return make;
}

int Car::getSpeed() const
{
return speed;
}

int Car::accelerate()
{
speed += 5;
return speed;
}

int Car::brake()
{
speed -= 5;
return speed;
}




int main()
{
string carMake;
int carSpeed = 0;
int caryearModel;

// Get the car make
cout << "Enter the make of the car: ";
cin >> carMake;

// Get the year of the car
cout << endl << "Enter the year of the car: ";
cin >> caryearModel;
cout << endl;

Car frame( caryearModel,carMake, carSpeed);

// Increase the car speed
cout << "INCREASING:" << endl;
for(int i = 0; i < 5; i++)
{
cout << "The speed is now " << frame.accelerate() << endl;
}

// Decrease the car speed
cout << endl << endl << "DECREASING:" << endl;
for(int i = 0; i < 5; i++)
{
cout << "The speed is now " << frame.brake() << endl;
}

// display the car speed, make and year model

cout << setprecision(2) << fixed;
cout << endl << "The make of the car is " << frame.getMake();
cout << endl << "The year of the model is " << frame.getYearModel();
cout << endl << "The speed of the car is " << frame.getSpeed() << endl << endl;

system("pause");
return 0;
}
Six answers:
Robc
2010-12-07 20:02:05 UTC
In file car.h place the following:



#ifndef CAR_H

#define CAR_H



#include



class Car

{

private:

int yearModel;

string make;

int speed;

public:

Car(int , string , int); // constructor

void setYearModel(int);

void setMake(string);

void setSpeed(int);

int getYearModel() const;

string getMake() const;

int getSpeed() const;

int accelerate();

int brake();

};

#endif



In file car.cpp place the following:



#include "car.h"

followed by your Car class functions.



You would then compile car.cpp into an object file



In control.cpp place your include statements followed by:

#include "car.h" // note use quotes not <>



int main()

{

...

}



You will need to compile each .cpp file and link them together.

If you are using an IDE, you would create a project and it will compile and link the files when you do a build.

If you are not using an IDE, you would need to create a make file. See web site below for information on creatine a make file.
SomeGuy
2010-12-07 20:39:33 UTC
You create something what is know as a header file which is a .h type file.



Now in your header file you only put the contents of the class in there. You do not declare any values or else you'll get an error , so an example would be like this



// Car.h



class Car

{

private:

int yearModel;

string make;

int speed;

public:

Car(int , string , int); // constructor

void setYearModel(int);

void setMake(string);

void setSpeed(int);

int getYearModel() const;

string getMake() const;

int getSpeed() const;

int accelerate();

int brake();



};



// End of Car.h





Then that would allow you to use #include to get the contents of the file



so normally you would create another file called Car.cpp

In this .cpp file you'd just declare what the stuff in your .h file does



Ex:



// Car.cpp





Car::Car(int yr, string mk, int sp )

{

yearModel = yr;

make = mk;

speed = sp;

}



void Car::setYearModel(int m)

{

yearModel = m;

}



void Car::setMake(string mk)

{

make = mk;

}

void Car::setSpeed(int sp)

{

speed = sp;

}



int Car::getYearModel() const

{

return yearModel;

}



string Car::getMake() const

{

return make;

}



int Car::getSpeed() const

{

return speed;

}



int Car::accelerate()

{

speed += 5;

return speed;

}



int Car::brake()

{

speed -= 5;

return speed;

}



and your main funciton would stay the same



Hope this Helped :D
?
2010-12-07 19:55:14 UTC
Create a new header file (.h) or create a new source file and save it as "car.h" then cut and paste all of the first part into that header file. In the .cpp file you will have to include car.h like this: #include "car.h"

Hope this helps :)

Goodluck!
?
2016-10-20 05:13:33 UTC
I study them yet there too no longer basic and all i could desire to grant you have been guesses. Sorry I havent taken physics yet im taking that my senior 12 months! yet i do think of A makes extra experience than B on question 3
modulo_function
2010-12-07 20:01:03 UTC
put it in a header file named something like: carheader.h



and include it with



#include "carheader.h"
2010-12-07 19:38:26 UTC
You can't. Sorry, you will have to try something different.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...