Objectives: The two main objectives of this project is to test your ability to (
ID: 3903033 • Letter: O
Question
Objectives: The two main objectives of this project is to test your ability to (1) create and use pointers, and (2) create and use C++ classes. A review of your knowledge of structs, arrays, iostream, file L/O and C-style strings is also included. Description: This project will expand Project 2 by adding additional functionality, using pointers, and implementing abstract data types (ADTs) through classes. Pointers must be used for all array manipulation, including arrays with ADTs (structs, classes) e.g, rental cars, rental agencies. Pointers must be used in function prototypes and function parameter lists not square brackets. Make sure all your C-string functions (e.g. string copy, string compare, etc.) work with pointers (parameters list and function implementation). Square brackets are to be used only when declaring an array. For this project, pointers can only be moved by conducting post/pre increment/decrement operations on them (i.e., ++ or-), or by setting the pointer back to the base address using the array name. All pointers must be passed by value. (Note: Try to use the arrow operator (->) with Class Object pointers for member access, if you use such in your code.) The new functionality is as follows: You are given an updated data file (e.g. Agencies.txt) where there are 3 rental Car Agency locations, where each of the 3 locations (RentalAgency) has 5 cars (RentalCar). You will have similar menu options, but the functionality has been updated below. Note: using multiple helper functions to do smaller tasks will make this project significantly easier. You may want to create a function that will get a car from a location based on the location and car indices. The RentalCar Class will contain the following data members: m_year, an int (year of production) m_make, a C-string (char array of 255 maximum size) m model, a C-string (char array of 255 maximum size) m price, a float (price per day) m available, a bool (1true; 0 -false; try to display true/false using the "std: :boolalpha" manipulator like: coutExplanation / Answer
CODE TO COPY:
#include<iostream>
#include<fstream>
using namespace std;
void MyStringCopy(char *des, char*src)
{
int index=0;
while(src[index]!='')
{
des[index]=src[index];
index++;
}
des[index]='';
}
int MyStringCompare(char *s1, char* s2)
{
int ind1=0, ind2=0;
while(s1[ind1]!='' && s2[ind2]!='')
{
if(s1[ind1]<s2[ind2])
return -1;
else if(s1[ind1]>s2[ind2])
return 1;
}
if(s1[ind1]=='' && s2[ind2]=='')
return 0;//strings are equal
if(s1[ind1]=='')
return -1;
else
return 1;
}
class RentalCar{
private:
int m_year;
char m_make[255];
char m_model[255];
float m_price;
bool m_available;//1 true 0 false
public:
RentalCar()
{
m_year=0;
m_price=0.0;
m_available=false;
}
RentalCar(int year, char *make, char *model, float price, bool available)
{
MyStringCopy(m_make, make);
MyStringCopy(m_model, model);
m_year=year;
m_price=price;
m_available=available;
}
void setYear(int year)
{
m_year=year;
}
int getYear()
{
return m_year;
}
void setMake(char *make)
{
MyStringCopy(m_make, make);
}
void setModel(char *model)
{
MyStringCopy(m_model, model);
}
char* getMake()
{
return m_make;
}
char* getModel()
{
return m_model;
}
void setPrice(float price)
{
m_price=price;
}
float getPrice()
{
return m_price;
}
void setAvailable(bool avai)
{
m_available=avai;
}
bool getAvailable()
{
return m_available;
}
};
struct RentalAgency{
char name[255];
int zipCode[5];
RentalCar* cars[5];
};
//print car method
void printCar(RentalCar *car)
{
cout<<car->getYear()<<" "<<car->getMake()<<" "<<car->getModel()<<", "<<"$"<<car->getPrice()<<" per day, "<<"Available: "<<car->getAvailable()<<endl;
}
//print all information function
void printAll(RentalAgency* agency[], int size)
{
for(int i=0;i<size; i++)
{
cout<<agency[i]->name<<" ";
for(int j=0; j<5; j++)
{
cout<<agency[i]->zipCode[j];
}
cout<<endl;
for(int j=0; j<5; j++)
printCar(agency[i]->cars[j]);
cout<<endl<<endl;
}
}
int main()
{
cout<<"Enter the file name."<<endl;
string fileName;
cin>>fileName;
RentalAgency* agencies[10];
int index=0;
//read data from file
ifstream input;
input.open(fileName.c_str());
char name[255];
string zip;
int year;
float price;
bool available;
//string make, model;
char make[255];
char model[255];
RentalAgency *agency;
while(!input.eof())
{
//cout<<"ind: "<<index<<endl;
input>>name;
input>>zip;
//cout<<name<<zip<<endl;
agency=new RentalAgency();
agencies[index]=agency;
//cout<<"t33"<<endl;
MyStringCopy(agency->name, name);
for(int i=0; i<5; i++)
{
agency->zipCode[i]=(int)(zip.at(i)-48);
}
//cout<<"QF"<<endl;
//read five cars
for(int i=0; i<5; i++)
{
(agency->cars[i])=new RentalCar();
input>>year>>make>>model>>price>>available;
cout<<year<<make<<model<<price<<available<<endl;
agency->cars[i]->setYear(year);
agency->cars[i]->setMake(make);
agency->cars[i]->setModel(model);
agency->cars[i]->setPrice(price);
agency->cars[i]->setAvailable(available);
//printCar(agency->cars[i]);
}
//cout<<"index: "<<index<<endl;
agencies[index]=agency;
index++;
}
//print all informtion
printAll(agencies, index);
cin.get();
return 0;
}
In case of any doubt, please let me know in the comments section. I'll get it resolved :):) Don't forget to give a THUMBS UP! :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.