mercredi 18 janvier 2023

How to properly declare a list in a class

Context: I'm at the second year of Uni and we started using classes and understanding of creating some little applications. I'm building an employer class, every employer has 4 parameters: serial_number, name, surname, earnings and then there also junior and senior employers, with all the above but other parameters too. For example junior employers have a set of skills and intern: first one is a list of skills that a junior can have and the second is at which senior employer they are assigned. ex. Junior with serial_number3 is the intern of Senior with serial_number1

NOW: I make use of the employer.h for the easy stuff, but when I get into declaring the list skills I just didn't understand what I should do.

#ifndef JUNIOR_H
#define JUNIOR_H

#include "employer.h"

#include <list>
using namespace std;

class junior : public employer
{
public:
    junior(string serial, string name, string surname, double earns, list<string> sk, string in)
        : employer(serial, name, surname, earns), skills(sk), intern(in) {}

    list<string> getSkills() const { return skills; }
    string getIntern() const { return intern; }

private:
    list<string> skills;
    string intern;

};

#endif // JUNIOR_H

Could I simply put in the body of the constructor this?

{ skills.push_back(sk); }

Aucun commentaire:

Enregistrer un commentaire