-3
#include <iostream>
using namespace std;

class Professor
{
    string name;
    long employeeID;
    string designation;

public:
    Professor()
    {
        name = "";
        employeeID = 0;
        designation = "";
    }

    Professor(string n, long ID, string d)
    {
        name = n;
        employeeID = ID;
        designation = d;
    }

    void setProfessorData(string name1, long ID1,string d1)
    {
        name = name1;
        employeeID = ID1;
        designation = d1;
    }

    string getName()
    {
        return name;
    }

    long getID()
    {
        return employeeID;
    }

    string getDesignation()
    {
        return designation;
    }
};

class Department
{
private:
    string name;
    long deptID;
    Professor profList[5];
    int noOfprofessors;

public:
    Department()
    {
        name = "";
        deptID = 0;
        for (int i = 0; i < 5; i++)
        {
            profList[i].setProfessorData ("",0,"");
        }
        noOfprofessors = 0;
    }

    Department(string name1, long id1, Professor array[5], int no_of_dpt)
    {
        name = name1;
        deptID = id1;
        for (int i = 0; i < 5; i++)
        {
            profList[i] = array[i];
        }
        noOfprofessors = no_of_dpt;
    }

    void setDepartmentData(string n, long i, Professor arr[5], int  nd)
    {
        name = n;
        deptID = i;
        for (int i = 0; i < 5; i++)
        {
            profList[i] = arr[i];
        }
        noOfprofessors = nd;
    }

    string getName1()
    {
        return name;
    }

    long getDeptId()
    {
        return deptID;
    }

    int getnoOfProfessors()
    {
        return noOfprofessors;
    }
};

class University
{
private:
    string name;
    Department dept[5];
    int numberOfDepartments;

public:
    University(string n, Department array[5], int no)
    {
        name = n;
        for (int i = 0; i > 5; i++)
        {
            dept[i] = array[i];
        }
        numberOfDepartments = no;
    }

    void setUniversityData(string name1, Department arr[5], int n1)
    {
        name = name1;
        for (int i = 0; i < 5; i++)
        {
            dept[i] = arr[i];
        }
        numberOfDepartments = n1;
    }

    bool addDepartment(Department D)
    {
    }

    bool deleteDepartment(string name)
    {
    }

    bool updateDepartment(int id, string name)
    {
    }
};

How to add, delete, and update Department in University class?

