Peter
Peter

Reputation: 3

same function but different parameter passing to function

struct student {

    char            *s_name;

    struct student_id   s_id;

    /** Number of references to this student. */
    unsigned int         s_ref;

    /** Transcript (singly-linked list, NULL terminator). */
    struct transcript_entry *s_transcript;

    /** Whether or not this student has completed his/her program. */
    student_complete     s_complete;
};

struct student* student_grad_create(const char *name, size_t namelen,
    struct student_id, int phd);

struct student* student_undergrad_create(const char *name, size_t namelen,
    struct student_id);

There are three kinds of student, master student and phd student and undergrad student.I need to implement a function that called:

int student_add_entry(struct student *, struct transcript_entry *);

I dont know how I can determine the student type?

should I do the following?

int student_add_entry(struct student *undergrad_create, struct transcript_entry *){}
int student_add_entry(struct student *grad_create, struct transcript_entry *){}

thanks.

Upvotes: 0

Views: 80

Answers (2)

Peter
Peter

Reputation: 3

I am working on some student registration system problem. 1. there are three different kinds of student types 2. the undergrad student pass the exam with 50, and master and phd pass with 65 3. Undergraduates must complete 40 courses, MEng students 5 and PhD students 2.

These are the three functionalities I need to implement. ' Below it is the struct for the transcript_entry:

struct transcript_entry {
    enum faculty         te_faculty;
    unsigned int         te_number;
    unsigned int         te_grade;
    /** Number of references to this entry. */
    unsigned int         te_ref;
    /** Next entry in the singly-linked list (or NULL terminator). */
    struct transcript_entry *te_next;
};

I implement the following functions:

struct transcript_entry* transcript_entry(enum faculty faculty, unsigned int course_number, unsigned int grade){
    struct transcript_entry *trans_entry;
    trans_entry = malloc(sizeof(struct transcript_entry));
    trans_entry->te_faculty = faculty;
    trans_entry->te_number = course_number;
    trans_entry->te_grade = grade;
    trans_entry->te_ref = 1;
    trans_entry->te_next = NULL;
    return trans_entry;
}

/** Deallocate a @ref transcript_entry. */
void    transcript_entry_free(struct transcript_entry *trans_entry){
    free(trans_entry);
}

/** Increment a @ref transcript_entry's refcount. */
void    tehold(struct transcript_entry *trans_entry){
    trans_entry-> te_ref++;
}

I looked some tutorials about the reference counting. I dont know my code is make sense or not. So is that mean increment the reference counting ++, or decrement by te_ref--;

Upvotes: 0

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

You can add a student_type field to the struct

struct student {
    char                    *s_name;
    struct student_id        s_id;
    /** Number of references to this student. */
    unsigned int             s_ref;
    /** Transcript (singly-linked list, NULL terminator). */
    struct transcript_entry *s_transcript;
    /** Whether or not this student has completed his/her program. */
    student_complete         s_complete;
    enum student_type        s_type;
};

and you would have an enum like

enum student_type
{
    UndergraduateStudent,
    MasterStudent,
    PHDStudent
};

when creating an instance of struct student you would set the s_type field, and then use s_type everywhere else to determine the type of student.

I would also write a generic function to create an instance of struct student passign to it all possible parameters, and if you want for convinience you could also create a function for each specific type, where you might pass default parameters to the generic function.

Upvotes: 2

Related Questions