MeIsNoob
MeIsNoob

Reputation: 69

Is it possible for a friend function to create new object? C++

Hello I am trying to make little simulation of school system.

I am wondering if I have 2 classes: 1: Principal 2: Teacher then is it possible for method from class Principal to create new object of class Teacher?

Here's what I would like to do (method AddTeacher):

class PRINCIPAL
{
    char* name;
    int wage;
    unsigned long long id;
public:
    void showinfo() const;
    PRINCIPAL();//constructor, but there will be overloaded constructors in this class;
    ~PRINCIPAL();
        void addTeacher(char* nameofobject,char* name,char* position,int wage, unsigned long long id);
    void removeTeacher(TEACHER& teacher); //method which will remove object of class TEACHER;
    void changeTEACHERSsalary(TEACHER& teacher);
    void changeTEACHERSposition(TEACHER& teacher);
}

class TEACHER
{
    char* name;
    char* position;
    int wage;
    unsigned long long id;
    friend class PRINCIPAL;
public:
    TEACHER(char* name,char* position,int wage, unsigned long long id);
    ~TEACHER();
    void gradeStudent(STUDENT& s,int grade,char* subject);
    void changestudentsgrade(STUDENT& s,char* subjectname,int oldgrade,int newgrade);
    void ViewStudentsAverageFromSubject(STUDENT& s, char* subjectname) const;
    void ViewStudentsAverage(STUDENT& s) const; // average from all subjects;
    void ViewClassAverage(KLASA& k) const; // average from all subjects of whole object KLASA(class);
    void ViewClassAverageSubject(KLASA& k,char* subjectname) const; // average from selected subject of whole object KLASA(class);
}

Also I would like to ask if its possible to do the following: I would like to do constraint which will allow only one object of class Principal to exist. If its possible could you explain me how to do it?

Thank you in advance.

P.S addTeacher would look like this:

void addTeacher(char* nameofobject,char* name,char* position,int wage, unsigned long long id){
TEACHER nameofobject(char* name,char* position,int wage, unsigned long long id);
}

Upvotes: 0

Views: 278

Answers (1)

David Hammen
David Hammen

Reputation: 33126

What is the principal going to add a teacher to, remove a teacher from? You need a collection of teachers somewhere. You are missing a key concept, that of a SCHOOL. A school has-a principal, presumably just one, so the principal member could be as simple as PRINCIPAL principal;

A school also has-a collection of teachers. This is screaming for one of the C++ template library container class templates. This gives you something that the principal object can easily add teachers to / remove teachers from.

Upvotes: 1

Related Questions