I have provided the skeleton code. I have implemented all constructors and destructors, but I don't know how to implement addDepartment(), deleteDepartment(), and updateDepartment()`. Kindly look into this and help me to complete this task.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    You should really take a shot at solving this yourself and asking a targeted question if you hit a problem you cannot solve. – user4581301 Apr 26 '22 at 22:28
  • @user4581301 Can you provide me brief algorithm to solve this task?? – Mudassir Waheed Apr 26 '22 at 22:31
  • Can you add departments to exceed the hard coded limit of 5? If so, this is where you need to start. Replace `Department dept[5];` with `std::vector dept;` and add departments with the `std::vector::push_back` method. – user4581301 Apr 26 '22 at 22:36
  • @user4581301 But I am not allowed to use vectors to solve this problem. – Mudassir Waheed Apr 26 '22 at 22:37
  • Update the question with that bit of information. It is very important. "You can't use `vector`" is often secret teacher code for "You won't get full marks if you don't write your own version of `vector`." You'll find many questions already on Stack Overflow about how to create a simple `vector`. – user4581301 Apr 26 '22 at 22:39
  • 2
    @ShahidJarral How do you NOT know how to implement these functions? You seem to already know how to work with arrays. You already have methods that are "adding"/"updating" array elements, so just apply similar logic to your `addDepartment()` and `updateDepartment()` accordingly. As for `deleteDepartment()`, "deleting" an array element is just a matter of shifting subsequent elements down 1 slot and then decrementing your array counter. Basics that [any good C++ book](https://stackoverflow.com/questions/388242/) should teach you. – Remy Lebeau Apr 26 '22 at 22:49
  • A few suggestions: use initializer lists in your constructors when possible. Use default values for arguments rather than defining your constructor twice. Mark any "getters" as `const`. – Chris Apr 26 '22 at 22:50
  • *I am not allowed to use vectors to solve this problem.* Most unfortunate. That's a clue that you are using C++ to learn C. – Eljay Apr 26 '22 at 22:53
  • If you are having difficulty adding, updating and deleting elements of an array, then I advise you to practice with something simple, like an array of `int`. Once you see how these functions work in the abstract, you will find this specific problem easy. – Beta Apr 26 '22 at 22:53
  • I wonder how the instructor would feel about using `std::array`. – Chris Apr 26 '22 at 22:54

1 Answers1

0

First off, several of your for loops are incorrect, namely the ones in the following methods:

  • Department::Department(string, long, Professor[5], int), should be using no_of_dpt (or better, std::min(no_of_dpt, 5)) instead of 5 for the loop counter.

  • Department::setDepartmentData(), should be using nd (or better, std::min(nd, 5)) instead of 5 for the loop counter.

  • University::University(string, Department[5], int), should be using no (or better, std::min(no, 5)) instead of 5 for the loop counter. Also, the loop needs to use < instead of >.

  • University::setUniversityData(), should be using n1 (or better, std::min(n1, 5)) instead of 5 for the loop counter.

That being said, you already have basic logic for adding elements to arrays, so you can implement addDepartment() by applying that logic correctly, eg:

bool addDepartment(Department D)
{
    if (numberOfDepartments < 5)
    {
        dept[numberOfDepartments] = D;
        ++numberOfDepartments;
    }
}

And, you can easily implement deleteDepartment(), you just need to find the index of the desired Department and shift the remaining departments down 1 element in the array, eg:

bool deleteDepartment(string name)
{
    for (int i = 0; i < numberOfDepartments; ++i)
    {
        if (dept[i].getName1() == name)
        {
            for(int j = i+1; j < numberOfDepartments; ++j)
            {
                dept[j-1] = dept[j];
            }
            --numberOfDepartments;
            dept[numberOfDepartments].setDepartmentData("", 0, NULL, 0);
            break;
        }
    }
}

Unfortunately, you cannot implement updateDepartment() with the current code you have shown. This is because University does not have access to update the Department::name field directly, and it does not have access to a Department's existing professor data in order to call Department::setDepartmentData() with just a new name.

So, you will have to fix this issue first, either by making University be a friend of Department, or by adding a Department::setName() setter, or by adding getters for the data in the Department::profList array.

However, once you have addressed that, you can then implement updateDepartment(), eg:

class Department
{
private:
    string name;
    ...

    friend class University;

public:
    ...
};

class University
{
private:
    ...

public:
    ...

    bool updateDepartment(int id, string newName)
    {
        for (int i = 0; i < numberOfDepartments; ++i)
        {
            if (dept[i].getDeptId() == id)
            {
                dept[i].name = newName;
                break;
            }
        }
    }
};

Or:

class Department
{
private:
    string name;
    ...

public:
    ...

    void setName(string newName)
    {
        name = newName;
    }
};

class University
{
private:
    ...

public:
    ...

    bool updateDepartment(int id, string newName)
    {
        for (int i = 0; i < numberOfDepartments; ++i)
        {
            if (dept[i].getDeptId() == id)
            {
                dept[i].setName(newName);
                break;
            }
        }
    }
};

Or:

class Department
{
private:
    ...
    Professor profList[5];
    int noOfprofessors;

public:
    ...

    Professor* getProfessors()
    {
        return profList;
    }

    int getnoOfProfessors()
    {
        return noOfprofessors;
    }
};

class University
{
private:
    ...

public:
    ...
    bool updateDepartment(int id, string newName)
    {
        for (int i = 0; i < numberOfDepartments; ++i)
        {
            if (dept[i].getDeptId() == id)
            {
                dept[i].setDepartmentData(newName, dept[i].getDeptId(), dept[i].getProfessors(), dept[i].getnoOfProfessors());
                break;
            }
        }
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `dept[numberOfDepartments] = D; ++numberOfDepartments;` might just be `dept[numberOfDepartments++] = D;` The OP has to learn about pre and post inc/decrement operators eventually. – Chris Apr 27 '22 at 00:28
  • @Chris If the assignment throws an exception, `numberOfDepartments` would be out of sync. Better to increment it after the assignment is finished. – Remy Lebeau Apr 27 '22 at 01:06
  • Given that you've already checked that `numberOfDepartments` is less than `5`, it shouldn't. – Chris Apr 27 '22 at 01:26
  • @Chris the `Department` assignment operator is *unlikely* to throw an exception, but it *can*, ie copying strings that can't allocate memory. – Remy Lebeau Apr 27 '22 at 02:13