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;
};
Related
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;
So, I've got a class Car:
car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string.h>
#include "car.cpp"
// Car class with its attributes
class Car {
public:
std::string brand;
std::string model;
int year;
// Constructor
Car(int year, std::string model, std::string brand);
};
#endif
and I wanted to make a class constructor definition outside the class in another .cpp file:
car.cpp
#include <string.h>
Car::Car(int year, std::string model, std::string brand)
{
this->brand = brand;
this->model = model;
this->year = year;
}
I tried to compile, but this error has occurred:
car.cpp:3:1: error: ‘Car’ does not name a type
Why it happened and how to fix it?
My main.cpp:
#include <iostream>
#include "car.h"
using namespace std;
int main() {
// Create an object of Car
Car carObj1 = Car(1992, "model X", "Brand1");
// Create another object of Car
Car carObj2 = Car(2003, "model Y", "Brand2");
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
You got the includes the wrong way round. car.cpp should #include "car.h" not the other way around.
Also the correct header file for std::string is <string> not <string.h>
Also member initialisation is better done with initialiser lists not assignment
Car::Car(int year, std::string model, std::string brand) :
brand(brand), model(model), year(year)
{
}
#include "car.cpp"
This is wrong. Never include source files.
‘Car’ does not name a type
Why it happened
car.cpp attempts to use the class Car which has not been defined.
how to fix it?
Add #include "car.h" into car.cpp to define Car before its use. Then remove #include "car.cpp" from car.h to avoid recursive inclusion that would prevent correct order of inclusion.
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.
I'm practicing some basic C++ right now, and decided to create a class in a header file and the constructor, GetString, etc functions in a separate file.
When I create my object using
"Person Bob" and use "." the code works fine, but if I do Person* Bob, the SetName(x) function seg faults, when I use ->SetName(x, with x being a "abc" string or a string variable
Main.cpp
#include <iostream>
#include <string>
#include "namevalue.h"
using namespace std;
int main(){
Person Bob;
string temp = "bob";
Bob.SetName(temp);
Bob.SetMoney(3000);
cout << Bob.GetName() << " " << Bob.GetMoney() << endl;
return 0;
}
Person.h
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Person{
public:
Person();
Person(int money, string name);
void SetName(string y);
void SetMoney(int x);
int GetMoney();
string GetName();
private:
int money;
string name;
};
Person.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include "namevalue.h"
using namespace std;
Person::Person(){
name = " ";
money = 0;
}
Person::Person(int x, string y){
SetName(y);
SetMoney(x);
}
void Person::SetMoney(int x){
money = x;
}
void Person::SetName(string x){
name = x;
}
int Person::GetMoney(){
return money;
}
string Person::GetName(){
return name;
}
If you declare a pointer variable, you need to populate it first with a valid instance. Otherwise, it is pointing to invalid memory and you will get the memory fault you are experiencing.
This should work.
Person* Bob = new Person();
Bob->SetName("Bob");
Bob->SetMoney(3000);
When you're finished, free the memory.
delete Bob;
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.