I Really Need Help (Car Class)

Exercise – Vehicle Class:
Create a class Vehicle with the following private attributes:
 Brand Name: should be a character array of size 20.
 Transmission Type: should be a Boolean attribute.
 Fuel Consumption: should be a floating-point attribute.
 Weight: should be an integer attribute.
 Color: should be a string attribute.
 Number of Modifications: should be an integer attribute.
The class Vehicle should contain the following behaviors:
 A constructor with no arguments should initialize the attributes to empty values.
 Set functions for each attribute except for Number of Modifications.
 A set all function that calls the attributes’ set functions.
 Get functions for each attribute except for Number of Modifications.
 A destructor should print the brand name plus the phrase “has been removed.”
 A print function to display the values of the object’s attributes.
 A utility function that increments the value of Number of Modifications by 1, and it should be called each time a set function is used. When the user calls the set all function, the attribute Number of Modifications should not be incremented. Number of Modifications can only be incremented if the attribute’s set function is called directly by the user and the new value should be different from the old value.
A header file and source file must be created for class Vehicle.
Another source file must be created for the main() function:
 Print on the screen the sentence “Creating Two Objects:”.
 Two objects must be created for class Vehicle, with the addition of a reference for the 1st object and a pointer to the 2nd object.
 Call the set all function to place values for the objects.
 Call the print function to display the values on the screen.
 Declare and initialize a random variable between 0 (1st object) and 1 (2nd object).
 Choose a number between 1 and 5 to customize an attribute.
 Based on the chosen random number, display the brand name and make the change of the attribute for that object using the reference if the 1st object is chosen or pointer if the 2nd object is chosen.
 Re-initialize the value of the random variable.
 Choose again a number between 1 and 5 to customize an attribute.
 Based on the chosen random number, display the brand name and make the change of the attribute for that object using the reference if the 1st object is chosen or pointer if the 2nd object is chosen.
 Keep on repeating the same process until the user chooses -1 to end.
 Print the values of the two objects on the screen.
 Print on the screen the sentence “Destroying Two Objects:”
Last edited on
#ifndef Vehicle_H
#define Vehicle_H

class Vehicle
{
private:

char BrandName[20];
bool TransmissionType;
float FuelConsumption;
int weight;
string color;
int NumberOfModifications;

public:
Vehicle();//constructor
//set functions
void setAttributes(char,bool,float,string,int);
void setBrandName(char);
void setTransmissionType(bool);
void setFuelConsumption(float);
void setweight(int);
void setcolor(string);
//Get functions
char getBrandNaem();
bool getTransmissionType();
float getFuelConsumption();
int getweight();
string getcolor();
void print();
//destructor
~Vehicle();
}
#endif



[b]Here's What Ive Been Up To I dont know how to continue the code any help[/b]
Here is that code. The only changes are that I put it in code tags and added whitespace.
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
#ifndef Vehicle_H
#define Vehicle_H

class Vehicle
{
private:

    char BrandName[20];
    bool TransmissionType;
    float FuelConsumption;
    int weight;
    string color;
    int NumberOfModifications;

public:
    Vehicle();//constructor

    //set functions
    void setAttributes(char,bool,float,string,int);
    void setBrandName(char);
    void setTransmissionType(bool);
    void setFuelConsumption(float);
    void setweight(int);
    void setcolor(string);

    //Get functions
    char getBrandNaem();
    bool getTransmissionType();
    float getFuelConsumption();
    int getweight();
    string getcolor();
    void print();

    //destructor
    ~Vehicle();
}
#endif 
Looking pretty good so far. A few comments, though.

First, class declarations end with }; not just } so update line 36 accordingly!

It looks like you want to use a string variable for 'color'. That's fine, but I would put
#include <string> at the very top of the file. Also, on lines 12, 19, 24, and 31 I would say std::string instead of just string. You could also say using namespace std; above your class declaration instead, but I prefer to qualify the types individually. If you don't do this, the compiler is not going to know what string is when you build your code.

I don't see a declaration for
A utility function that increments the value of Number of Modifications by 1, and it should be called each time a set function is used. When the user calls the set all function, the attribute Number of Modifications should not be incremented. Number of Modifications can only be incremented if the attribute’s set function is called directly by the user and the new value should be different from the old value.

I'd stick this under the private: section of your class since it is only going to be called internally (i.e. only by your other class functions, never directly from main()) And you'll probably want to pass in some indicator of whether or not it was called from an individual set or the set all method.
void incrementModificationCounter(bool calledFromSetAll);
Last edited on
To get you started, you have Vehicle.h already. Here's how Vehicle.cpp is going to shape out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Vehicle.h"

// remember to qualify the method name with Vehicle::
// otherwise the compiler won't know where to look for setFuelConsumption
void Vehicle::setFuelConsumption(float milesPerGallon)
{
    //the name FuelConsumption is the variable in the class
    //milesPerGallon is something that main() will give when it calls the function
    FuelConsumption = milesPerGallon;
    
    //don't forget to call this guy in your sets
    //uncomment the next line when you implement this method
    //incrementNumModifications(false);
}

void Vehicle::setTransmissionType(bool isAutomatic)
{
    //....
}

//...and so on for all your methods, including constructor, destructor, and utility method!!! 


EDIT: The more I think about what that utility function is supposed to do, the more confused I get. The way it is now, you'd have to pass 2 parameters into each 'set' function. The first would be the variable's new value, the second would be some boolean indicator as to whether or not it is being called by 'setAll'. I guess you could always increment inside each individual 'set', and if you call it from 'setAll' just decrement the modifications counter directly (since setAll is part of the class, it can manipulate the counter directly) after each call.
Last edited on
10x mate you're amazing
Can you please put the MAIN and The source file here also ??! I want the whole program also ! Thank you
I want the whole program also !

So does your instructor. ;) It is board policy not to give away complete solutions to homework problems, a policy with which I agree.

One more nudge in the right direction... It looks like we'll have 3 files. Vehicle.h, Vehicle.cpp, and main.cpp. In this thread we already have a class header file and the makings of a class source file. For the main source file, this is all the head start I feel comfortable giving.
1
2
3
4
5
6
7
8
//with this include here, we can now (from main) access the 
//public methods and fields that we declared in the Vehicle class.
#include "Vehicle.h"

int main()
{
    return 0;
}

If you make the corrections to the .h file I addressed earlier, then COPY WORD FOR WORD the Vehicle.cpp code I gave (comments and everything, even with only one method defined) and then drop this snippet of main into main.cpp, you should have a program that will already compile! It is up to you to fill in these files and give the program its intended behavior.
Topic archived. No new replies allowed.