6. Consider the following statements: class yClass { public: void one(); void tw
ID: 3542800 • Letter: 6
Question
6. Consider the following statements:
class yClass
{
public:
void one();
void two(int, int);
yClass();
private:
int a;
int b;
};
class xClass: public yClass
{
public:
void one();
xClass();
private:
int z;
};
yClass y;
xClass x;
a. The private members of yClass are public members of xClass.
True or False?
b. Mark the following statements as valid or invalid. If a statement is
invalid, explain why.
i. void yClass::one()
{
cout << a + b << endl;
}
ii. y.a = 15;
x.b = 30;
iii. void xClass::one()
{
a = 10;
b = 15;
z = 30;
cout << a + b + z << endl;
}
iv. cout << y.a << " " << y.b << "
Explanation / Answer
class yClass
{
public:
void one();
void two(int, int);
yClass();
private:
int a;
int b;
};
class xClass: public yClass
{
public:
void one();
xClass();
private:
int z;
};
yClass y;
xClass x;
a. The private members of yClass are public members of xClass.
False. they still private only.
b. Mark the following statements as valid or invalid. If a statement is
invalid, explain why.
i. void yClass::one()
{
cout << a + b << endl;
}
Valid. since one() is member function of yClass it can access its own private member variables a and b.
ii. y.a = 15; .. invalid since a is private member variable. we cant access this from outside class.
x.b = 30; .. invalid since b is private member variable. we cant access this from outside class.
iii. void xClass::one()
{
a = 10;
b = 15;
z = 30;
cout << a + b + z << endl;
}
.. invalid since a is private member variable. we cant access this from outside class.
.. invalid since b is private member variable. we cant access this from outside class.
iv. cout << y.a << " " << y.b << "
.. invalid since a is private member variable. we cant access this from outside class.
.. invalid since b is private member variable. we cant access this from outside class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.