Polymorphism program - c++

I am needing help with polymorphism. I have no clue how to work with this. I have to write a program that creates an Orc and a Human and demonstrates the behavior of the classes. I am just needing some help with setting this program up. I have set up the classes with the information, but how do I get the createCharacter function to work? Would I have characterTotal = characterStrength + characterDexterity + characterIntelligence in the createCharacter function? I know my program is not correct right now and I have some errors and things, but I am still just trying to get a better understanding of this.
UPDATE:
I am having trouble with the createCharacter function. It is a pure virtual function and I am needing some help on how to get it to work.
For Human class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
(Let’s assume STR = 17, DEX = 12 and INT = 10. It will store 37 into characterTotal.Itwill print out a message: “The strong human Paladin, has a total scoreof 37.” (Strong adjective comes due to STR being 17. If something is above 17 you should say something related. STR = strong, DEX = dexterous, INT =
intelligent).
For Orc class createCharacter will:
Get the values of STR, DEX and INT. Will calculate the total of the values.
However Orcs receive -2 to INT and DEX. They receive +2 to STR. (Let’s
assume STR = 16, DEX = 10 and INT = 8. It will store 16+2,10-2,8-2 = 28 into
characterTotal.
It will print out a message “The berserker Orc has a total score of 28.” (Here the
Orcs get their adjectives from their clan names so you do not need to do
something specific to STR, DEX or INT.)
CODE:
//character.h
#ifndef CHARACTER_H
#define CHARACTER_H
using namespace std;
class Character
{
protected:
float characterTotal;
public:
virtual void createCharacter() = 0; //Pure virtual function
};
#endif
//human.h
#ifndef HUMAN_H
#define HUMAN_H
#include "Character.h"
using namespace std;
class Human
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterType;
public:
Human();//Constructor
int getStength
{
cout << "Enter a number from 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number from 0 to 18\n";
cin >> characterDexterity;
return CharacterDexterity;
}
int getIntelligence
{
cout << "Enter a number from 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getType
{
cout << "Please choose one of the following\n";
cout << "A -- Paladin \n";
cout << "B -- Ranger \n";
cout << "C -- Wizard \n";\
cin >> characterType;
return characterType;
}
};
#endif
//orc.h
#ifndef ORC_H
#define ORC_H
#include "Character.h"
#include "Human.h"
using namespace std;
class orc
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterClan;
public:
orc(); //Constructor
int getStrength
{
cout << "Enter a number between 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity
{
cout << "Enter a number between 0 to 18\n";
cin >> characterDexterity;
return characterDexterity;
}
int getIntelligence
{
cout << "Enter a number between 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getClan
{
cout << "Please choose one of the following\n";
cout << "A -- Barbarian \n";
cout << "B -- Berserker \n";
cout << "C -- Vanguard \n";\
cin >> characterClan;
return characterClan;
}
};
#endif

Example: A class called Account posesses a container with Character. Your classes Human and Orc inherit both from Character. Inside of your Human class you may want to set spell ABC to all characters which are created (in createCharacter). But if the player creates an Orc, you might want to set another spell XYZ to it, instead of ABC. Base classes come in handy here. In this example you see one of these abstract functions in action (Character::initCharacter).
You can put in more abstract functions inside of Character IF the classes which are supposed to inherit from Character MUST / NEED TO implement these functions.
This is only an example. If you want to do it properly, you need more than this and would have to modify all for database access and further abstraction.
enum CharacterType
{
CHAR_INVALID = 0x0,
CHAR_HUMAN = 0x1,
CHAR_ORC = 0x4
};
class Character
{
public:
Character(unsigned int charGUID) //You might want to set data in the constructor already
: m_charGUID(charGUID) //right after it has been load from Account::LoadAccountInformation()
{
std::cout << "Constructor of \"Character\"" << std::endl;
}
virtual ~Character(void) //Cleanup if needed
{
std::cout << "Destructor of \"Character\"" << std::endl;
}
virtual void createCharacter(void) = 0;
virtual void initCharacter(void) = 0;
CharacterType GetTypeID(void)
{ return m_typeID; }
protected:
void SetTypeID(CharacterType ct)
{ m_typeID = ct; }
private:
Character(const Character &);
unsigned int m_charGUID;
CharacterType m_typeID;
};
class Human : public Character
{
public:
Human(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_HUMAN);
std::cout << "Constructor of \"Human\"" << std::endl;
}
virtual ~Human(void) //Cleanup if needed
{
std::cout << "Destructor of \"Human\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Human\"" << std::endl;
}
};
class Orc : public Character
{
public:
Orc(unsigned int charGUID)
: Character(charGUID)
{
SetTypeID(CHAR_ORC);
std::cout << "Constructor of \"Orc\"" << std::endl;
}
virtual ~Orc(void) //Cleanup if needed
{
std::cout << "Destructor of \"Orc\"" << std::endl;
}
void createCharacter(void) override
{
//Set data...
}
void initCharacter(void) override
{
std::cout << "You initialized a character of type \"Orc\"" << std::endl;
}
};
class Account
{
public:
Account(unsigned int accountGUID)
{ m_accGUID = accountGUID; }
//#Return: False if load from database failed
bool LoadAccountInformation(void) //You could give it data also
{
//You could also load data directly from a database here if you'd like to
//Here are just some sample values (partially hardcoded)
characters.clear();
const int charsOnAccount = 1; //Load from database
for (int i = 0; i < charsOnAccount; ++i)
{
CharacterType ct = CHAR_HUMAN; //Load from database
unsigned int characterGUID = i;
switch (ct)
{
case CHAR_HUMAN:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Human(characterGUID)));
} break;
case CHAR_ORC:
{
characters[characterGUID] = std::move(std::shared_ptr<Character>(new Orc(characterGUID)));
} break;
default:
{
std::cout << "Invalid character type: " << ct << std::endl; //Or log to file
} break;
}
}
return true;
}
void InitCharacters(void)
{
for (auto itr = std::begin(characters); itr != std::end(characters); ++itr)
itr->second->initCharacter();
}
private:
//A unique account-GUID
unsigned int m_accGUID;
//Let's say a unique char-GUID and the Character object
std::map<unsigned int, std::shared_ptr<Character> > characters;
//And more information...
};
int main(int argc, char *argv[])
{
Account ac = Account(1);
ac.LoadAccountInformation();
ac.InitCharacters();
return 0;
}

