1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <string>
class Student;
class Department
{
std::string name;
public:
Department(std::string Name)
{
std::cout << "Department::ctor: " << Name << std::endl;
name = Name;
}
std::string Name() const
{
return(name);
}
~Department()
{
std::cout << "Department::dtor: " << name << std::endl;
}
};
class Student
{
std::string name;
public:
Student(std::string Name)
{
std::cout << "Student::ctor: " << name << std::endl;;
name = Name;
}
std::string Name()const
{
return(name);
}
~Student()
{
std::cout << "Student::dtor "<< name << std::endl;
};
};
class Course
{
Student * std_p;
Department * dept_p;
std::string coursename;
static unsigned int index;
static Course *courseList[4];
public:
Course(std::string crseName, Student* student, Department* dept)
: coursename(crseName), std_p(student), dept_p(dept)
{
std::cout << "Course:ctor: " << coursename << std::endl;
if (index < 4)
{
courseList[index] = this;
++index;
}
else
{
std::cout <<"Course list full" << std::endl;
}
};
~Course()
{
std::cout << "Course:dtor: " << coursename << std::endl;
};
static std::string findStudent(char *crseName, char* deptName)
{
for(int i=0; i<index; i++)
{
if ( (courseList[i]->getCourseName() == crseName) &&
(courseList[i]->getDeptName() == deptName) )
{
return(courseList[i]->getStdName());
}
}
}
std::string getStdName()const
{
return(std_p->Name());
};
std::string getDeptName() const
{
return(dept_p->Name());
};
std::string getCourseName()const
{
return(coursename);
};
};
unsigned int Course::index =0;Course* Course::courseList[4] = {0,0,0,0};
int main()
{
int i;
std::cout<<"\nExample of Association class...\n";
std::cout<<"-----------------------------------\n\n";
std::cout<<"We have got 4 students ...\n";
Student *studentNames[4] = {new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;
std::cout<<"\n";
std::cout<<"We have got 2 Departments...\n";
Department *departNames[2] = {new Department("Mathemetics"), new Department("ComputerSceince")} ;
std::cout<<"\n";
std::cout<<"Here class Course Associates Student and Department, with a Course name ...\n";
Course course1("DataStructure",studentNames[0], departNames[1]);
Course course2("Maths",studentNames[3], departNames[0]);
Course course3("Geometry",studentNames[2], departNames[0]);
Course course4("CA",studentNames[1], departNames[1]);
std::cout<<"\n";
std::cout<<"Finding a Student using Course and Department...\n";
std::cout<<"Student who has taken Maths Course in Mathemetics Department is:"<<Course::findStudent("Maths", "Mathemetics")<<std::endl;
std::cout<<"\n";
std::cout<<"Deletion of objects...\n\n";
for(i=0; i<4; ++i)
{
delete studentNames[i];
}
std::cout<<"\n";
for(i=0; i<2; ++i)
{
delete departNames[i];
}
std::cout<<"\n";
return(0);
}
|