
Patrick B. answered 07/19/21
Math and computer tutor/teacher
//CAR.H
#ifndef _CAR
#define _CAR
#define DEFAULT_LENGTH (25)
typedef
class Car
{
protected:
char * make;
char * model;
int year;
public:
Car();
Car( char * carMake, char * carModel, int yr);
Car(const Car&);
~Car();
operator = (Car);
int GetCarMakeLength();
int GetCarModelLength();
void GetCarMake(char*);
void GetCarModel(char*);
int GetYear();
Car(char * carBuffCSV);
void SerializeToCSV(char*);
} * TCar;
#define CAR_SIZE (sizeof(Car))
#endif
//CAR.CPP
#ifndef _CAR
#include "car.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Car::Car()
{
make = (char*)malloc(DEFAULT_LENGTH);
model =(char*)malloc(DEFAULT_LENGTH);
memset(make,0,DEFAULT_LENGTH);
memset(model,0,DEFAULT_LENGTH);
year=0;
}
Car::Car( char * carMake, char * carModel, int yr)
{
int n = strlen(carMake)+1;
make = (char*)malloc(n);
strcpy(make,carMake);
n=strlen(carModel)+1;
model = (char*)malloc(n);
strcpy(model,carModel);
year = yr;
}
Car::Car(const Car& car)
{
int n = strlen(car.make)+1;
make = (char*)malloc(n);
strcpy(make,car.make);
n=strlen(car.model)+1;
model = (char*)malloc(n);
strcpy(model,car.model);
year = car.year;
}
Car::~Car()
{
if (make!=NULL) { free(make); }
if (model!=NULL) { free(model); }
year =0;
}
Car::operator = (Car car)
{
int n = strlen(car.make)+1;
make = (char*)malloc(n);
strcpy(make,car.make);
n=strlen(car.model)+1;
model = (char*)malloc(n);
strcpy(model,car.model);
year = car.year;
}
int Car::GetCarMakeLength()
{
return(strlen(make)+1);
}
int Car::GetCarModelLength()
{
return(strlen(model)+1);
}
void Car::GetCarMake(char* carMakeBuff)
{
strcpy(carMakeBuff,make);
}
void Car::GetCarModel(char* carModelBuff)
{
strcpy(carModelBuff,model);
}
int Car::GetYear()
{
return(year);
}
//make,model,year
Car::Car(char * carBuffCSV)
{
char token[1024];
memset(token,0,1024);
strcpy(token,strtok(carBuffCSV,","));
int n = strlen(token)+1;
make = (char*)malloc(n);
strcpy(make,token);
memset(token,0,1024);
strcpy(token,strtok(NULL,","));
n = strlen(token)+1;
model= (char*)malloc(n);
strcpy(model,token);
year = atoi(strtok(NULL,","));
printf(" parsed --- make >%s< model >%s< year:%d \n",make,model,year);
}
void Car::SerializeToCSV(char* carBuffCSV)
{
sprintf(carBuffCSV,"%s,%s,%d",make,model,year);
}
//Main.cpp
using namespace std;
#include <iostream>
#include <string.h>
#ifndef _CAR
#include "car.h"
#endif
void CarDump(Car car, char * strMsg=NULL)
{
char * outbuff;
if (strMsg!=NULL)
{
cout << " *************************************************************" << endl;
cout << strMsg << endl;
}
cout << " *************************************************************" << endl;
int n = car.GetCarMakeLength();
outbuff = (char*)malloc(n);
car.GetCarMake(outbuff);
cout << " Car Make :>" << outbuff << "<" << endl;
free(outbuff);
n = car.GetCarModelLength();
outbuff = (char*)malloc(n);
car.GetCarModel(outbuff);
cout << " Car Model :>" << outbuff << "<" << endl;
free(outbuff);
cout << "Year :" << car.GetYear() << endl;
char carBuffCSV[1024];
car.SerializeToCSV(carBuffCSV);
cout << " car csv :>" << carBuffCSV << "<" << endl ;
}
int main(int argc, char** argv)
{
char carMake[25];
char carModel[25];
strcpy(carMake,"MITSUBISHI");
strcpy(carModel,"MIRAGE");
Car car(carMake,carModel,2020);
CarDump(car);
char carBuffCSV[]={"Toyota,Corolla,2019"};
Car herCar(carBuffCSV);
CarDump(herCar);
Car carCopy = car;
CarDump(carCopy,(char*)"COPY OF MY CAR");
return 0;
}
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) ^07/19/21