Related

How to execute a class method by string input in C++

I am trying to develop a text adventure in C++ where users can input string commands (ex. "take apple").
Here is a very naive sample of code I came up with:
# include <iostream>
using namespace std;
class fruit{
public:
string actual_name;
fruit(string name){
actual_name = name;
}
take() {
cout << "You take a " << actual_name << "." << endl;
}
};
fruit returnObjectFromName(string name, fruit Fruits[]){
for(int i = 0; i <= 1; i++){ // to be modified in future depending on Fruits[] in main()
if (Fruits[i].actual_name == name)
return Fruits[i];
}
}
int main(){
string verb;
cout << "Enter verb: ";
cin >> verb;
string object;
cout << "Enter object: ";
cin >> object;
fruit apple("apple");
fruit Fruits[] = { apple }; // to be extended in future
// returnObjectFromName(object, Fruits). ??? ()
}
How can I possibly get the fruit method with something similar to the function returnObjectFromName, if this is even possible?
I began the development with Python (independently), and there I can at least use eval(), but as I understand in C++ this is not an option.
I tried also with map, but I didn't manage to make it work with methods.
Thank you all for your answers.
Its not good way to rely on reflection in C++ and i think there is no way to list methods in classes. Maybe you can use function pointers but pointer to instance methods are hell.
I recommend to use polymorphism and good design. If some items might be taken, then use interface like this:
#include <iostream>
using namespace std;
class ITakeable {
public:
virtual bool isTakeable() = 0;
virtual void take() = 0;
virtual void cannotTake() = 0;
};
class fruit : public ITakeable {
public:
string actual_name;
fruit(string name){
actual_name = name;
}
bool isTakeable() {
return true;
}
void take() {
cout << "You take a " << actual_name << "." << endl;
}
void cannotTake() {
cout << "not needed to be implemented";
}
};
class airplane : public ITakeable {
public:
string actual_name;
airplane(string name){
actual_name = name;
}
bool isTakeable() {
return false;
}
void take() {
cout << "not needed to be implemented";
}
void cannotTake() {
cout << "You CANNOT take a " << actual_name << "." << endl;
}
};
int main() {
fruit apple("apple");
if (apple.isTakeable()) {
apple.take();
}
airplane plane("boeing");
if (plane.isTakeable()) {
plane.take();
} else {
plane.cannotTake();
}
// use of interface in general
ITakeable * something = &apple;
if (something->isTakeable()) {
something->take();
}
something = &plane;
if (something->isTakeable()) {
something->take();
} else {
something->cannotTake();
}
return 0;
}
Since fruit is a user defined type, you have to declare your own methods for your type or you inherit from one previously defined.
There are a lot of method for "built-in" string type
that Performs virtually the same job as eval (...) in python.
Also I noticed your function need not be defined independently outside of class fruit.

