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

#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();

}

One error :

ptr.credit

ptr is a pointer to array.


The permssion problem with :

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 :

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.