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;
}