Is it possible to use same functions for every class

I have multiple classes and each of them has their own methods. All of these methods perform the same task, as you can see in my code. The only unique thing is the values of the title, code and credit members that are defined inside the classes.
Is there a way to write this code such that a single set of methods can do the required tasks (using the specific values within the class that made the request to the method) for each and every class?
I'm a university student, and due to this I don't want to use inheritance since we haven't learned it yet.
class seng305
{
string title = "Software design and architecture", code = "SENG305";
int credit = 4;
public:
seng305();
~seng305();
string get_info();
string get_title();
int get_credit();
};
class comp219
{
string title = "Electronics in computer engineering", code = "COMP219";
int credit = 4;
public:
comp219();
~comp219();
string get_info();
string get_title();
int get_credit();
};
seng305::seng305()
{
cout << '\t' << "Created" << endl;
}
seng305::~seng305()
{
cout << '\t' << "Destroyed" << endl;
}
string seng305::get_info()
{
return (code + "-" + title);
}
string seng305::get_title()
{
return title;
}
int seng305::get_credit()
{
return credit;
}
//--------------------------------------------------
comp219::comp219()
{
cout << '\t' << "Created" << endl;
}
comp219::~comp219()
{
cout << '\t' << "Destroyed" << endl;
}
string comp219::get_info()
{
return (code + "-" + title);
}
string comp219::get_title()
{
return title;
}
int comp219::get_credit()
{
return credit;
}
As you can see, the get_info(), get_title(), and get_credit() methods do the same thing.
I would like for a single get_info(), get_title(), get_credit() to be able to do the task for each class.
There is no reason to use separate classes at all in this example. A single class will suffice, eg:
class course
{
string title, code;
int credit;
public:
course(const string &title, const string &code, int credit);
~course();
string get_info() const;
string get_title() const;
int get_credit() const;
};
course::course(const string &title, const string &code, int credit)
: title(title), code(code), credit(credit)
{
cout << '\t' << "Created" << endl;
}
course::~course()
{
cout << '\t' << "Destroyed" << endl;
}
string course::get_info() const
{
return (code + "-" + title);
}
string course::get_title() const
{
return title;
}
int course::get_credit() const
{
return credit;
}
Then, you simply create instances of your class as needed, eg:
course seng305("Software design and architecture", "SENG305", 4);
course comp219("Electronics in computer engineering", "COMP219", 4);
...
I know you said that you don't want to use inheritance, but that could be the next logical step, using the above code as a base:
class courseSeng305 : public course
{
public:
courseSeng305() : course("Software design and architecture", "SENG305", 4) {}
};
class courseComp219 : public course
{
public:
courseComp219() : course("Electronics in computer engineering", "COMP219", 4) {}
};
courseSeng305 seng305;
courseComp219 comp219;
...

C++ Initializing and Functions from Derived Class from Vector of Pointer Base

