473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Calling an object

94
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.displaySin();" 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.
Expand|Select|Wrap|Line Numbers
  1. class Student;
  2. class Person
  3. {
  4. public:
  5.     Person(); 
  6.     Person(int sinNumber, string studentName);
  7.     int compareByBirthday(Student &s);            
  8.     int compareByMajor(Student &s);            
  9.     int compareByName(Student &s);                
  10.     int compareBySin(Student &s); 
  11.     int getSin();
  12.     void displaySin();    //TESTING FUNCTION
  13. private:
  14.     int sin;
  15.     string name;
  16. };
  17. Person::Person(){}
  18. Person::Person(int sinNumber, string studentName)
  19.     sin = sinNumber;
  20.     string temp;
  21.     temp = studentName;
  22.     studentName=name;
  23.     name = temp;
  24. }
  25. int Person::compareByBirthday(Student & s)
  26. {
  27.     return 0;
  28. }
  29. int Person::compareByMajor(Student &s) 
  30. {
  31.     return 0;
  32. }
  33. int Person::compareByName(Student &s)
  34. {
  35.     return 0;
  36. }
  37. int Person::compareBySin(Student &s)
  38. {
  39.     return 0;
  40. }
  41. int Person::getSin()
  42. {
  43.     return sin;
  44. void Person::displaySin()
  45. {
  46.     cout<<sin<<endl;
  47. }
  48. class Student : public Person
  49. {
  50. public:
  51.     Student();
  52.     Student(int sinNumber, string studentName, int bDay, string email, string mjr);
  53. private:
  54.     int birthday;
  55.     string emailAddress;
  56.     string major;
  57. };
  58. Student::Student(){}
  59. Student::Student(int sinNumber, string studentName, int bDay, string email, string mjr) : Person(sinNumber, studentName) 
  60. {
  61.     birthday = bDay;
  62.     email.swap(emailAddress);
  63.     mjr.swap (major);
  64. }
  65. //************* Display user menu/accept the user selection ************//
  66. int userMenu()
  67. {
  68.     int selection;
  69.     cout << endl << endl;
  70.     cout << "1.  Sort students by name\n";
  71.     cout << "2.  Sort students by SIN\n";
  72.     cout << "3.  Sort students by major\n";
  73.     cout << "4.  Sort students by birthday\n";
  74.     cout << "5.  Exit\n";
  75.     cout << endl << endl;
  76.     do 
  77.     {
  78.         cout << "Enter selection [1-5]: ";
  79.         cin >> selection;
  80.     } while ((selection < 1) || (selection > 6));
  81.     return (selection);
  82. }
  83. int _tmain(int argc, _TCHAR* argv[])
  84. {
  85.     Student *temp = new Student[6];
  86.     string str;
  87.     int selection;    //Variable to accept user selection from user menu
  88. //******* Reading file 1 line at a time. Then extracting *******//
  89. //******* the information needed to create a new object  *******//
  90.     ifstream file("studentFile.txt");
  91.     if ( file.is_open())
  92.     {
  93.         cout<<"Reading File .....\n\n"<<endl;
  94.         int sinNum;        //dummy variable for sin number
  95.         string name;    //variable to pass to Student to create object
  96.         string name1;    //dummy variable 
  97.         string name2;    //2nd part of name dummy variable
  98.         int birthDay;    //dummy variable for birthday
  99.         string email;    //dummy variable for email
  100.         string major;    //dummy variable for major
  101.         int pos;        //Dummy variable to traverse through each line
  102.  
  103. //*************Creating Student Objects*************//        
  104.     while(! file.eof () )            
  105.     {                            
  106.         file>>sinNum;                
  107.         file>> name1;                
  108.         file>> name2;                
  109.         file>> birthDay;
  110.         file>> email;
  111.         file>> major;
  112.         name = name1 + " " + name2;
  113.            temp = new Student(sinNum,name,birthDay,email,major); //Creation of new student object 
  114.     }
  115.   }
  116.  
  117. //************* Getting user's selection from menu *************//
  118.   selection = userMenu();    //Display User menu
  119.  
  120.   switch (selection)
  121.   {
  122.     case 1://name
  123.     cout<<"test"<<endl;
  124.         break;
  125.     case 2://sin
  126.  
  127.         break;
  128.     case 3://major
  129.  
  130.         break;
  131.     case 4://birthday
  132.  
  133.         break;
  134.   }
  135.   return 0;
  136. }
Jan 31 '07 #1
15 2327
Ganon11
3,652 Expert 2GB
You will not be able to call the function as

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 31 '07 #2
dav3
94
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.
Jan 31 '07 #3
Hello,
One possible way is to override displaySin() in the derived class:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Person
  3. {
  4.     public:
  5.     void displaySin();
  6. };
  7.  
  8. class Student: public Person
  9. {
  10.     public:
  11.     void displaySin()
  12.     {
  13.         Person::displaySin();
  14.     }
  15. };
  16.  
  17.  
Regards,
Jan 31 '07 #4
Ganon11
3,652 Expert 2GB
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?
Jan 31 '07 #5
dav3
94
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.

:)
Jan 31 '07 #6
Ganon11
3,652 Expert 2GB
You don't want to write

