-4
\$\begingroup\$

The idea is to create a Student object which contains object of type Course, Department, Hostel.
My problem is how to create a single object with multiple courses under one student.

Below is my code:

#include <iostream>
#include <cstdlib>

typedef struct node{
    char* value;
    struct node * next;
}node;

node* addnode(node* head,char* value){
    node *ptr=new node;
    ptr->value=value;
    if(head)
        ptr->next=head;
    else
        ptr->next=NULL;
    head=ptr;
    return head;
}

void displayLinkedList(node* head){
    node* ptr=head;
    while(ptr){
        cout<<ptr->value<<"\n";
        ptr=ptr->next;
    }
}


class Entity{
    public:
        virtual char* name()=0;
        virtual node* studentList()=0;
};

class Hostel:public Entity
{
        char* entyName;
        char* hstName;
        static node* studList;
    public:
        Hostel(char* hstName,char* studName){
            this->entyName="Hostel";
            this->hstName=hstName;
            studList=addnode(studList,studName);
        }

        char* name(){
            return this->entyName;
        }

        char* hstname(){
            return this->hstName;
        }

        node* studentList(){
            return this->studList;
        }
        void display(){
            cout<<this->hstName;
            displayLinkedList(studList);
        }
};

node* Hostel::studList = NULL;

class Department:public Entity
{
        char* entyName;
        char* deptName;
        static node* studList;
    public:
        Department(char* deptName,char* studName){
            this->entyName="Department";
            this->deptName=deptName;
            studList=addnode(studList,studName);
        }

        char* name(){
            return this->entyName;
        }

        char* deptname(){
            return this->deptName;
        }

        node* studentList(){
            return this->studList;
        }
        void display(){
            cout<<this->deptName;
            displayLinkedList(studList);
        }

};
node* Department::studList = NULL;

class Course:public Entity
{
        char* entyName;
        char* courseName;
        static node* studList;
    public:
        Course(char* crsName,char* studName){
            this->entyName="Hostel";
            this->courseName=crsName;
            studList=addnode(studList,studName);
        }

        char* name(){
            return this->entyName;
        }

        char* coursename(){
            return this->courseName;
        }

        node* studentList(){
            return this->studList;
        }
        void display(){
            cout<<this->courseName;
            displayLinkedList(studList);
        }

};
node* Course::studList = NULL;

class Student{
        char* stName;
        char* entNo;
        Hostel* htName;
        Department* deptName;
        static node* cl;
    public:
        Student(char* entNo,char* stName,Department* deptName,Hostel 
*htName,char* crs){
                this->stName=stName;
                this->entNo=entNo;
                this->htName=htName;
                this->deptName=deptName;
                this->cl=addnode(cl,crs);
        }
        char* name(){
            return this->stName;
        }
        char* entryNo(){
            return this->entNo;
        }
        Hostel* hostel(){
            return this->htName;
        }
        Department* department(){
            return this->deptName;
        }
        node* courseList(){
            return this->cl;
        }
        void display(){
        cout<<this->entNo<<"\n";
        cout<<this->stName<<"\n";
        cout<<this->deptName->deptname();
        cout<<"\n";
        cout<<this->htName->hstname();
        cout<<"\n";
        displayLinkedList(cl);
        }
};
node* Student::cl=NULL;


int main()
{   Course c1("DS","Ajay");
    Course cl("CS","Ajay");
    Hostel h1("Himadri","Ajay");
    Department d1("IT","Ajay");
    Student st1("2014an1540","Ajay",&d1,&h1,"DS");
    Student st2("2014an1540","Ajay",&d1,&h1,"CS");
    st1.display();
    return 0;
}
\$\endgroup\$
3
  • \$\begingroup\$ Please see What to do when someone answers. I have rolled back Rev 4 → 2. \$\endgroup\$ Feb 16, 2018 at 19:45
  • \$\begingroup\$ @200_success I am new to this community. \$\endgroup\$
    – katty
    Feb 17, 2018 at 2:08
  • \$\begingroup\$ @Incomputable please check my code I have made some changes \$\endgroup\$
    – katty
    Feb 18, 2018 at 3:59

1 Answer 1

3
\$\begingroup\$

I'm not sure I understand what you mean by

My problem is I am getting confused with the notations so please let me know what changes can be made.

...but three items came to my mind while reading the code:

  1. Try to avoid using raw C strings in favor of std::string
  2. Don't reinvent singly-linked lists using a node structure, use any of the standard container templates like std::list instead.
  3. The Entity base type doesn't seem to be needed since there's no place where you're accessing Entity values polymorphically, so you could remove it for now.
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.