derived classes - accessing private members

This is a review for a test. I have attempted numerous things and maybe I've just been trying too long and need a rest haha but this is the problem and what I have.

a. Write the definition of the function setData of classTwo.
b. Write the definition of the function print of classTwo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class One
	{
	public: 
		void print() const;		//print x and y
	protected: 
		void setData(int a, int b);	//set x to a and y to b
	private: 
		int x; 
		int y; 
	}; 

	class Two: public One
	{
	public: 
		void setData(int a, int b, int c);//x to a, y to b, z to c
		void print() const;		    //print x, y, z

	private: 
		int z; 
	}; 



and this is what I have (I know its TOTALLY wrong):
1
2
3
4
5
6
7
8
9
10
11
12
13
void One::setData(int a, int b)
{

	x=a;
	y=b;
	cout<<"x="<<x<<" y="<<y<<endl;

}
void Two::setData(int a, int b, int c)
{	
	z=c;
	
}


I couldn't figure out how to access those members without first defining class One? Please can someone help? Not looking for answers, just a clear explanation of what I'm overlooking! PLEASE! :/ oh and just part a.. I think once I understand part A, I can understand part B.
Last edited on
Since Two is also a One (inheritance), Two can call any public or protected method on One.
Thanks so much.. I haven't had any sleep and I don't know how I could have overlooked protected.. I kept figuring it as private.. Thanks...
Topic archived. No new replies allowed.