Answer to Question #267916 in C++ for chise

Question #267916

Write a C++ program that will prompt the user to enter the detail of a car as shown on




Question 2: Write the class definition for a Car class. Provide the following data members:  A C-string called make of size 20 to store the car manufactures name, e.g. Ford, Toyota, ….;  In integer called year to hold the year of first registration of the vehicle;  A floating point called km to contain the number of kilometers traveled for the trip;  A floating point called liter to contain the liters used to cover the distance;  A floating point called consumption is the calculated value of liter per km.  Class-wide floating point called expense, for the cost per kilometer, which should be initialized to R7.55.




1
Expert's answer
2021-11-18T06:54:58-0500
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Vehicle {
protected:
	Vehicle myVehicle[9];
	string make;  //make
    string model; // model
    string color;  // color
    int	year;  // year
    int mileage;  // miles on car
	string type;  //Type of vehicle

public:
	//Constructor that will set information for a new car
	void New_vehicle (string a, string b, string c, int d, int e) 
	{make = a; model = b; color = c; year = d; mileage = e;}
	
	Vehicle(); //Default constructor
	Vehicle(string, string, string, int, int, string);
	//mutator and accessor functions
	void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);
	void setType(string);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();
	string getType();

	//Check mileage to see if valid
	void valid_mileage(int);

	//virtual function
	virtual void details() {
	}

};
//Sets to default values
Vehicle::Vehicle() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
	type = " ";
}

Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
    Vehicle::make =  make;
    Vehicle::model = model;
    Vehicle::color = color;
    Vehicle::year = year;
    valid_mileage(mileage);
	Vehicle::type = type;
}

void Vehicle::setMake(string make) {
    Vehicle::make = make;
}

void Vehicle::setModel(string model) {
    Vehicle::model = model;
}

void Vehicle::setColor(string color) {
    Vehicle::color = color;
}

void Vehicle::setYear(int year) {
    Vehicle::year = year;
}

void Vehicle::setMileage(int mileage) {
    valid_mileage(mileage);
}

void Vehicle::setType(string type) {
	Vehicle::type = type;
}


string Vehicle::getMake() {
    return make;
}
string Vehicle::getModel() {
    return model;
}
string Vehicle::getColor() {
    return color;
}
int Vehicle::getYear() {
    return year;
}
int Vehicle::getMileage() {
    return mileage;
}

string Vehicle::getType() {
	return type;
}


void Vehicle::valid_mileage(int mileage) {
    if (mileage>=0)
        Vehicle::mileage=mileage;
    else {
        Vehicle::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }

	    Vehicle& getVehicle(int n) {
        return myVehicle[n];
    }
};


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS