Hi there,
Thanks for answering, that helps me understand your situation a little bit better.
Alright, now as for your assignment:
I'll use a very simple example, which you can directly convert to your situation.
Note that I won't use a vector, as I'm not yet familiar enough with those myself, but the program could easily be modified to use one.
Also, the way I'm presenting may help you understand vectors a little bit better.
I'll use the simple example of making a phone book.
Phone books exist of a long list of phone numbers, and the name of the person whom that number belongs to.
So in object oriented code, we can make a phone number look like this:
1 2 3 4 5 6 7 8 9
|
class Phonenumber {
int number;
std::string name;
public:
Phonenumber(int nr, std::string nm) : number(n),name(nm) {}
int get_number() {return number;}
std::string get_name {return name;}
};
|
int number
and
std::string name;
are characteristics that describe a phone number. Now it's important to distinguish between a class and an object. Classes are to object what blueprints are to houses. The blueprint is not the house, the first has to be built (created) first. In a similar way, you need to create objects, in your case this could be done in main():
1 2 3 4 5 6 7 8
|
int main()
{
Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");
//You will then be able to access the data by doing
nr1.get_number();
nr1.get_name();
}
|
When creating an object, it's constructor is automatically called. Constructors have the same name as the class, so in this case:
Phonenumber(int nr, std::string nm) : number(n),name(nm) {}
This makes sure that the objects internal data, number and name, are set to the data you pass when creating the object:
Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");
In order to represent a list of phone numbers, we can create another class that will represent the phonebook:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
class Phonebook {
Phonenumber list[100]; //array of maximum 100 phonenumbers
int list_count; //stores the amount of items in the list
public:
Phonebook() : list_count(0) {} //constructor sets list count to zero
void add(Phonenumber pn)
{
//check should be added here that list is < 100 elements
list[list_count] = pn //add phone number to list
list_count++; //increase list count
}
Phonenumber get_by_name(std::string search)
{
for (int i=0; i<list_count; i++) //for every number in the list
{
if (list[i].get_name().compare(search) != 0 ) //if the name is the same as our search
{
return list[i]; //return the phonenumber
}
}
std::cout << "nothing found for " <<search; //if not found
return Phonenumber(0, "not found"); //return empty phonenumber
}
};
|
This is very basic of course, but it suffices for your purposes.
You would use it in main as such:
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");
Phonenumber nr2 = Phonenumber(1237654, "Swedish Chef");
Phonebook pb;
pb.add(nr1);
pb.add(nr2);
Phonenumber search = pb.get_by_name("Kurt Wallander");
std::cout << "Phonenumber sought: " << search.get_name() << " - " <<search.get_number();
}
|
Hopefully that makes sense to you.
Let us know if you have any further questions.
All the best,
NwN