Kat H.

asked • 07/19/21

C++: Create a Dynamic Car Class

You will be using your previous version of the Car class and modifying it to have dynamic memory.


previous code:


carmain3.cpp:


#include <iostream>

#include <cstring>

#include "car.h"


using namespace std;


int main()

{

char make[MAX_STR] = "Honda";

char model[MAX_STR] = "Civic";

Car car1(make, model, 2017);

Car car2;

   

  cout << "print car1 " << endl;

  car1.print();

  cout << "print car2 " << endl;

  car2.print();

   

  cout << "Now doing car2 = car1 " << endl;

  car2 = car1;

   

  cout << "print car1 " << endl;

  car1.print();

  cout << "print car2 " << endl;

  car2.print();

   

  cout << "change car2's model from Honda to Ford" << endl;

  char make2[MAX_STR] = "Ford";

  car2.setMake(make2);

   

  cout << "re-print car1" << endl;

  car1.print();

  cout << "re-print car2 (this should print a different make than car1)" << endl;

  car2.print();


  

  return 0;  

 }

   

 car.cpp:


#include "car.h"

Car::Car(const char* make, const char* model , int year){

  this->make = strcpy((char *)malloc(strlen(make) + 1),make);

  this->model = strcpy((char *)malloc(strlen(model) + 1),model);

  this->year = year;

}

Car::Car(){

  this->make = strcpy((char*)malloc(1 + strlen("<no value>")),"<no value>");

  this->model = strcpy((char*)malloc(1 + strlen("<no value>")),"<no value>");

  this->year = 0;

}

void Car::setMake(const char* make){

  char *temp = strcpy((char *)malloc(strlen(make) + 1),make);

  free(this->make);   // avoid memory leak

  this->make = temp;

}

void Car::setModel(const char* model){

  char* temp = strcpy((char *)malloc(strlen(model) + 1),model);

  free(this->model);   // avoid memory leak

  this->model = temp;

}

void Car::setYear(int year){

  this->year = year;

}

const char* Car::getMake()const{

  return make;

}

const char* Car::getModel()const{

  return model;

}

int Car::getYear()const{

  return year;

}

void Car::print()const{

  std::cout<<"make: "<<make<<"\n"<<"model: "<<model<<"\n"<<"year: "<<year<<"\n";

}

Car::~Car(){

  free(make);

  free(model);

}


car.h:


#include<iostream>

#include<cstring>

#define MAX_STR 1024

class Car{

  char* make;

  char* model;

  int year;


  public:

  Car();   

  Car(const char* make, const char* model , int year);

  void setMake(const char* make);

  void setModel(const char* model);

  void setYear(int year);

  const char* getMake()const;

  const char* getModel()const;

  int getYear()const;

  void print()const;

  ~Car();

};


What to code:


Take your Car class from where you have made the C strings (make and model) dynamic. Add the following to your class:


  1. Week 6 Lab ExercisesCopy constructor.
  2. Operator Overloading for assignment operator (=)

Make sure that the C strings are copied by allocating new space for them and then using strcpy to copy the values into the new space (see lecture on how to do this). Also, make sure you delete any space that is already allocated by checking for a non-NULL pointer and then using delete to free up the space. The destructor needs to delete the C strings if the pointers are non-NULL.


Requirements:


Week 6 Lab Exercises• Must have dynamic CStrings in your Car class. No fixed arrays.


  1. Must have the following functions in your Car class: ◦ Default Constructor

◦ Parameterized Constructor

◦ Copy Constructor

◦ Destructor

◦ Operator Overload function for the assignment operator (=)

  1. Must have 2 files: car.h and car.cpp
  2. No function definitions in the car.h file. Only prototypes.

EXAMPLE OUTPUT #1

--------------------------------

print car1

make: Honda

model: Civic

year: 2017

print car2

make: Honda

model: Civic

year: 2017

change car2's model from Honda to Ford

re-print car1

make: Honda

model: Civic

year: 2017

re-print car2 (this should print a different make than car1) make: Ford

model: Civic

year: 2017

1 Expert Answer

By:

Patrick B. answered • 07/19/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Kat H.

Hi there, I'm getting the errors for each of these. Please correct them. Thank you. car.h:45:23: error: ISO C++ forbids declaration of ‘operator=’ with no type [-fpermissive] operator = (Car); ^ In file included from car.cpp:5:0: car.h:45:23: error: ISO C++ forbids declaration of ‘operator=’ with no type [-fpermissive] operator = (Car); ^ car.cpp:119:26: error: ISO C++ forbids declaration of ‘operator=’ with no type [-fpermissive] Car::operator = (Car car) ^
Report

07/19/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.