| | |
Help with calling class members
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 0
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
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <iostream.h> class course { private: char string[20]; int credit; public: course(void); ~course(void); }; class registration { private: course *ptr; int num; public: registration(int iNum); ~registration(); total_credits(); }; registration::registration(int iNum) { num = iNum; ptr = new course[num]; } registration::~registration() { delete [] ptr; cout << "reg deconstructor\n"; } registration::total_credits() { int iTotal =0, i; for(i=0; i<=num-1;++i) iTotal= iTotal + ptr.credit; cout<<"/nTotal credits are:" << iTotal; cout<< "\n"; } course::course(void) { cout<< "Enter the course name and number of credits\n"; cin>> string >> credit; cout << "constructor" << endl; } course::~course(void) { cout << "\n" <<string <<" " << credit; cout << "\ndeconstructor\n" << endl; } void main(void) { int iNum; cout<< "how many:"; cin >> iNum; registration test(iNum); test.total_credits(); }
•
•
Join Date: Apr 2006
Posts: 26
Reputation:
Solved Threads: 0
One error :
ptr is a pointer to array.
The permssion problem with :
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 :
C++ Syntax (Toggle Plain Text)
ptr.credit
The permssion problem with :
C++ Syntax (Toggle Plain Text)
ptr[i].credit
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 :
C++ Syntax (Toggle Plain Text)
class course { private: char string[20]; int credit; public: course(void); ~course(void); /* Added tne next line */ friend class registration; }; course::course(void) { cout<< "Enter the course name : "; cin>> string ; cout<< "Enter the number of credits :"; cin >> credit; cout << "constructor " << string << " " << credit << endl; } course::~course(void) { cout << "\n" <<string <<" " << credit; cout << "\ndeconstructor\n" << endl; } class registration { private: course *ptr; int num; public: registration(int iNum); ~registration(); /* total_credits(); changed to : */ void total_credits(); }; registration::registration(int iNum) { num = iNum; ptr = new course[num]; } registration::~registration() { delete [] ptr; cout << "reg deconstructor\n"; } void registration::total_credits() { int iTotal =0, i; for(i=0; i<=num-1;++i) /* iTotal= iTotal + ptr.credit; changed too : */ iTotal= iTotal + ptr[i].credit; /* cout<<"/nTotal credits are:" << iTotal; changed too : */ cout<<"\nTotal credits are:" << iTotal; cout<< "\n"; } int main(void) { int iNum; cout<< "how many:"; cin >> iNum; registration test(iNum); test.total_credits(); cout << "Press Enter to end ..." << endl; cin.get(); cin.get(); return (0); }
![]() |
Similar Threads
- Pointers to class members - it just won't work. (C)
- Dynamic class members? (C++)
- Calling a class upon button click (C#)
- SegFault on class instantiation (C)
- a class question (Python)
- accessing private data members (C++)
- Class Passing and Returning Arrays (Java)
Other Threads in the C++ Forum
- Previous Thread: string?
- Next Thread: Help with Big-O notation
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





