Untitled

 avatar
unknown
plain_text
3 years ago
6.1 kB
5
Indexable


// File Name: Students.cpp

#include <iostream>
#include <string>
using namespace std;

// Defines class Student
class Student
{
// Data members to store student information
string name;
string id;
string level;
public:
// Default constructor to assign default values to data members
Student()
{
name = id = level = "";
}

// Parameterized constructor to assign parameter values to data members
Student(string name, string id, string level)
{
this->name = name;
this->id = id;
this->level = level;
}

// Getter functions
string getName()
{
return name;
}
string getID()
{
return id;
}
string getLevel()
{
return level;
}

// Setter functions
void setName(string name)
{
this->name = name;
}
void setID(string id)
{
this->id = id;
}
void setLevel(string level)
{
this->level = level;
}

// Function to display student information
void showStudent()
{
cout<<"\n Student ID: "<<id<<"\n Name: "<<name<<"\n Level: "<<level;
}
};// End of class Student

// Defines class Teacher
class Teacher
{
// Data members to store teacher information
string name;
string specialty;
public:
// Default constructor to assign default values to data members
Teacher()
{
name = specialty = "";
}

// Parameterized constructor to assign parameter values to data members
Teacher(string name, string specialty)
{
this->name = name;
this->specialty = specialty;
}

// Getter functions
string getName()
{
return name;
}
string getSpecialty()
{
return specialty;
}

// Setter functions
void setName(string name)
{
this->name = name;
}
void setSpecialty(string specialty)
{
this->specialty = specialty;
}

// Function to display teacher information
void showTeacher()
{
cout<<"\n Teacher Name: "<<name<<"\n Specialty: "<<specialty;
}
};// End of class Teacher

// Defines sub class TA derived from base class Teacher
class TA : public Teacher
{
// Creates an object of class Teacher
Teacher supervisor;
public:
// Default constructor
TA()
{
supervisor = Teacher();
}

// Parameterized constructor
TA(Teacher supervisor) : Teacher(supervisor.getName(), supervisor.getSpecialty())
{
this->supervisor = supervisor;
}

// Function to return teacher
Teacher getSupervisor()
{
return supervisor;
}

// Function to display TA teacher information
void show()
{
supervisor.showTeacher();
}
};// End of class TA

// Defines class CourseClass
class CourseClass
{
// Data members to store course information
string courseName;
int maxStud;
int nbStud;
// Creates a pointer of type student
Student *stud;
// Creates an object of class Teacher for course teacher
Teacher courseTeacher;
// Creates an object of class TA for lab teacher
TA labTA;
public:

// Parameterized constructor
CourseClass(string courseName, Teacher courseTeacher, TA labTA)
{
this->courseName = courseName;
maxStud = 10;
// Creates the array of size maxStud
stud = new Student[maxStud];
nbStud = 0;
this->courseTeacher = courseTeacher;
this->labTA = labTA;
}

// Overloads ++ operator to add a student
void operator += (Student s)
{
// Checks if current number of students is equals to maximum number of students
// then displays error message
if(nbStud == maxStud)
cout<<"\n No student can be added. Full.";

// Otherwise not reached maximum limit
else
// Assigns the parameter Student object to nbStud index position
// increase the counter by one
stud[nbStud++] = s;
}

// Function to search the parameter student name in the student list
// If found then display the student information
// Otherwise display error message
void find(string studentName)
{
// Initially found status is -1 for not found
int found = -1;

// Loops till number of students registered
for(int c = 0; c < nbStud; c++)
{
// Checks if parameter student name is equals to current student name
if(studentName.compare(stud[c].getName()) == 0)
{
// Sets the found status to loop variable
found = c;
// Come out of the loop
break;
}
}

// Checks if found status is -1 then not found
if(found == -1)
cout<<"\n ERROR: Student: "<<studentName<<" not found.";

// Otherwise found
else
{
cout<<"\n\n Student Found \n";
// Displays the found index position student information
// by calls the function
stud[found].showStudent();
}
}

// Function to display course information
void show()
{
cout<<"\n\n ************* Course Information ************* ";
cout<<"\n Course Name: "<<courseName;
cout<<"\n Course Teacher: ";
courseTeacher.showTeacher();
cout<<"\n Lab Teacher: ";
labTA.showTeacher();
cout<<"\n Students Registered: \n";

// Loops till number of students
for(int c = 0; c < nbStud; c++)
// Calls the function to display current student information
stud[c].showStudent();
}
};// End of class CourseClass

// Defines main function
int main()
{
// To store the name to search
string name;

// Creates 3 students object using parameterized constructor
Student s1("Tanvi", "S111", "XII");
Student s2("Manvi", "S222", "XI");
Student s3("Trinav", "S333", "X");

// Creates a teacher object using parameterized constructor
Teacher t("Pyari Sahu", "Programming");

// Creates a TA teacher using parameterized constructor
TA ta(t);

// Creates a CourseClass object using parameterized constructor
CourseClass cc("Java", t, ta);

// Using += operator add a student
cc += (s1);
cc += (s2);
cc += (s3);

// Accepts a name to search
cout<<"\n Enter a student name to search: ";
cin>>name;
// Calls the function to search
cc.find(name);

// Accepts a name to search
cout<<"\n Enter a student name to search: ";
cin>>name;
// Calls the function to search
cc.find(name);

// Calls the function to display course information
cc.show();
return 0;
}

Sample Output:

Enter a student name to search: Sasmita

ERROR: Student: Sasmita not found.
Enter a student name to search: Tanvi


Student Found

Student ID: S111
Name: Tanvi
Level: XII

************* Course Information *************
Course Name: Java
Course Teacher:
Teacher Name: Pyari Sahu
Specialty: Programming
Lab Teacher:
Teacher Name: Pyari Sahu
Specialty: Programming
Students Registered:

Student ID: S111
Name: Tanvi
Level: XII
Student ID: S222
Name: Manvi
Level: XI
Student ID: S333
Name: Trinav
Level: X
Editor is loading...