This should be a very simple task, but its giving me fits. I have made some progress (i think) since my last post and forgive me for making a new thread. I have a program that is reading in a file and creating an array of objects (if someone could verify that this program is actually doing this just by looking at my code I would appreciate it). I have 2 classes as you can see below and I want to simply call my displaySin(); function from Class Person. I am unsure how to make the call from my main function I want to call it from case 2: of my switch statement in my main program. I thought it would be simply "Student.displa ySin();" but it is not. Any help is appreciated at always. Also I am aware there are probably errors in my class function, but that is not my priority atm.
Code:
class Student;
class Person
{
public:
Person();
Person(int sinNumber, string studentName);
int compareByBirthday(Student &s);
int compareByMajor(Student &s);
int compareByName(Student &s);
int compareBySin(Student &s);
int getSin();
void displaySin(); //TESTING FUNCTION
private:
int sin;
string name;
};
Person::Person(){}
Person::Person(int sinNumber, string studentName)
{
sin = sinNumber;
string temp;
temp = studentName;
studentName=name;
name = temp;
}
int Person::compareByBirthday(Student & s)
{
return 0;
}
int Person::compareByMajor(Student &s)
{
return 0;
}
int Person::compareByName(Student &s)
{
return 0;
}
int Person::compareBySin(Student &s)
{
return 0;
}
int Person::getSin()
{
return sin;
}
void Person::displaySin()
{
cout<<sin<<endl;
}
class Student : public Person
{
public:
Student();
Student(int sinNumber, string studentName, int bDay, string email, string mjr);
private:
int birthday;
string emailAddress;
string major;
};
Student::Student(){}
Student::Student(int sinNumber, string studentName, int bDay, string email, string mjr) : Person(sinNumber, studentName)
{
birthday = bDay;
email.swap(emailAddress);
mjr.swap (major);
}
//************* Display user menu/accept the user selection ************//
int userMenu()
{
int selection;
cout << endl << endl;
cout << "1. Sort students by name\n";
cout << "2. Sort students by SIN\n";
cout << "3. Sort students by major\n";
cout << "4. Sort students by birthday\n";
cout << "5. Exit\n";
cout << endl << endl;
do
{
cout << "Enter selection [1-5]: ";
cin >> selection;
} while ((selection < 1) || (selection > 6));
return (selection);
}
int _tmain(int argc, _TCHAR* argv[])
{
Student *temp = new Student[6];
string str;
int selection; //Variable to accept user selection from user menu
//******* Reading file 1 line at a time. Then extracting *******//
//******* the information needed to create a new object *******//
ifstream file("studentFile.txt");
if ( file.is_open())
{
cout<<"Reading File .....\n\n"<<endl;
int sinNum; //dummy variable for sin number
string name; //variable to pass to Student to create object
string name1; //dummy variable
string name2; //2nd part of name dummy variable
int birthDay; //dummy variable for birthday
string email; //dummy variable for email
string major; //dummy variable for major
int pos; //Dummy variable to traverse through each line
//*************Creating Student Objects*************//
while(! file.eof () )
{
file>>sinNum;
file>> name1;
file>> name2;
file>> birthDay;
file>> email;
file>> major;
name = name1 + " " + name2;
temp = new Student(sinNum,name,birthDay,email,major); //Creation of new student object
}
}
//************* Getting user's selection from menu *************//
selection = userMenu(); //Display User menu
switch (selection)
{
case 1://name
cout<<"test"<<endl;
break;
case 2://sin
break;
case 3://major
break;
case 4://birthday
break;
}
return 0;
}
Comment