simplycoding
simplycoding

Reputation: 2977

C++ Calling Private Data Members in a Vector Class

I'm looking to create a class that's in the form of a standard vector. I've been writing some programs with classes that have implemented Sets and not vectors, so I'm a little confused.

Here's my class:

class Employee
{

private:

  Struct Data
  {
unsigned Identification_Number;
// Current capacity of the set

unsigned Department_Code;
// Department Code of employee

unsigned Salary;
// Salary of employee

str Name;
// Name of employee
  }

If I want to call the private data members later on, can I just do the following?

vector<Employee> Example;

//Assume there's data in Example

cout << Example[0].Identification_Number;
cout << Example[3].Salary;

If not, what would be an apprpriate container? Would a list of a list be better for handling this set of data?

Upvotes: 2

Views: 1173

Answers (3)

ctor
ctor

Reputation: 6138

Assuming Struct is a typo.

You could make the Data struct in Employee anonymous by removing the name of the struct. This will allow you to access the data directly with Example[0].Identification_Number however for this to work properly you would also have to make the struct public.

Another option is to remove the struct completely and store the data directly as members of the Employee class.

A 3rd option would be to add const accessor methods to return the data from the struct.

Upvotes: 0

Cornstalks
Cornstalks

Reputation: 38238

A common way is accessor functions:

#include <iostream>

class Employee
{
public:
    void setID(unsigned id)
    {
        Identificaiton_Number = id;
    }

    unsigned getID()
    {
        return Identificaiton_Number;
    }

private:
    unsigned Identification_Number;
    // Current capacity of the set

    unsigned Department_Code;
    // Department Code of employee

    unsigned Salary;
    // Salary of employee

    str Name;
    // Name of employee
};

int main()
{
    Employee e;

    e.setID(5);
    std::cout << e.getID() << std::endl;
}

Some argue that if you have getter/setter accessors, you might as well make the member public. Others argue it's better to have getter/setter accessors, because it allows you to enforce invariants/constraints or change various implementation details.

As for accessing private members: you shouldn't do it. It's technically possible, but don't do it.

Upvotes: 0

In silico
In silico

Reputation: 52217

It is not possible with the code you provide as-is, but with a few modifications you can make it work:

class Employee
{
public:
    unsigned GetID() const               { return Identification_Number; }
    unsigned GetDepartment() const       { return Department_Code; }
    unsigned GetSalary() const           { return Salary; }
    // Assuming you're using std::string for strings
    const std::string& GetString() const { return string; }
private:
    unsigned Identification_Number; // Current capacity of the set
    unsigned Department_Code;       // Department Code of employee
    unsigned Salary;                // Salary of employee
    string Name;                    // Name of employee
};

Note that the Data structure is totally superfluous in this case as you've presented. I've just placed all the data members within the Employee class itself as private data members for encapsulation.

Then you can access them this way:

std::vector<Employee> Example; //Assume there's data in Example
// ...
cout << Example[0].GetID();
cout << Example[3].GetSalary();

Presumably you will set the individual variables to their correct values within the Employee class somehow.

Upvotes: 1

Related Questions