Student and Course Class in C++ Programming Example Code Solution Implementation for Homework assignment and quiz
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
private:
int Rollnumber;
double GPA;
int Credithours;
public:
CStudent(int aRollnumber,double aGPA,int aCredithours)
{
Rollnumber=aRollnumber;
GPA=aGPA;
Credithours=aCredithours;
}
///////////////////////////////////////////////////////////
const bool operator>(const CStudent& s2)
{
if(GPA ==s2.GPA)
{
if (Credithours > s2.Credithours)
return true;
else
return false;
}
else
{
if (GPA > s2.GPA)
return true;
else
return false;
}
}
/////////////////////////////////////////////////////////////////////////
const bool operator<(const CStudent& s2)
{
if(GPA ==s2.GPA)
{
if (Credithours< s2.Credithours)
return true;
else
return false;
}
else
{
if (GPA < s2.GPA)
return true;
else
return false;
}
}
///////////////////////////////////////////////////////
void display()
{
cout << "\nRoll Number: " << Rollnumber ;
cout << "\nGPA: " << GPA ;
cout << "\nCredit hours: " << Credithours<<endl;
}
};
class Course
{
private:
string Name;
CStudent *student;
int totalnumber;
public:
Course()
{
Name="";
student=NULL;
totalnumber=0;
}
Course(string aName )
{
Name=aName;
totalnumber=0;
student= new CStudent[10];
}
Course(const Course& c2)
{
Name=c2.Name;
student=c2.student;
}
~Course( )
{
delete [] student;
}
///////////////////////////////////////////////////////////
void addStudent(int RollNumber)
{
totalnumber++;
student= new int[totalnumber];
student[totalnumber-1]=RollNumber;
}
////////////////////////////////////////
void ascendingSort()
{
for(int i=0;i<totalnumber;i++)
{
for(int j=0;j<i;j++)
{
if(student[i]<student[j])
{
int temp;
temp= student[i];
student[i]=student[j] ;
student[j]=temp;
}
}
}
}
////////////////////////////////////////
void descendingSort()
{
for(int i=0;i<totalnumber;i++)
{
for(int j=0;j<i;j++)
{
if(student[i]>student[j])
{
int temp;
temp= student[i];
student[i]=student[j] ;
student[j]=temp;
}
}
}
}
///////////////////////////////////////////////////////
void display()
{
cout<<"\nCourse Name"<<Name<<endl;
cout<<"Total Student "<<totalnumber<<endl;
for(int i=0;i<totalnumber;i++)
cout << "Roll Number: " << student[i]<<endl;
}
};
///////////////////////////////////////////////////////////////////////////////////
int main()
{
Course c1("CS200");
Course c2;
c2=c1;
c1.addStudent(12312321);
c1.addStudent(12312421);
c1.addStudent(12312021);
c2.ascendingSort();
c1.display();
c2.descendingSort();
c1.display();
system("pause");
}