Calling an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dav3
    New Member
    • Nov 2006
    • 94

    Calling an object

    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;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You will not be able to call the function as

    Code:
    Student.displaySin();
    displaySin() is a member of an object of type Person - that is, you need to create a Person object before you can call displaySin(). After all, all Persons wouldn't have the same sin to display, so it wouldn't make sense to call displaySin() on the class name rather than the object name.

    Comment

    • dav3
      New Member
      • Nov 2006
      • 94

      #3
      You are correct. But i am creating Students, all of whom have different sin numbers etc.... I have no idea how to call these objects because they are being stored in an array.

      Student[0].displaySin(); was one of many failed attempts at tryin to get at these lil buggers.

      Comment

      • vpawizard
        New Member
        • Nov 2006
        • 66

        #4
        Hello,
        One possible way is to override displaySin() in the derived class:

        Code:
        class Person
        {
            public:
            void displaySin();
        };
        
        class Student: public Person
        {
            public:
            void displaySin()
            {
                Person::displaySin();
            }
        };
        Regards,

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by dav3
          Student[0].displaySin(); was one of many failed attempts at tryin to get at these lil buggers.
          Maybe temp[0].displaySin(), since temp[] is your array of Students?

          Comment

          • dav3
            New Member
            • Nov 2006
            • 94

            #6
            i got it. my temp = new Student(XXX); was incorrect. took out the "new" and now I am on the gravy train (for the time being at least). I think I was essentially allocating memory for the array twice? Anyways my trial and error way of hacking up my program worked, hehe. Thanks all for your effort and input, its always appreciated.

            :)

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              You don't want to write

              temp = new Student(whateve r);

              but instead

              temp[i] - new Student(whateve r);

              Otherwise, you will be changing what temp points to from an array of Students to an individual Student.

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                is this something like you require
                Code:
                #import <iostream>
                #import <fstream>
                #import <string>
                using namespace std;
                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();
                    int displaySin();	//TESTING FUNCTION
                    string getName(){ return name;}
                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;
                } 
                int Person::displaySin()
                {
                	return sin;  // ** retrun sin????
                }
                 
                
                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 main() //_tmain(int argc, _TCHAR* argv[])
                {
                	Student temp[6];// = 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 << "file open fail "; cin.get(); return -1; }
                
                		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*************//		
                    int n=0;
                	while(! file.eof () )			
                	{							
                		file>>sinNum;				
                		file>> name1;				
                		file>> name2;				
                		file>> birthDay;
                		file>> email;
                		file>> major;
                		cout << (name = name1 + " " + name2) << endl;;
                       // ** put data in array
                       	temp[n++] = Student(sinNum,name,birthDay,email,major); //Creation of new student object 
                
                	}
                  // ** check data read in OK2
                  for (int i=0;i<n;i++)
                   cout << "student " << temp[i].getName() << endl;
                
                //************* Getting user's selection from menu *************//
                  selection = userMenu();	//Display User menu
                 
                  switch (selection)
                  {
                	case 1://name
                	cout<<"test"<<endl;
                		break;
                	case 2://sin
                 for (int i=0;i<n;i++)
                   cout << "student " << temp[i].getName() << " sin " <<  temp[i].displaySin() << endl;
                
                		break;
                	case 3://major
                
                		break;
                	case 4://birthday
                
                		break;
                  }
                  cin.get();cin.get();
                  return 0;
                }

                Comment

                • dav3
                  New Member
                  • Nov 2006
                  • 94

                  #9
                  Horace yes that is very similar to what I was finally able to work around to. However, I am confused about how I am going to get my array up to my: compareByAttrib ute() functions so the user can sort the list.

                  Comment

                  • dav3
                    New Member
                    • Nov 2006
                    • 94

                    #10
                    Originally posted by dav3
                    Horace yes that is very similar to what I was finally able to work around to. However, I am confused about how I am going to get my array up to my: compareByAttrib ute() functions so the user can sort the list.
                    My apologies this is not "exactly" what i need to do. I have to use CompareBySIN to return a number to express larger, equal, less than another student. Then create an array of the students based on that. The sorting function should not be a problem as I have done many of them before, but the compareBy()'s have got me bamboozled.

                    Comment

                    • dav3
                      New Member
                      • Nov 2006
                      • 94

                      #11
                      still looking for ideas about these stupid compareBy() functions.

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        You have the objects value, and you have the value being passed - return -1 if the given value is less than the object value, 1 if the given value is greater than the object value, and 0 if they are equal. I believe there is a function that will compare two strings - strcomp, perhaps...If you can't find it, you could write your own comparing each string character by character.

                        Comment

                        • dav3
                          New Member
                          • Nov 2006
                          • 94

                          #13
                          Originally posted by Ganon11
                          You have the objects value, and you have the value being passed - return -1 if the given value is less than the object value, 1 if the given value is greater than the object value, and 0 if they are equal. I believe there is a function that will compare two strings - strcomp, perhaps...If you can't find it, you could write your own comparing each string character by character.
                          Alright I am not sure if I was dropped on my head as a child or what but this still is not making any sense. Here is my function.

                          Code:
                          int Person::compareBySin(Student &s)
                          {
                          	for(int x = 0; x<6; x++)
                          	{
                          	if(s[x].sin == s[x+1].sin)
                          	{
                          		return 0;
                          	}
                          	if(s[x].sin > s[x+1].sin)
                          	{
                          		return 1;
                          	}
                          	if(s[x].sin < s[x+1].sin)
                          	{
                          		return -1;
                          	}
                          	}
                          	return 0;
                          }
                          the call to the function is.
                          Code:
                          for(int x = 0; x<6;++x)
                           {
                               s[x].compareBySin(*s);
                           }
                          This is giving me 12 errors (4 errors in each if statement in the function) and they are as follows.

                          Error 1 error C2676: binary '[' : 'Student' does not define this operator or a conversion to a type acceptable to the predefined operator
                          Error 2 error C2228: left of '.sin' must have class/struct/union
                          Error 3 error C2676: binary '[' : 'Student' does not define this operator or a conversion to a type acceptable to the predefined operator
                          Error 4 error C2228: left of '.sin' must have class/struct/union


                          A special place in my heart forever for whoever can point me in the right direction and kick me in the butt.

                          Comment

                          • dav3
                            New Member
                            • Nov 2006
                            • 94

                            #14
                            Decided to do this in another language. To hell with c++ and its annoying pointers
                            :(

                            within a few hours I was able to code 95% of this in JAVA (I have never done any java programming before)

                            Thx to all that tried to help, pointers are just to frustrating for me in c++

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              Sorry you feel that way.

                              One last point though - JAVA uses pointers all over the place. Every time you create an object by saying:

                              Code:
                              ObjectType myObject = new ObjectType();
                              you are creating a pointer of type ObjectType. JAVA doesn't let you make regular object variables - they are always pointers.

                              Comment

                              Working...