temp = new Student(whatever);

but instead

temp[i] - new Student(whatever);

Otherwise, you will be changing what temp points to from an array of Students to an individual Student.
Jan 31 '07 #7
horace1
1,510 Expert 1GB
is this something like you require
Expand|Select|Wrap|Line Numbers
  1. #import <iostream>
  2. #import <fstream>
  3. #import <string>
  4. using namespace std;
  5. class Student;
  6. class Person
  7. {
  8. public:
  9.     Person(); 
  10.     Person(int sinNumber, string studentName);
  11.     int compareByBirthday(Student &s);            
  12.     int compareByMajor(Student &s);            
  13.     int compareByName(Student &s);                
  14.     int compareBySin(Student &s); 
  15.     int getSin();
  16.     int displaySin();    //TESTING FUNCTION
  17.     string getName(){ return name;}
  18. private:
  19.     int sin;
  20.     string name;
  21. };
  22. Person::Person(){}
  23. Person::Person(int sinNumber, string studentName)
  24.     sin = sinNumber;
  25.     string temp;
  26.     temp = studentName;
  27.     studentName=name;
  28.     name = temp;
  29. }
  30. int Person::compareByBirthday(Student & s)
  31. {
  32.     return 0;
  33. }
  34. int Person::compareByMajor(Student &s) 
  35. {
  36.     return 0;
  37. }
  38. int Person::compareByName(Student &s)
  39. {
  40.     return 0;
  41. }
  42. int Person::compareBySin(Student &s)
  43. {
  44.     return 0;
  45. }
  46. int Person::getSin()
  47. {
  48.     return sin;
  49. int Person::displaySin()
  50. {
  51.     return sin;  // ** retrun sin????
  52. }
  53.  
  54.  
  55. class Student : public Person
  56. {
  57. public:
  58.     Student();
  59.     Student(int sinNumber, string studentName, int bDay, string email, string mjr);
  60. private:
  61.     int birthday;
  62.     string emailAddress;
  63.     string major;
  64. };
  65. Student::Student(){}
  66. Student::Student(int sinNumber, string studentName, int bDay, string email, string mjr) : Person(sinNumber, studentName) 
  67. {
  68.     birthday = bDay;
  69.     email.swap(emailAddress);
  70.     mjr.swap (major);
  71. }
  72. //************* Display user menu/accept the user selection ************//
  73. int userMenu()
  74. {
  75.     int selection;
  76.     cout << endl << endl;
  77.     cout << "1.  Sort students by name\n";
  78.     cout << "2.  Sort students by SIN\n";
  79.     cout << "3.  Sort students by major\n";
  80.     cout << "4.  Sort students by birthday\n";
  81.     cout << "5.  Exit\n";
  82.     cout << endl << endl;
  83.     do 
  84.     {
  85.         cout << "Enter selection [1-5]: ";
  86.         cin >> selection;
  87.     } while ((selection < 1) || (selection > 6));
  88.     return (selection);
  89. }
  90. int main() //_tmain(int argc, _TCHAR* argv[])
  91. {
  92.     Student temp[6];// = new Student[6];
  93.     string str;
  94.     int selection;    //Variable to accept user selection from user menu
  95. //******* Reading file 1 line at a time. Then extracting *******//
  96. //******* the information needed to create a new object  *******//
  97.     ifstream file("studentFile.txt");
  98.     if (! file.is_open())
  99.          { cout << "file open fail "; cin.get(); return -1; }
  100.  
  101.         cout<<"Reading File .....\n\n"<<endl;
  102.         int sinNum;        //dummy variable for sin number
  103.         string name;    //variable to pass to Student to create object
  104.         string name1;    //dummy variable 
  105.         string name2;    //2nd part of name dummy variable
  106.         int birthDay;    //dummy variable for birthday
  107.         string email;    //dummy variable for email
  108.         string major;    //dummy variable for major
  109.         int pos;        //Dummy variable to traverse through each line
  110.  
  111. //*************Creating Student Objects*************//        
  112.     int n=0;
  113.     while(! file.eof () )            
  114.     {                            
  115.         file>>sinNum;                
  116.         file>> name1;                
  117.         file>> name2;                
  118.         file>> birthDay;
  119.         file>> email;
  120.         file>> major;
  121.         cout << (name = name1 + " " + name2) << endl;;
  122.        // ** put data in array
  123.            temp[n++] = Student(sinNum,name,birthDay,email,major); //Creation of new student object 
  124.  
  125.     }
  126.   // ** check data read in OK2
  127.   for (int i=0;i<n;i++)
  128.    cout << "student " << temp[i].getName() << endl;
  129.  
  130. //************* Getting user's selection from menu *************//
  131.   selection = userMenu();    //Display User menu
  132.  
  133.   switch (selection)
  134.   {
  135.     case 1://name
  136.     cout<<"test"<<endl;
  137.         break;
  138.     case 2://sin
  139.  for (int i=0;i<n;i++)
  140.    cout << "student " << temp[i].getName() << " sin " <<  temp[i].displaySin() << endl;
  141.  
  142.         break;
  143.     case 3://major
  144.  
  145.         break;
  146.     case 4://birthday
  147.  
  148.         break;
  149.   }
  150.   cin.get();cin.get();
  151.   return 0;
  152. }
  153.  
Jan 31 '07 #8
dav3
94
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: compareByAttribute() functions so the user can sort the list.
Jan 31 '07 #9
dav3
94
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: compareByAttribute() 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.
Jan 31 '07 #10
dav3
94
still looking for ideas about these stupid compareBy() functions.
Feb 1 '07 #11
Ganon11
3,652 Expert 2GB
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.
Feb 1 '07 #12
dav3
94
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.

Expand|Select|Wrap|Line Numbers
  1. int Person::compareBySin(Student &s)
  2. {
  3.     for(int x = 0; x<6; x++)
  4.     {
  5.     if(s[x].sin == s[x+1].sin)
  6.     {
  7.         return 0;
  8.     }
  9.     if(s[x].sin > s[x+1].sin)
  10.     {
  11.         return 1;
  12.     }
  13.     if(s[x].sin < s[x+1].sin)
  14.     {
  15.         return -1;
  16.     }
  17.     }
  18.     return 0;
  19. }
  20.  
the call to the function is.
Expand|Select|Wrap|Line Numbers
  1. for(int x = 0; x<6;++x)
  2.  {
  3.      s[x].compareBySin(*s);
  4.  }
  5.  
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.
Feb 2 '07 #13
dav3
94
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++
Feb 2 '07 #14
Ganon11
3,652 Expert 2GB
Sorry you feel that way.

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

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 2 '07 #15
dav3
94
i dunno, all i know is i have a couple of years experience with c++ in school and my marks are always A's. But for whatever reason when it comes down to programming projects nothing seems to fit in the right places.

With the exception of not being able to get this program to write to a file in JAVA I started and finished this project in just under 5 hours (on 2 hours sleep), with no previous java experience.

For the limited programming I have to do in my life I think ill be using the easier of the 2 from now on.

:)

I am sure my next programming assignment will be started in JAVA and finished in c++ though, cause thats just the way the world works.
Feb 2 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() '...
30
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
1
by: Lakshmi | last post by:
Hi All, I am having performance issues with the .NET client calling the Java Webservice running on axis. Have detailed the problem below. Please help. I wrote a webservice in Java. Lets name...
5
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
6
by: Mirek Endys | last post by:
Hello all, another problem im solving right now. I badly need to get typeof object that called static method in base classe. I did it by parameter in method Load, but i thing there should be...
10
by: Max Yuzhakov | last post by:
Hello! It is correct behaviour for python to call __del__ on some identity of a class object more than once? In brief I shall describe a situation. Sorry for my english. For debugin...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.