I want to create a dynamic memory for a car dealership for bikes and cars but the vector didn't print the right things and has lots of repetitions that aren't supposed to be there.
I created an object for the overlapping variables for both vehicles so I can pass it through the bike class later . I tried to store all the elements in a vector and tried to overload the cout so it could print all elements inside the vector. I wanted to be able to have the vector use the object and print out all the elements correctly. Only the first 2 elements seem to be printing.
Main class
#include <iostream>
#include <algorithm>
#include "Car.h"
#include "Bike.h"
#include <list>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
#ifdef _DEBUG
_onexit(_CrtDumpMemoryLeaks);
#endif
vector<Car> cars ;
Car test = Car(2, "2", vehicleInfo("car", "M40 0J2", "lambo", "adventador", 3));
Car test2 = Car(3, "4", vehicleInfo("car", "M40 0J2", "lambo", "adventador", 3));
for (int i = 1; i <= 10; i++)
{
cars.push_back(test);
cars.push_back(test2);
}
for (Car number : cars)
{
cout << number << endl;
}
return 0;
}
Car class
#include "Car.h"
#include <iostream>
#include <cstdlib>
Car::Car(int doors, string seats, const vehicleInfo& carinfo) : number_of_doors(doors), number_of_seats(seats), carInfo(carInfo)
{
}
Car::Car(const Car& src) : number_of_doors(src.number_of_doors), number_of_seats(src.number_of_seats), carInfo(src.carInfo) //constant reference which initialises the variables
{
}
Car::~Car()
{
}
std::ostream& operator<<(ostream& os, Car& rhs)
{
return os<< rhs.number_of_doors << rhs.number_of_seats << rhs.carInfo;
// attempted operator overload
}
Car.h
#include <iostream>
using namespace std;
class Car
{
public:
Car(const Car& src); // takes in constant reference
Car(int doors, string seats,const vehicleInfo& carinfo);
~Car();
friend ostream& operator<<(ostream& os, Car& rhs);
private:
int number_of_doors;
string number_of_seats;
string carInfo;
};
ostream& operator<<(ostream& os, Car& rhs);
VehicleInfo.h
#pragma once
#include <string>
using namespace std;
class vehicleInfo
{
public:
//vehicleInfo() : type("N/A"), registration_number("N/A"), make("N/A"), age(0){}
vehicleInfo(string type, string reg, string make, string model, int age);
~vehicleInfo();
private:
string type;
string registration_number;
string make;
string model;
int age;
};
The output was
22 34 22 34 22 34 22 34 22 34 22 34 22 34 22 34 22 34 22 34