Consider the below Car class. Demonstrate the class in a program by defining poi
ID: 3548080 • Letter: C
Question
Consider the below Car class. Demonstrate the class in a program by defining pointer(s) to Car object(s) (i.e. create a Car object, store the address of the Car object in a pointer, and invoke Car methods using operator). Demonstrate the class in a program by creating dynamic Car object(s) (i.e. create a Car object using the new operator, assign its address to a pointer, and invoke Car methods using operator). Note that the dynamic object should be deleted at the end using the delete operator and the pointer should be set to NULL. Demonstrate the class in a program by creating an array of Car objects. Use an array size of at least 4. (Note: Since the Car class does not have a default constructor, you must provide an initializer for each object in the array). Invoke accelerate and brake methods on at least two of the objects.Explanation / Answer
A)
Car obj1(2012,"lamborgini");
Car *obj_ptr;
obj_ptr = &obj1;
obj_ptr->accelerate(); // increase speed by 5
cout<<" Car model"<<obj_ptr->getYearModel();
cout<<" Current speed"<<obj_ptr->getSpeed();
obj_ptr->brake();
B)
Car *obj_ptr;
obj_ptr = new Car(2012,"lamborgini");
obj_ptr->accelerate(); // increase speed by 5
cout<<" Car model"<<obj_ptr->getYearModel();
cout<<" Current speed"<<obj_ptr->getSpeed();
obj_ptr->brake();
delete obj_ptr;
obj_ptr= null;
C)
int main(){
// Creating 4 car objects
Car obj_arr[4] = { Car(2012,"lamborgini") , Car(2011, "Safari") , Car(2013, "GrandI10"), Car(2012, "Hyundai")};
// increasing the speed of safari by 10 and hyundai by 5
obj_arr[1].accelerate(); obj_arr[1].accelerate();
obj_arr[3].accelerate();
// Printing the Speed
cout<<" Speed safari = "<<obj_arr[1].getSpeed();
cout<<" Speed Hyundai = "<<obj_arr[3].getSpeed();
// Applying the Brakes
obj_arr[1].brake();
obj_arr[1].brake();
obj_arr[3].brake();
return 0;
}