I have a problem where I have 2 classes, Word and Noun. Noun is derived from Word.
All Words and Nouns are to be stored in a single vector hence why I require it being in pointer.
The problem is I can't seem to initialize or call functions from the derived class Noun.
I can pushback for a new Word but not for a Noun.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Word{
protected:
string wordName, def, type;
public:
Word();
Word(string wordName, string def, string type) {
this->wordName = wordName;
this->def = def;
this->type = type;
};
void getWord() {
cout << wordName << endl;
}
void getDef() {
cout << def << endl;
}
void getType() {
cout << type << endl;
}
};
class Noun : public Word{
public:
Noun();
Noun(string wordName, string type, string def);
};
int main(){
vector <Word*> wordVector;
for (int i = 0; i < 2; i++)
{
string wordName, def, type;
cin >> wordName;
cin >> def;
cin >> type;
cout << endl;
wordVector.push_back(new Noun(wordName, def, type)); //new Word(wordName, def, type) works
}
for (int i = 0; i < 2; i++)
{
wordVector[i]->getWord(); //how do you call the function from Noun?
wordVector[i]->getDef();
wordVector[i]->getType();
}
system("pause");
return 0;
}
It comes out with 1 unresolved external symbol.
There are too many things wrong with this code, so allow me to present a correct, complete rewrite that does what you mean:
#include <iostream>
#include <memory> // for std::unique_ptr
#include <string>
#include <vector>
class Word
{
private:
std::string wordName, def, type;
public:
virtual ~Word() = default;
Word(std::string w, std::string d, std::string t)
: wordName(std::move(w))
, def(std::move(d))
, type(std::move(t)) {}
void printWord() const { std::cout << wordName << '\n'; }
void printDef() { std::cout << def << '\n'; }
void printType() const { std::cout << type << '\n'; }
};
class Noun : public Word
{
public:
using Word::Word;
};
int main()
{
std::vector<std::unique_ptr<Word>> wordVector;
for (int i = 0; i != 2; ++i)
{
std::string w, d, t;
if (!(std::cin >> w >> d >> t))
{
std::cerr << "Failed to read input!\n";
continue;
}
wordVector.push_back(std::make_unique<Noun>(
std::move(w), std::move(d), std::move(t)));
}
for (const auto& wp : wordVector)
{
wp->printWord();
wp->printDef();
wp->printType();
}
}
A completely unsorted and incomplete set of issues that I corrected:
Memory management (std::unique_ptr)
Virtual destructor for deletion through base
No nonsensical default constructors
Inheriting constructors rather than manual forwarding
Properly named member functions
Const correctness
No superfluous semicolons
No superfluous std::endl
Not abusing namespace std
Range-based for loop
I/O error handling
Initialize rather than assign
Enable move optimizations
No raw new
Correct access level for private members (exposed via member functions)
I think you just forgot to initialize the superclass constructor In your Noun class try this
class Noun : public Word{
public:
Noun();
Noun(string wordName, string type, string def): Word(wordName, type, def){};
};
I have tested it and it worked.
Also theres a light modification in you main(). just a slight. so that i can see what input it is asking for.
int main(){
vector <Word*> wordVector;
for (int i = 0; i < 2; i++)
{
string wordName, def, type;
cout<< "entere name";
cin >> wordName;
cout<< "entere def";
cin >> def;
cout<< "entere type";
cin >> type;
cout << endl;
wordVector.push_back(new Noun(wordName, def, type)); //new Word(wordName, def, type) works
}
for (int i = 0; i < 2; i++)
{
wordVector[i]->getWord(); //how do you call the function from Noun?
wordVector[i]->getDef();
wordVector[i]->getType();
}
system("pause");
return 0;
}

Error on my first class when trying to display

