Question

In: Computer Science

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set...

Given main(), complete the Car class (in files Car.h and Car.cpp) with member functions to set and get the purchase price of a car (SetPurchasePrice(), GetPurchasePrice()), and to output the car's information (PrintInfo()).

Ex: If the input is:

2011
18000
2018

where 2011 is the car's model year, 18000 is the purchase price, and 2018 is the current year, the output is:

Car's information:
   Model year: 2011
   Purchase price: 18000
   Current value: 5770

Note: printInfo() should use three spaces for indentation.

___________________________________________________

given starting code:

__________________________________________________

Main.cpp

#include <iostream>
#include "Car.h"
using namespace std;

int main(int argc, const char* argv[]) {
int userYear;
int userPrice;
int userCurrentYear;
Car myCar;
  
cin >> userYear;
cin >> userPrice;
cin >> userCurrentYear;
  
myCar.SetModelYear(userYear);
myCar.SetPurchasePrice(userPrice);
myCar.CalcCurrentValue(userCurrentYear);
  
myCar.PrintInfo();

return 0;
}

_________________________________________________

Car.cpp

#include <iostream>
#include <math.h>
#include "Car.h"
using namespace std;

void Car::SetModelYear(int userYear){
modelYear = userYear;
}

int Car::GetModelYear() const {
return modelYear;
}

// TODO: Implement SetPurchasePrice() function

// TODO: Implement GetPurchasePrice() function

void Car::CalcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
  
// Car depreciation formula
currentValue = (int)
round(purchasePrice * pow((1 - depreciationRate), carAge));
}

// TODO: Implement PrintInfo() function to output modelYear, purchasePrice, and
// currentValue

________________________________________________________________

Car.h

#ifndef CARH
#define CARH

class Car {
private:
int modelYear;
// TODO: Declare purchasePrice member (int)
int currentValue;

public:
void SetModelYear(int userYear);

int GetModelYear() const;

// TODO: Declare SetPurchasePrice() function

// TODO: Declare GetPurchasePrice() function

void CalcCurrentValue(int currentYear);

// TODO: Declare PrintInfo() method to output modelYear, purchasePrice, and
// currentValue

};

#endif

Solutions

Expert Solution

// Please comment below, if you have any doubt

// If the answer is helpful, please give a like :-)

//********************************************************************************

1) Main.cpp

#include <iostream>
#include "Car.h"
using namespace std;

int main(int argc, const char* argv[]) {
int userYear;
int userPrice;
int userCurrentYear;
Car myCar;
  
cin >> userYear;
cin >> userPrice;
cin >> userCurrentYear;
  
myCar.SetModelYear(userYear);
myCar.SetPurchasePrice(userPrice);
myCar.CalcCurrentValue(userCurrentYear);
  
myCar.PrintInfo();

return 0;
}

// ********************************************************************************

2) Car.cpp

#include <iostream>
#include <math.h>
#include "Car.h"
using namespace std;

void Car::SetModelYear(int userYear){
modelYear = userYear;
}

int Car::GetModelYear() const{
return modelYear;
}

void Car::SetPurchasePrice(int boughtPrice){
purchasePrice = boughtPrice;
}

int Car::GetPurchasePrice() const{
return purchasePrice;
}

void Car::CalcCurrentValue(int currentYear) {
double depreciationRate = 0.15;
int carAge = currentYear - modelYear;
// Car depreciation formula
currentValue = (int)round(purchasePrice * pow((1 - depreciationRate), carAge));
}

Car::PrintInfo() const{
cout << "Car's information:" << endl;
cout << " Model year: " << modelYear <<endl;
cout << " Purchase price: " << purchasePrice <<endl;
cout << " Current value: " << currentValue <<endl;
}

//*********************************************************************

3) Car.h

#ifndef CARH
#define CARH

class Car {
private:
int modelYear;
int purchasePrice;
int currentValue;

public:
void SetModelYear(int userYear);

int GetModelYear() const;

void SetPurchasePrice(int boughtPrice);

int GetPurchasePrice() const;

void CalcCurrentValue(int currentYear);

void PrintInfo() const;

};

#endif


Related Solutions

How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp)...
7.26 LAB: Nutritional information (classes/constructors) Given main(), complete the FoodItem class (in files FoodItem.h and FoodItem.cpp) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value. Ex: If the input is: M&M's 10.0 34.0 2.0 1.0 where M&M's...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp)...
7.27 LAB: Artwork label (classes/constructors) Given main(), complete the Artist class (in files Artist.h and Artist.cpp) with constructors to initialize an artist's information, get member functions, and a PrintInfo() member function. The default constructor should initialize the artist's name to "None" and the years of birth and death to 0. PrintInfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Complete the Artwork class (in files Artwork.h and Artwork.cpp) with constructors...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
functions loadMNISTImages and loadMNISTLabels to read in the training/testing set and label files Function loadMNISTImages will...
functions loadMNISTImages and loadMNISTLabels to read in the training/testing set and label files Function loadMNISTImages will return an array of 784 rows, each column contains pixel values of an image. Use the following code to convert labels into a 10-row array, each column represents one digit: labels = loadMNISTLabels('training_label'); % initialize figure labels = labels'; labels(labels==0)=10; labels=dummyvar(labels); .
What is the advantage of defining the member functions outside a class rather than defining inside...
What is the advantage of defining the member functions outside a class rather than defining inside a class? How the following are achieved in C++? a) static polymorphism. b) dynamic polymorphism What will happen if you forget to implement the pure virtual function in the derived class? How a function templates differs from function overloading?
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT