I've been trying to figure out how to reach and change an attribution of an instance from another class' instance in C++. Apperently I've made a copying process inbetween, I don't know how to solve this.
I have two classes, Woman and Man, each has one instance in main: Andy and John, respectively. Both have names, their names is setted by their own mutators. Then I've created a vector attribution called as list for both Andy and John, and I've enlisted them into one another's list.
Now I want to reach and change the name of them throughout one another's lists. In a nutshell:
push back John to Andy's list.
get John's name through Andy's list.
change John's name through Andy's list (this is supposed to affect not only the item's name in Andy's list, but also the name of John-himself).
As far as I know, the solution should do something with pass-by-ref, but I don't know how to do so, once the classes are included.
output:
name of persons Andy John
name of persons Andy John
name of persons AndySugars JohnWick \\name of the person in Andy's and John's lists
name of persons Andy John \\name of Andy and John after changing their name from one-another's lists.
main.cpp:
#include "Man.h"
int main()
{
Woman Andy;
Man John;
Andy.setName("Andy");
John.setName("John");
Andy.setList(John);
John.setList(Andy);
std::vector<Man> list_of_Andy = Andy.getList();
std::vector<Woman> list_of_John = John.getList();
std::cout << "name of persons in the lists " << list_of_Andy.at(0).getName() << " " << list_of_John.at(0).getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
list_of_Andy.at(0).setName("JohnWick");
list_of_John.at(0).setName("AndySugars");
std::cout << "name of persons in the lists " << list_of_Andy.at(0).getName() << " " << list_of_John.at(0).getName() << std::endl;
std::cout << "name of persons " << Andy.getName() << " " << John.getName() << std::endl;
int a;
std::cin >> a;
return 0;
}
classes.h:
#include<string>
#include<iostream>
#include<vector>
class Man;
class Woman;
class Man
{
public:
Man();
std::string name;
std::vector<Woman> list;
void setName(std::string nameIn);
std::string getName();
void setList(Woman manIn);
std::vector<Woman> getList();
};
class Woman
{
public:
Woman();
std::string name;
std::vector<Man> list;
void setName(std::string nameIn);
std::string getName();
void setList(Man manIn);
std::vector<Man> getList();
};
Woman::Woman()
{
name = "undeclared";
}
void Woman::setName(std::string nameIn)
{
name = nameIn;
}
std::string Woman::getName()
{
return name;
}
void Woman::setList(Man manIn)
{
list.push_back(manIn);
}
std::vector<Man> Woman::getList()
{
return list;
}
Man::Man()
{
name = "undeclared";
}
void Man::setName(std::string nameIn)
{
name = nameIn;
}
std::string Man::getName()
{
return name;
}
void Man::setList(Woman manIn)
{
list.push_back(manIn);
}
std::vector<Woman> Man::getList()
{
return list;
}