I'm building a class that will create a vehicle's characteristics and I'm having some difficulties when trying to display autovehicle a1 on the console. I get an error which I cannot explain. Please help, if possible
#include <iostream>
using namespace std;
class Autovehicle {
private:
//registry number
char * car_registration_number;
char engine;
int engine_cc;
int avg_consumption;
int avg_speed;
int avg_consumption_urban;
int avg_speed_urban;
int max_speed;
static int number_of_autovehicles;
public:
//getters
char * get_car_registration_number() {
return this->car_registration_number;
}
char get_engine()
{
return this->engine;
}
int get_engine_cc() {
return this->engine_cc;
}
int get_avg_consumption() {
return this->avg_consumption;
}
int get_avg_speed() {
return this->avg_speed;
}
int get_avg_consumption_urban() {
return this->avg_consumption_urban;
}
int get_avg_speed_urban() {
return this->avg_speed_urban;
}
int get_max_speed() {
return this->max_speed;
}
static int get_number_of_cars() {
return number_of_autovehicles;
}
//setters
void set_car_registration_number(char *car_reg_nr) {
this->car_registration_number = car_reg_nr;
}
void set_engine(char eng) {
this->engine = eng;
}
void set_engine_cc(int eng_cc) {
this->engine_cc = eng_cc;
}
void set_avg_consumption(int avg_cons) {
this->avg_consumption = avg_cons;
}
void set_avg_speed(int avg_spd) {
if (this->avg_speed > avg_spd)
cout << endl << "You should probably slow down";
this->avg_speed = avg_spd;
}
void set_avg_consumption_urban(int avg_cons_urb) {
this->avg_consumption_urban = avg_cons_urb;
}
void set_avg_speed_urban(int avg_spd_urb) {
if (this->avg_speed_urban > avg_spd_urb)
cout << endl << "You should probably slow down";
this->avg_speed_urban = avg_spd_urb;
}
void set_max_speed(int max_spd) {
this->max_speed = max_spd;
}
//default constructor
Autovehicle(){
number_of_autovehicles++;
this->car_registration_number = new char[strlen("There are no autovehicles") + 1];
strcpy(this->car_registration_number, "There are no autovehicles");
this->engine = NULL;
this->engine_cc = 0;
this->avg_consumption = 0;
this->avg_speed = 0;
this->avg_consumption_urban = 0;
this->avg_speed_urban = 0;
this->max_speed = 0;
}
//constructor
Autovehicle(char* car_reg_nr, char eng, int eng_cc, int avg_cons, int avg_spd, int avg_cons_urb, int avg_spd_urb, int max_spd){
number_of_autovehicles++;
this->car_registration_number = new char[strlen(car_reg_nr) + 1];
strcpy(this->car_registration_number, car_reg_nr);
this->engine = eng;
this->engine_cc = eng_cc;
this->avg_consumption = avg_cons;
this->avg_speed = avg_spd;
this->avg_consumption_urban = avg_cons_urb;
this->avg_speed_urban = avg_spd_urb;
this->max_speed = max_spd;
}
//destructor
~Autovehicle() {
delete[]this->car_registration_number;
number_of_autovehicles--;
}
};
This is where the void main, and the problem starts:
void main() {
Autovehicle a1( "Engniasd", "gasoline", 1980, 11, 50, 8, 100) {
cout<<"registration number: "<< this->car_registration_number;
cout << "engine: " << this->engine;
cout << "engine cc:" << this->engine_cc;
cout << "consumption: " << this->avg_consumption;
cout << "avg speed: " << this->avg_speed;
cout << "urban consumption: " << this->avg_consumption_urban;
cout << "urban speed " << this->avg_speed_urban;
}
}
The problem that I'm getting is "No instance of constructor matches the argument list" when trying to display Autovehicle a1(Engniasd, the first term)
Try to add the registration number to your car and the number of vehicles:
Autovehicle a1("REG12345", "gasoline", 1980, 11, 50, 8, 100, 50, 3).
Your constructor defines eng as a char, but you're passing it "gasoline" which is a const char*.
Also, storing the car registration number as a char*. Make your life easier and use a std::string instead. That way you, and your class users, don't need to worry about managing the lifetime of the pointer.
And, in main you've declared a variable called a1 but it looks like you are trying to call methods on it using this. You need to call against the instance variable:
cout << "engine: " << a1.engine;
You also need to terminate the a1 declaration with a semicolon and don't need the braces, eg:
Autovehicle a1( "Engniasd", "gasoline", 1980, 11, 50, 8, 100);
cout << "registration number: "<< a1.car_registration_number;
At school atm and unable to get a good look at this, but look at your constructor, you have a char instead of a char ptr for your 2nd argument. you can't implicitly convert char to const char*, you can char* -> const char*, so change your 2nd argument to a char*. Also, change void main() to int main(), main is supposed to be an int (ansi standard), and many compilers might not accept you using void main().
I see several problems.
Can you explain why car_registration_number and engine have different types?
If you can answer this one for yourself, you can fix one of your issues.
Also you should separate the class declaration from the implementation and make sure that you initialize all the variables. Including the static one.
Problems in calling constructor
Autovehicle(char* , char , int, int, int, int, int, int)
the second parameter is char, but you pass char*, also constructor have 6 int parameters, but you pass only five in main. And you cannot print values of object in your way. Create function in you class
void print()
{
cout<<"registration number: "<< this->car_registration_number;
cout << "engine: " << this->engine;
cout << "engine cc:" << this->engine_cc;
cout << "consumption: " << this->avg_consumption;
cout << "avg speed: " << this->avg_speed;
cout << "urban consumption: " << this->avg_consumption_urban;
cout << "urban speed " << this->avg_speed_urban;
}
and call it in main() as
Autovehicle a1( "Engniasd", "gasoline", 1980, 11, 50, 8, 100, 0);
a1.print();

