1. Write a constructor for the Car class that will accept two input arguments as
ID: 3718558 • Letter: 1
Question
1. Write a constructor for the Car class that will accept two input arguments as the initial values of the attributes.
2. Write an overloaded constructor for the Car class that will take a single argument for the model of the car, setting the make to "unknown".
3. Write a setter method for the make attribute.
4. Write a getter method for the model attribute.
5. Complete a main method that will accomplish the following steps. (You may assume that the class includes all appropriate setter methods and getter methods for all instance variables.) • Create a car with the make "Ford" and the model "Fusion". • Create a car with the make "Honda" and the model "Civic". • Change the model of the second car from "Civic" to "Accord". • On a separate line, print the make and model values for each car.
Explanation / Answer
Class Car
{
public:
string car_make;
string car_model;
//Answer 1 : constructor of the Car Class taking 2 arguments and setting internal values
void Car(string car_make_, string car_model_)
{
car_make = car_make_;
car_model = car_model_;
}
//Answer 2 : overloaded constructor of the Car Class taking 1 argument and setting car_make values as "unkown"
void Car(string car_model_)
{
car_make = "unknown";
car_model = car_model_;
}
//Answer 3 : constructor of the Car Class taking 2 arguments and setting internal values
void set_car_make(string new_make_data)
{
//Setter function for Make Value
car_make = new_make_data;
}
//Answer 4 : constructor of the Car Class taking 2 arguments and setting internal values
string get_car_model()
{
// Getter Function for Model value
return car_model;
}
};
//Answer 3 : constructor of the Car Class taking 2 arguments and setting internal values
void set_car_make(string new_make_data)
{
//Setter function for Make Value
car_make = new_make_data;
}
//Answer 5 : Main function to perform different operations
void main()
{
// Create a car with the make "Ford" and the model "Fusion".
Car Ford_Fusion_Car_obj = new Car("Ford" , "Fusion");
// Create a car with the make "Honda" and the model "Civic".
Car Honda_Civic_Car_obj = new Car("Honda" , "Civic");
// Change the model of the second car from "Civic" to "Accord".
Honda_Civic_Car_obj.model = "Accord";
// On a separate line, print the make and model values for each car.
cout << " Ford Car Detail: Make :" << Ford_Fusion_Car_obj.car_make << " Model : " << Ford_Fusion_Car_obj.car_model << endl;
cout << " Honda Car Detail: Make :" << Honda_Civic_Car_obj.car_make << " Model : " << Honda_Civic_Car_obj.car_model << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.