Using vector to store two different class information

I am basically trying to get the vector to store information entered by the user and then display it. Having trouble displaying information. Here is the code that i had written


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>



using namespace std;

class Vehicle
{
private:
string vehColor;
int regNo;
public:
Vehicle(string color, int reg):vehColor(color), regNo(reg){}
~Vehicle(){}

virtual void display()
{
cout << "Vehicle's color is:" << vehColor << endl;
cout << "Vehicle's Registration number is:" << regNo << endl;
}

};

class Car : public Vehicle
{
private:
string fuelType;
int engineCapacity;
public:
Car(string vehicleColor, int reg, string fuelT, int capacity): Vehicle(vehicleColor, reg), fuelType(fuelT), engineCapacity(capacity) {}

void display()
{
Vehicle::display();
cout << "Fuel type is:" << fuelType << endl;
cout << "Engine capacity is:" << engineCapacity << endl;
}

};

class Bike : public Vehicle
{
private:
int breakHorsePower;
public:
Bike(string vehColor, int reg, int bhp ): Vehicle(vehColor, reg), breakHorsePower(bhp){}
void display()
{
Vehicle::display();
cout << "Break horse power is:" << breakHorsePower << endl;
}
~Bike(){}
};



int main()
{
vector<Vehicle*> storeInfo;
Car * car;
Bike * bike;
char choice;
string color;
int registration;
string fType;
int eCapacity;
int bhp;

cout << "a.Insert car info \nb.Insert bike info \nc.display \nq.exit";
cout << "\nEnter your choice:";

cin >> choice;

do
{
switch(choice)
{
case 'a':
{
cout << "Enter the Car's color:";
cin >> color;
cout << "Enter the registration number:";
cin >> registration;
cout << "Enter the fuel type:";
cin >> fType;
cout << "Enter the engine capacity:";
cin >> eCapacity;

car = new Car(color, registration, fType, eCapacity);
storeInfo.push_back(car);
break;

}
case 'b':
{
cout << "Enter the Bike's color:";
cin >> color;
cout << "Enter the registration number:";
cin >> registration;
cout << "Enter the break horse Power:";
cin >> bhp;


bike = new Bike(color, registration, bhp);
storeInfo.push_back(bike);
break;
}

case 'c':
{
for_each(storeInfo.begin(), storeInfo.end(), car -> display(), bike -> display());
break;

}

}
cout << "\nEnter your choice again:";
cin >> choice;
}while(choice != 'q');

}

Last edited on
Hello,
wrap your code in [.code][./code] tags next time. Without dots, of course :)

Here's reference to for_each:
http://www.cplusplus.com/reference/algorithm/for_each/
Basically, you're using it wrong. However, since I'm not familiar to this function, I do not know how to use it properly to use member function.
Instead, I'd recommend this loop type:
1
2
for(auto& it : storeInfo)
    it.display();


Cheers!
std::for_each takes three parameters, not four. http://www.cplusplus.com/reference/algorithm/for_each/


1
2
3
4
5
6
7
8
// range for
for ( auto v : storeInfo )
{
  v->display();
}

// or lambda
for_each( storeInfo.begin(), storeInfo.end(), [](Vehicle * v){ v->display(); } );
yeah the indentation was completely lost. I will keep that in mind.
Silly mistake with the for_each. Checked on the syntax.

The lambda bit does not work, Do i need to have the operator() function included in my vehicle class?

and i am using an older version of codeblocks which does not let me use the c++11 changes.
1
2
3
4
5
vector<Vehicle*>::iterator iter;
               for (iter = storeInfo.begin(); iter != storeInfo.end(); iter++ )
                {
                    
                }

This would be similar to that, right? and what would i write in the block to display.
Last edited on
You have an iterator. Dereferencing the iterator returns the value_type: Vechile*. Member access via a pointer does use the -> operator.
generic - I would highly recommend you to upgrade MinGW then. I suppose you're using windows. It doesn't really matter what IDE you use, or what version of your IDE you use(in other words, it doesn't really matter if you use old or new C::B). The thing that you should "replace" is compiler. And you can do that by downloading new one and installing it. It's good practice for you, since you'll eventually need to do that anyway, and the faster you learn it, the more you'll know. And it's useful knowledge to have :)

Back to the topic - Firstly, I'd recommend using this syntax:
 
for(std::vector<Vehicle*>::iterator iter = storeInfo.begin(); //rest...) 

Or even this one(it's easier to use):
 
for(auto it = storeInfo.begin(); //rest...) 

I believe the variable there is limited to the scope of the loop, so memory it occupies(iterator) is automatically freed after loop.

And yes. Your function, if you don't have the loop type I provided you with, would look like this:
1
2
3
4
for(auto it = storeInfo.begin(); it != storeInfo.end(); ++iter)
{
   it->display();
}

It's just the plain old for loop, just using vector's iterator :)
Cheers!
Last edited on
Thank you Matthew, it works now and i will update that now.
Topic archived. No new replies allowed.