Concept of data storing in c++

I have a Spieler class and a Verein class with a vector of Spieler members.
Now if I change something of the Players like the Staerke(german for strength) by using a function of this class in the player class it does not automatically change the value for this player.
Here is the code:
#include <vector>
#include<iostream>
#include <string>
using namespace std;
class Spieler
{
public:
void setinformation(int a, string b, string c, int d)
{
ID = a;
Vorname = b;
Nachname = c;
Staerke = d;
}
void getinformation()
{
cout << "ID: " << ID << endl;
cout << "Vorname: " << Vorname << endl;
cout << "Nachname: " << Nachname << endl;
cout << "Staerke: " << Staerke << endl << endl;
}
void setStaerke(int x)
{
Staerke = x;
}
int getStaerke()
{
return Staerke;
}
private:
string Vorname, Nachname;
int Staerke, ID;
};
class Verein
{
public:
void setSpielerListe(vector<Spieler> x)
{
Spielerliste = x;
}
vector<Spieler> getSpielerListe()
{
return Spielerliste;
}
string getVereinsName()
{
return VereinsName;
}
int getVereinsID() const
{
return VereinsID;
}
void setVereinsID(int x)
{
VereinsID = x;
}
int getGesamtstaerke()
{
Gesamtstaerke = 0;
vector<Spieler> b;
b = getSpielerListe();
for (size_t i = 0; i < b.size(); i++)
{
Gesamtstaerke = Gesamtstaerke + b[i].getStaerke();
}
return Gesamtstaerke;
}
void Vereinsinformationen()
{
vector<Spieler> list;
int id;
string vereinsname;
int gesamtstaerke;
id = getVereinsID();
vereinsname = getVereinsName();
gesamtstaerke = getGesamtstaerke();
list = getSpielerListe();
cout << "VereinsID: " << id << endl;
cout << "Vereinsname: " << vereinsname << endl;
cout << "Gesamstaerke: " << gesamtstaerke << endl << endl;
cout << "Spieler: " << endl;
for (size_t i = 0; i < list.size(); i++)
list[i].getinformation();
}
private:
vector<Spieler> Spielerliste;
int VereinsID, Gesamtstaerke;
string VereinsName;
};
vector<Spieler> spieler;
int main()
{
Spieler Spieler1;
Spieler1.setinformation(0, "Peter", "Pan", 10);
spieler.emplace_back(Spieler1);
Verein Team1;
Team1.setSpielerListe(spieler);
Spieler1.setStaerke(20);
Team1.Vereinsinformationen();
cin.get();
return 0;
}
I'm really new into c++ and programming so the code might be terrible.
Guess it has to do with pointers, I'm really not into the concept of storing data in c++, try to get it by trial & error; So how to change the Staerke in a way that it is changed in the Teams Playerlist too?
The problem is you are storing full object in the vector and not pointers. When you run this line:
spieler.emplace_back(Spieler1);
a copy of Spieler1 is made and put in the vector. So modifying it in the main will have no effect in the vector. Also not that you are copying the vector when setting in Verein class.
You should use pointer if this is what you are after or better yet have a function to modify strength from Verein class taking its id and new strength as parameters might be a good idea. Something like this:
void setStaerke(int id, int x)
{
vector<Spieler>::iterator it = Spielerliste.begin();
while (it != Spielerliste.end())
{
if ((*it).GetId() == id)
{
(*it).setStaerke(x);
break;
}
}
}
If you have access to C++11, it could be made more elegantly.
Hereby you pass and store a copy from the vector into the object:
Team1.setSpielerListe(spieler);
Therefore changes to the original vector and the contained objects will not affect the member.
Further, I don't have much experience with emplace_back, but the more usual way to append an object to a std::vector would also append a copy:
spieler.push_back(Spieler1);
Therefore changes to the original object would not affect the object you've appended to the container.
Make sure you better understand when objects are copied.
For reference:
http://en.cppreference.com/w/cpp/container/vector/emplace_back
http://en.cppreference.com/w/cpp/container/vector/push_back
How to pass objects to functions in C++?