How to get the name of an Object - c++

I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}

If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;

Related

Construct two classes that has attributes and can use methods of each other

I'd like to know is it possible that two classes has attributes and can use methods of each other. For example, there're a class STUDENT and a class COURSE, a STUDENT have a list of joined courses and a COURSE have list of participants(students). I tried this:
in STUDENT.h
#include <iostream>
#include <vector>
// #include "COURSE.h"
class COURSE;
class STUDENT {
string name;
std::vector<COURSE*> listCourses;
public:
STUDENT(){};
addCourse(COURSE* &course){
listCourses.push_back(course);
course.addStudent(this);
}
string getName(){
return this->name;
}
void showCourses(){
for(COURSE* course : listCourses)
std::cout << course->getName() << std::endl;
}
};
in COURSE.h
#include <iostream>
#include <vector>
// #include "STUDENT.h"
class STUDENT;
class COURSE {
string name;
std::vector<STUDENT*> listStudents;
public:
COURSE(){}
addStudent(STUDENT* &student){
listStudents.push_back(student);
student.addCourse(this);
}
string getName(){
return this->name;
}
void showStudent(){
for(STUDENT* student : listCourses)
std::cout << student->getName() << std::endl;
}
};
If I include two classes each other, it said errors. If I just include one, just one class worked, other class has problem.
Can anyone help me to fix it and I wonder that is it necessary to use some design patterns or data structures to solve this.
Thank you
Yes, what you are attempting is possible, but not in the way you are attempting it. You need to separate your method declarations and definitions.
Also, you have a flaw in your add... methods that will lead to endless recursion as soon as a Student is added to a Course or vice versa. You need to detect when the two are already linked together so that you can avoid the loop.
Try something more like this:
Student.h
#ifndef StudentH
#define StudentH
#include <vector>
#include <string>
class Course;
class Student {
std::string name;
std::vector<Course*> listCourses;
public:
Student() = default;
~Student();
std::string getName() const;
void addCourse(Course* course);
void removeCourse(Course* course);
void showCourses() const;
};
#endif
Student.cpp
#include "Student.h"
#include "Course.h"
#include <iostream>
#include <algorithm>
Student::~Student() {
for(Course* course : listCourses) {
course->removeStudent(this);
}
}
std::string Student::getName() const {
return name;
}
void Student::addCourse(Course* course) {
if (std::find(listCourses.begin(), listCourses.end(), course) == listCourses.end()) {
listCourses.push_back(course);
course->addStudent(this);
}
}
void Student::removeCourse(Course* course) {
auto iter = std::find(listCourses.begin(), listCourses.end(), course);
if (iter != listCourses.end()) {
listCourses.erase(iter);
course->removeStudent(this);
}
}
void Student::showCourses() const {
for(Course* course : listCourses) {
std::cout << course->getName() << std::endl;
}
}
Course.h
#ifndef CourseH
#define CourseH
#include <vector>
#include <string>
class Student;
class Course {
std::string name;
std::vector<Student*> listStudents;
public:
Course() = default;
~Course();
std::string getName() const;
void addStudent(Student* student);
void removeStudent(Student* student);
void showStudents() const;
};
#endif
Course.cpp
#include "Course.h"
#include "Student.h"
#include <iostream>
#include <algorithm>
Course::~Course() {
for(Student* student : listStudents) {
student->removeCourse(this);
}
}
std::string Course::getName() const {
return name;
}
void Course::addStudent(Student* student) {
if (std::find(listStudents.begin(), listStudents.end(), student) == listStudents.end()) {
listStudents.push_back(student);
student->addCourse(this);
}
}
void Course::removeStudent(Student* student) {
auto iter = std::find(listStudents.begin(), listStudents.end(), student);
if (iter != listStudents.end()) {
listStudents.erase(iter);
student->removeCourse(this);
}
}
void Course::showStudents() const {
for(Student* student : listStudents) {
std::cout << student->getName() << std::endl;
}
}

class managing objects of other class with container

I need to implement two classes.
One should contain an Employee with no methods, just constructor.
Second one should have std::vector container in with all the Employers made inside it, and few methods to manipulate those objects for example to add new employee or change his id.
After many attempts with errors im stuck and i dont know how to pass created employees to container in second class. I dont even know how the HR class should look like (its constructor should only have container?) and how it can make this container of Employee class objects.
First class is Employee.h, Employee.cpp and second is HR.h, HR.cpp
Here is my Employee.h
#include <iostream>
class Employee {
public:
std::string id;
std::string name;
std::string surname;
std::string departmentId;
std::string position;
Employee(std::string id, std::string name, std::string surname, std::string departmentId, std::string position);
~Employee();
};
Employee.cpp
#include "Employee.h"
#include <iostream>
#include <vector>
Employee::Employee(std::string xid, std::string xname, std::string xsurname, std::string xdepartmentId, std::string xposition) : id (std::move(xid)), name (std::move(xname)), surname (std::move(xsurname)), departmentId (std::move(xdepartmentId)), position (std::move(xposition))
{
std::cout << "constructor of worker"<< std::endl;
std::cout << "id:" + this->id + " name:" + this->name + " surname:" + this-> surname + " department:" + this->departmentId + " position:" + this->position << std::endl;
};
Employee::~Employee()
{
std::cout << "destructor"<< std::endl;
};
main.cpp
#include <iostream>
#include <vector>
#include "Employee.h"
#include "HR.h"
int main() {
Employee first("2","John","Smith","1","c++ developer");
Employee second("3","Steven","McDonald","2","administrator");
Employee third("4","Mark","Johnson","1","c++ developer");
system("pause");
return 0;
}
Should i place this push_back in Employee constructor and throw it somehow to HR class everytime new employee is created?
I'm really stuck right here.
EDIT: HR.h code - it's almost nothing there. std::vector is now created by method of HR. I dont know how HR constructor should look like and how can it implement objects from Employee class to vector container.
HR.h
#include "Employee.h"
#include <vector>
class HR {
public:
void addEmp(Employee);
HR();
~HR();
};
HR.cpp
#include <iostream>
#include <vector>
#include "HR.h"
HR::HR()
{
std::cout << "constructor HR"<< std::endl;
};
void HR::addEmp(Employee)
{
std::vector < Employee > all;
};
HR::~HR()
{
std::cout << "destructor HR"<< std::endl;
};

