-1

I am writing a university management system and I have total of ten classes.As we know we establish polymorhism between classes when we use virtual function in base class and then we inherit derived classes from the base class and the virtual function is overriden in derived classes but here I am facing a problem which I have never faced as I have implemented polymorphism many times in c++ but the now it gives error when I write this statement and the error is at new keyword.It can be seen in the display class

 //Using Polymorphism
    Department* department=new Faculties();

I am unable to establish polymorphism between these department and the faculties class.Similarly there is an employee class which is derived from department and same error is there I am very much confused as I am still a begginer in OOP. Department Class

#pragma once
#include"Accounts.h"
#include"Faculties.h"
#include"Employee.h"
#include<iostream>
#include<vector>
using namespace std;
#include<string>
class Department
{
public:
    Department()=default;
    ~Department()=default;
    //Declaring the methods
    
//Setters
    
    void setDepartmentID(int);
    void setDepartmentStrength(int);
    void setHODName(string);
    //Inheriting
    virtual void setName(string);
    virtual void set_id(int);
    virtual void set_address(string);
    virtual void setEmail(string);
    //Defining getters 
    virtual string getName(int);
    virtual int get_id(int);
    virtual string get_address(int);
    virtual string getEmail(int);


    //for calculating payments
    virtual void set_payments(float, string, float)=0;

    virtual void showPaymentDetails(int);

 //getters
    int getDepartmentStrength();
private:
    vector<string> departmentName;
    vector<string> departmentHOD;
    vector<int> departmentID;
    vector<int> departmentStrength;
    //Declaring an array to enter the department faculties
};

Department::Department()
{
}
Department::~Department()
{
}
void Department::setDepartmentID(int id)
{
    departmentID.push_back(id);
}
void Department::setName(string name)
{
    departmentName.push_back(name);
}
void Department::setDepartmentStrength(int strength)
{
    departmentStrength.push_back(strength);
}
void Department::setHODName(string name)
{
    departmentHOD.push_back(name);
}
int Department::getDepartmentStrength()
{
    static int sum = 0;
    for (size_t i = 0; i < departmentStrength.size(); i++)
    {
        sum = sum + departmentStrength[i];
    }
    return sum;
}

Faculties class

#pragma once
#include"Accounts.h"
#include"Department.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;


class Faculties:public Department
{
public:
    Faculties()=default;
    ~Faculties()=default;
    //////////////////////////////////Using Polymorphism///////////////////////////////////
        //Setters
    void setName(string) override;
    void set_id(int) override;
    void set_address(string) override;
    void setEmail(string) override;
    void set_payments(float, string, float);
    //Getters
    string getName(int) override;
    int get_id(int) override;
    string getEmail(int) override;
    string get_address(int) override;
    //for showing payments
    void showPaymentDetails(int) override;
private:
    vector<int> faculty_id;
    vector<string> faculty_name;
    vector<string> faculty_email;
    vector<string> faculty_address;
    vector<string> faculty_type;
    vector<string> faculty_description;
    Accounts accounts;
};

Faculties::Faculties()
{
}

Faculties::~Faculties()
{
}


void Faculties::set_id(int id)
{
    this->faculty_id.push_back(id);
}
void Faculties::setEmail(string email)
{
    this->faculty_email.push_back(email);
}
void Faculties::setName(string name)
{
    this->faculty_name.push_back(name);
}
void Faculties::set_address(string saddress)
{
    this->faculty_address.push_back(saddress);
}
string Faculties::getName(int index)
{
    return this->faculty_name[index];
}
int Faculties::get_id(int index)
{
    return this->faculty_id[index];
}
string Faculties::getEmail(int index)
{
    return this->faculty_email[index];
}
string Faculties::get_address(int index)
{
    return this->faculty_address[index];
}
void Faculties::set_payments(float decidedSalary, string payment_method, float total_working_hours)
{
    //Using composition
    accounts.set_decided_salary(decidedSalary);
    accounts.set_payment_method(payment_method);
    accounts.set_total_working_hours(total_working_hours);
}
void Faculties::showPaymentDetails(int index)
{
    cout << "\nThe decided salaray of faculty with name " << faculty_name[index] << " is " <<
        accounts.get_decided_salary(index);
    cout << "\nThe payment method of faculty with name " << faculty_name[index] << " is " <<
        accounts.get_payment_method(index);
    cout << "\nThe deducted salary of faculty with name " << faculty_name[index] << " is " <<
        accounts.get_deducted_salary(index);
    cout << "\nThe paid salary of faculty with name " << faculty_name[index] << " is " <<
        accounts.get_paid_salary(index);
    cout << "\nThe bonus salary paid of faculty with name " << faculty_name[index] << " is " <<
        accounts.get_bonus_salary(index);
}

Display class here it gives error and same in source.cpp

