Answer to Question #182631 in C++ for Ian

Question #182631

Let’s make a program that calculates the age of your car!

We should ask the user to input the Make, Model, and the Year their car was made.

Our program should create a Car object and then calculate how old that car is by subtracting the year the car was made from the current year. (Example: 2021 - 2001 = 20 years old)

Coding Instructions:

Create a class called Car.

This class should have 3 member variables:

  • a string called Make
  • a string called Model
  • an integer called Year

The class should have a function that returns the age of the car by subtracting the member variable "year" from the current year (2021).

The class should have a constructor that takes 3 parameters: make, model, year.

Example Output:

“Welcome to Historic Car Calculator”

“Please enter the make of your car.”

>>Honda

“Please enter the model of your car.”

>>Civic

“Please enter the year your car was made.”

>>1995

“Your Honda Civic is 26 years old”


1
Expert's answer
2021-04-18T07:50:55-0400
#include <iostream>
#include <string>
 
using namespace std;
 
class Car
{
public:
	Car(string make, string model, int year);
	void Display() { cout << "Your " << make << " " << model << " is " << GetYear() << " years old."; }
 
private:
	string make;
	string model;
	int year;
	int GetYear() { return 2021 - year; }
};
 
Car::Car(string make_, string model_, int year_) : make(make_), model(model_), year(year_)
{
}
 
 
int main()
{
	cout << "Welcome to Historic Car Calculator" << endl;
	cout << "Please enter the make of your car: ";
	string make;
	cin >> make;
	cout << "Please enter the model of your car: ";
	string model;
	cin >> model;
	cout << "Please enter the year your car was made: ";
	int year;
	cin >> year;
	Car car(make, model, year);
	car.Display();
	cout << endl;
	return 0;
}

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