Can't set member data when nested in a class

I'm having this issue on another program, but I tried to simplify it with this one. I cannot set the weapon name through p.getWeaopn().setName("sword"); It works fine when I simply set it through its own object, but when I try to access the setter through player it doesn't set anything.
#include <iostream>
#include <string>
#include "Player.h"
#include "Weapon.h"
using namespace std;
int main()
{
Player p; // Player contains only a Weapon weapon;
Weapon w; // Weapon only contains a string name;
//w.setName("sword"); // this changes the name of the weapon
p.setWeapon(w);
p.weapon.setName("sword"); // this also changes the name
p.getWeapon().setName("sword"); // this is not setting the name. Why?
// checking if weapon has a name
if (p.getWeapon().getName().empty())
{
cout << "Weapon name is empty!" << endl;
}
else
{
cout << "Weapon name is " << p.getWeapon().getName() << endl;
}
}
Weapon.h
#pragma once
#include <string>
using namespace std;
class Weapon
{
private:
string name;
public:
string getName();
void setName(string);
};
Weapon.cpp
#include "Weapon.h"
string Weapon::getName()
{
return name;
}
void Weapon::setName(string n)
{
name = n;
}
Player.h
#pragma once
#include "Weapon.h"
class Player
{
private:
Weapon weapon;
public:
Weapon getWeapon();
void setWeapon(Weapon);
};
Player.cpp
#include "Player.h"
Weapon Player::getWeapon()
{
return weapon;
}
void Player::setWeapon(Weapon w)
{
weapon = w;
}
Weapon Player::getWeapon()
You return a copy and not a reference of the weapon, so any change to the copy does not affect the original.
For return a reference, use & operator:
Weapon& Player::getWeapon()
{
return this->weapon;
}
Player::getWeapon() returns a copy of the weapon every time instead of a reference to the weapon. Changing the name in the copy changes nothing in the original.

Understanding the ambiguating new declaration of function error

I don't understand this error
here is a link to view the code online:
https://onlinegdb.com/rkirYvU_M
I am trying to add the names of the drivers, owners, and model to vectors, and we need to use pointers and files.
Here is my main file:
#include "person.h"
#include "car.h"
#include <iostream>
#include <vector>
std::vector <Person*>people;
std::vector <Car*> cars;
int main()
{
bool done = false;
Person person;
while(! done)
{
std::cout << "\n Please enter the owners ";
Person*prompt_info();
std::cout << "\n Please enter the drivers ";
Car*prompt_info();
Car*set();
Car*print();
}
return 0;
}
Here is the person.h file:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
//using namespace std;
class Person
{
public:
Person();
std::string get_name();
int get_age();
void prompt_info();
private:
std::string name;
int age;
};
#endif
Here is the person.c++ file:
#include "person.h"
Person::Person()
{
}
void Person::prompt_info()
{
std::cout << " name: ";
std::cin >> name;
std::cout << "enter their age: ";
std::cin >> age;
}
std::string Person::get_name()
{
return name;
}
int Person::get_age()
{
return age;
}
Here is the car.h file:
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
#include "person.h"
using namespace std;
class Car
{
public:
Car();
std::string get_model();
Person* get_owner();
Person* get_driver();
void print();
void set(Person _owner,Person get_driver);
void prompt_info();
private:
std::string model;
Person* owner;
Person* driver;
};
#endif
I am trying to understand this error.
main.cpp:23:25: error: ambiguating new declaration of 'Car* prompt_info()'
Car*prompt_info();
^
You seem to be confusing function declarations with member functions. Just declare a Person object on the stack and call the method through it's object. Do the same for your Car object. You can use your objects like this.
while(! done)
{
Person person; ///< Person object named 'person'
Car car; ///< Car object named 'car'
std::cout << "\n Please enter the owners ";
person.prompt_info();
std::cout << "\n Please enter the drivers ";
car.prompt_info();
car.set();
car.print();
// TODO do something with your objects (store to vector?)
// next time through the loop your person and car will
// get initialized all over again
}
return 0;
You will have to store your temporary objects before they go out of scope if you want to use them later.

C++ Class object as argument in other class function

I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.