#pragma once
#include"Department.h"
#include"Student.h"
#include"Courses.h"
#include"SubCampuses.h"
#include"Faculties.h"
#include"Employee.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Display
{
public:
    Display()=default;
    ~Display()=default;
    void setStudentsData(string, int, string)
    {

    }


    void setStudentsData(string name, int id, string address, string email, int departmentID, int departmentStrength, string HODname);

    void setStudentsAcademicRecord(float marks, char grades, float attandancePercentage, float CGPA);

    void setStudentCourseInfo(Courses courses, string coursesEnrolled, float courseWiseAttandancePercentage, float courseWiseMarks);

    //Setting the faculties of students
    void setFacultiesInfo(string name,int id,string address,string email, float decidedSalary, string payment_method, float total_working_hours);

    //setting the employees info
    //void setEmployeesInfo(string name, int id, string address, string email, float decidedSalary, string payment_method, float total_working_hours);

    void ShowStudentCoursesInfo();

    void ShowStudentRecord(int);

    void ShowFacultiesDetails(int);
    
    //void ShowEmployeesDetails(int);

private:
    //Using Inheritance

    Student *student;

    Courses courses;

    Faculties faculties;

    //Using Polymorphism
    Department* department=new Faculties(); ///////////////////Here is the error ////////////////////////

    

};

Display::Display()
{
}

Display::~Display()
{
}
void Display::setStudentsAcademicRecord(float marks, char grades, float attandancePercentage, float CGPA)
{
    student->set_student_marks(marks);
    student->set_student_grades(grades);
    student->set_Attandance_percentage(attandancePercentage);
    student->set_GCPA(CGPA);
}
void Display::setStudentsData(string name, int id, string address, string email,int departmentID,int departmentStrength,string HODname)
{
    student->setName(name);
    student->set_id(id);
    student->set_address(address);
    student->setEmail(email);

} 
void Display::ShowStudentRecord(int id)
{
    cout<<"\nThe name of student is equal to "<<student->getName(id);
    cout << "\nThe Id of student is equal to " << student->get_id(id);
    cout << "\nThe Email of student is equal to " << student->getEmail(id);
    cout << "\nThe Adress of student is equal to " << student->get_address(id);

    //Now showing the student academic records
    student->ShowStudentAcademicRecord(id);

}
void Display::setStudentCourseInfo(Courses courses, string coursesEnrolled, float courseWiseAttandancePercentage, float courseWiseMarks)
{
    courses.set_courses_enrolled(coursesEnrolled);
    courses.set_coursewise_attendance_percentage(courseWiseAttandancePercentage);
    courses.set_course_wise_marks(courseWiseMarks);
}
void Display::ShowStudentCoursesInfo()
{
    //Passing the courses object to the courses info
    student->ShowStudentCoursesInfo(courses);
}
void Display::setFacultiesInfo(string name, int id, string address, string email, float decidedSalary, string payment_method, float total_working_hours)
{
    department->setName(name);
    department->set_id(id);
    department->set_address(address);
    department->setEmail(email);
    department->set_payments(decidedSalary, payment_method, total_working_hours);
}
void Display::ShowFacultiesDetails(int id)
{
    cout<<"\nThe id of department faculty is "<<department->get_id(id);
    cout << "\nThe Name of department faculty is " << department->getName(id);
    cout << "\nThe Email of department faculty is " << department->getEmail(id);
    cout << "\nThe Address of department faculty is " << department->get_address(id);
    cout << "\nThe Department faculty strength is " << department->getDepartmentStrength();
}

The screen shot of the error is below The error picture The complete program is at the following link https://github.com/Muhammad-Bilal-7896/Univeristy-Management-System

  • 1
    make department destructor virtual. The reason ppl downvote you is that you didn't post a minimal reproductible example. Also rather quote your error in your post than give link to screenshot – user Sep 09 '20 at 15:08
  • @user Thank you sir I am trying your advice –  Sep 09 '20 at 15:10
  • @user Sir I have tried your solution but still error is same I wrote ```Department()=default; virtual ~Department(); //Declaring the methods``` –  Sep 09 '20 at 15:13
  • 2
    You have circular includes. The base class shouldn't need to know anything about the derived class(es). – 1201ProgramAlarm Sep 09 '20 at 15:18
  • @1201ProgramAlarm Thank you very much my problem is resolved and the error has gone –  Sep 09 '20 at 15:23
  • In this case the solution probably should be to remove `#include"Accounts.h" #include"Faculties.h" #include"Employee.h"` from `Department.h` – drescherjm Sep 09 '20 at 15:35

1 Answers1

0

As @1201ProgramAlarm included in the comments you are including circular inlcudes which can be annoying to the compiler and the program fails to be compiled so remove those unnecessary header files.Also make the destructor virtual of the department class.Make sure that only destructors are virtual and not the constructors.

bilalmohib
  • 280
  • 3
  • 16