Help with calling class members

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 12
Reputation: chelo77 is an unknown quantity at this point 
Solved Threads: 0
chelo77 chelo77 is offline Offline
Newbie Poster

Help with calling class members

 
0
  #1
Apr 23rd, 2006
I have problems with this small class program, have two classes...registration and course. I am writing a small function for registration adds the credits. I made an array called ptr of type course in the registration class. Now when i try to access credit a private member in the class course, when i compile it says i cannot access it, but i am using ptr which is a course type, i thought it should have access since its part of the course class. example this is how how i called it... ptr.credits . This is not working in the total_credits function in the registration class, need help to call credits into this class, how can i do this?????????? cannot create any more variables as is, just need to find how to get the values of credits from the course class into the registration class


  1. #include <stdio.h>
  2. #include <iostream.h>
  3.  
  4. class course
  5. {
  6. private:
  7. char string[20];
  8. int credit;
  9.  
  10. public:
  11. course(void);
  12. ~course(void);
  13.  
  14. };
  15.  
  16. class registration
  17. {
  18. private:
  19. course *ptr;
  20. int num;
  21.  
  22. public:
  23. registration(int iNum);
  24. ~registration();
  25. total_credits();
  26.  
  27. };
  28.  
  29.  
  30.  
  31. registration::registration(int iNum)
  32. {
  33. num = iNum;
  34. ptr = new course[num];
  35. }
  36.  
  37. registration::~registration()
  38. {
  39. delete [] ptr;
  40. cout << "reg deconstructor\n";
  41. }
  42.  
  43. registration::total_credits()
  44. {
  45. int iTotal =0, i;
  46. for(i=0; i<=num-1;++i)
  47. iTotal= iTotal + ptr.credit;
  48. cout<<"/nTotal credits are:" << iTotal;
  49. cout<< "\n";
  50. }
  51.  
  52.  
  53. course::course(void)
  54. {
  55. cout<< "Enter the course name and number of credits\n";
  56. cin>> string >> credit;
  57. cout << "constructor" << endl;
  58. }
  59.  
  60. course::~course(void)
  61. {
  62. cout << "\n" <<string <<" " << credit;
  63. cout << "\ndeconstructor\n" << endl;
  64. }
  65.  
  66. void main(void)
  67. {
  68. int iNum;
  69.  
  70.  
  71. cout<< "how many:";
  72. cin >> iNum;
  73. registration test(iNum);
  74. test.total_credits();
  75.  
  76. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: Help with calling class members

 
0
  #2
Apr 23rd, 2006
One error :
  1. ptr.credit
ptr is a pointer to array.


The permssion problem with :
  1. ptr[i].credit
credit was declared private in class course. So only
members of this class may acess credit.

Two solution :
a: Change credit to public . This is ugly.
b: let class registration be a friend of class course. This is what
I would have done.

So the solution :
  1. class course
  2. {
  3. private:
  4. char string[20];
  5. int credit;
  6. public:
  7. course(void);
  8. ~course(void);
  9. /* Added tne next line */
  10. friend class registration;
  11. };
  12. course::course(void)
  13. {
  14. cout<< "Enter the course name : ";
  15. cin>> string ;
  16. cout<< "Enter the number of credits :";
  17. cin >> credit;
  18. cout << "constructor " << string << " " << credit << endl;
  19. }
  20. course::~course(void)
  21. {
  22. cout << "\n" <<string <<" " << credit;
  23. cout << "\ndeconstructor\n" << endl;
  24. }
  25.  
  26. class registration
  27. {
  28. private:
  29. course *ptr;
  30. int num;
  31. public:
  32. registration(int iNum);
  33. ~registration();
  34. /* total_credits(); changed to : */
  35. void total_credits();
  36. };
  37.  
  38. registration::registration(int iNum)
  39. {
  40. num = iNum;
  41. ptr = new course[num];
  42. }
  43. registration::~registration()
  44. {
  45. delete [] ptr;
  46. cout << "reg deconstructor\n";
  47. }
  48. void registration::total_credits()
  49. {
  50. int iTotal =0, i;
  51. for(i=0; i<=num-1;++i)
  52. /* iTotal= iTotal + ptr.credit; changed too : */
  53. iTotal= iTotal + ptr[i].credit;
  54. /* cout<<"/nTotal credits are:" << iTotal; changed too : */
  55. cout<<"\nTotal credits are:" << iTotal;
  56. cout<< "\n";
  57. }
  58.  
  59. int main(void)
  60. {
  61. int iNum;
  62.  
  63.  
  64. cout<< "how many:";
  65. cin >> iNum;
  66. registration test(iNum);
  67. test.total_credits();
  68. cout << "Press Enter to end ..." << endl;
  69. cin.get();
  70. cin.get();
  71. return (0);
  72. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC