-3

I have a class called University. I wish to create objects of class Department, and the class University has a vector of type Department. My program is to read a file, and based on the command given, call different functions. My program is to make multiple objects of class Department and put them into the vector named Departments in the University class.

I do not understand how to make multiple objects of type Department with different names.

bool University::CreateNewDepartment(string depName, string depLoc, long depChairId)
{
if (depChairId == 0)
//Department ___(depName, depLoc, depChairId);
//Departments.pushback(___)
return true;
}

___ is a placeholder for the name of the Department objects being created. How do I make it so there is a different name each time it is created? Thank you.

  • What do you think happens with your automatic variable when you leave its scope? – Deduplicator Nov 17 '15 at 02:59
  • Are you referring to depChairId? Then it idoesn't create the object at all? – Joshua Manero Nov 17 '15 at 03:03
  • To clarify, what harm would there be to have the same name, according to your current understanding? –  Nov 17 '15 at 03:36
  • if we used the name test for ex. I'm confused how I would change a specific variable in the object like `test.location` – Joshua Manero Nov 17 '15 at 03:38
  • No, "I'm confused how..." is a *confusion*, not a *harm* or *understanding*. What is the harm according to your current understanding? –  Nov 17 '15 at 03:42
  • the harm as I see it is that if the objects had the same name, I wouldn't know how to call each individual object – Joshua Manero Nov 17 '15 at 03:44
  • Let me use @Deduplicator's question to find out the base of your current understanding. What will happen to local variable `___` when the program leaves the scope of `___`? –  Nov 17 '15 at 03:56
  • http://stackoverflow.com/questions/11137516/scope-vs-lifetime-of-variable – Deduplicator Nov 17 '15 at 04:52

2 Answers2

1

You are mixing what are variable names with data (which is what is contained in such variables).

A variable name is something that doesn't mean anything, it's just used to refer to a specific placeholder somewhere in your code, while data is what you usually modify.

So:

Department department = Department(depName, location, chairID);
departments.push_back(department);

is perfectly fine. department is just a local name to the department that is being created inside the function. depName is another variable, that will contain the real name, which is a std::string (eg "Nice Department") and it's the real data.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Let's say I created a second object with the name department. That doesn't overwrite the previous object? And how would I modify the values of the object later like for example `department.location` if they had the same name? – Joshua Manero Nov 17 '15 at 03:06
  • @JoshuaManero I honestly have no idea how to clear up all your misunderstandings with something fitiing in an answer ... please, keep reading your book (or what you use to learn). The difference between value and name of a (one of the) variable(s) with it, what scope means, what a map is... – deviantfan Nov 17 '15 at 03:10
  • @deviantfan I've came here as a last resort, after looking through my textbook and through lecture notes. I'm confused as to how I can make multiple objects with the same name, and then modify them later. Perhaps making the object, placing it in a vector, and then referring to it by the spot in the vector? `departments[0]` for ex? – Joshua Manero Nov 17 '15 at 03:14
0

Define Department something like this:

class Department 
{
public:
    Department(const std::string& name, const std::string& location, long chairId) 
        : name_(name)
        , location_(location)
        , chairId_(chairId)
    {
    }

    // probably want accessors to get the variables ... 

private:
    std::string name_;
    std::string location_;
    long chairId_;
};

Then in your University::CreateNewDepartment do

departments.push_back(Department(depName, depLoc, depChairId));

Your University class would need to have a std::vector<Department> member named departments, etc.

oz10
  • 153,307
  • 27
  • 93
  • 128