Classes

Hi there :)

I am learning about classes and there are certain things I am not sure.
One of this is that I wanted to check, if I create a class for example CarDatabase. Inside I have ability to provide specific details about object such as brand, size, type of engine.

What is best way to store this?
Each object have different attributes which can be of any type, unique numbers such as weight, dimensions, size, colour etc.
how to keep this?
How this can be accessed if I want to list all red cars? or cars with 2 doors?

I hope I am not mixing this badly ;) and its fairly clear what I need :)

thanks :)
One way to do it is to create a class or struct for a car and store all your cars in a vector. Then you can search the vector for specific cars.
1
2
3
4
5
6
7
8
struct Car
{
   string brand;
   int weight;
   int doors;
   // more attributes
};
vector<Car> cars;

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm
http://www.cplusplus.com/reference/vector/vector/vector/
Hi Thomas1965

thanks for this, so we could keep data about car inside struct (details such as brand, weight, colour etc) but then would you use vector to all car objects, do you have to do that?

I am trying to put this in my mind,
I have class CARS

then I create objects and each object have its own attributes

Cars car1 red, VW, 4 door, 2000KG
cars car2 blue, Mercedes, 3 door, 1500KG
cars car3 silver, BMW, 2 door, 5000kg
....

how this would be kept, in class, struct, vector?

How about situation when program is switched off? Do we keep this in some kind of database?

thanks :)
Hi,

Naming is important in OOP design. Notice Thomas1965 called the struct Car: because it is a solitary car. And the vector is cars because it is a collection of cars.

but then would you use vector to all car objects, do you have to do that?


No, but it makes more sense to do so, having a collection is better than having lots of individual objects. A collection is a sort of abstraction: we group like things together.

std::vector is a good choice for a container. Have a look at the documentation for it, one can push_back items into it.

Read the tutorial on this site about classes too.
How about situation when program is switched off? Do we keep this in some kind of database?


Well that would necessitate writing and reading from files.
Hi TheIdeasMan,

thank for the explanation.
I done few exercises that involved vector I assumed that vector will keep (in similar way to array) would keep only one type of data like integer so I could store in index 0 weight 2000, in index 1 1500 and in index 3 5000KG which would represent three cars from above example VW - 2000KG, Mercedes, 1500KG and BMW 5000KG.

based on what i read in this post I think message is that vector will allow (through) struct to keep all attribiutes, colour, brand, weight etc...

correct?
Yes all the STL containers allow any type including one's own struct or class (these are types in C++). They can also be nested.

Have a big read of all the documentation - a lot to be learnt there.

Edit:

It's late at my end, back tomorrow :+)
Last edited on
TheIdeasMan - I am guessing you are located in sunny Australia then? :)

and thank your for the assistance :)

Most definitely I will be reading more
Topic archived. No new replies allowed.