I'm working on a project where we're not allowed to use the <string> library at all - we can only work with strings as character pointers and we have to write our own functions for them(strcpy, strlen, etc). I'm trying to construct a RentalCar class with the following header file:
#ifndef RENTALCAR_H
#define RENTALCAR_H
class RentalCar {
public:
RentalCar();
RentalCar(char* make, char* model);
char* getMake() const;
char* getModel() const;
void setMake(char* make = "");
void setModel(char* model = "");
private:
char m_make[256];
char m_model[256];
};
#endif
My source file contains the following:
#include <iostream>
#include "RentalCar.h"
using namespace std;
RentalCar::RentalCar() {
setYear();
setMake();
setModel();
setPrice();
setAvailable();
}
RentalCar::RentalCar(int year, char* make, char* model, float price,
bool available) {
setYear(year);
setMake(make);
setModel(model);
setPrice(price);
setAvailable(available);
}
char* RentalCar::getMake() const{
return m_make;
}
char* RentalCar::getModel() const{
return m_model;
}
void RentalCar::setMake(char* make) {
myStringCopy(m_make, make);
}
void RentalCar::setModel(char* model) {
myStringCopy(m_model, model);
}
char* myStringCopy(char* destination, const char* source) {
int index = 0;
while(*(source + index) != '\0') {
*(destination + index) = *(source + index);
index++;
}
*(destination + index) = '\0';
return destination;
}
My problem is that I'm getting the following error in my getMake and getModel methods:
cannot initialize return object of type 'char *'
with an lvalue of type 'char const[256]'
I'm not sure how to construct default strings without making them literals - which is why I think I'm getting this error.
My other question is that in order to set the strings in my setMake() and setModel() functions, I need to use my myStringCopy() function, so should I include it as a function in this class, or is there a way to get access to it otherwise? I also need to use it in my actual project file and it feels redundant to include it there and in the RentalCar.cpp as well
It's worth mentioning we're not allowed to work with strings using array indexing in any way - except to initialize a new string.
Any help would be appreciated! Thank you!
std::stringbut encapsulating the string management and comparison makes the code that uses it so much easier to write.std::stringhas been available since then. That is a generation now. Could you imagine a TV repair course teaching how to replace vacuum tubes?