Valid Access

This is the code we were given and the question is , Is line 18 (return x;) a valid access and justify your answer. My question is. What is Valid / Invalid access? If something is private (x) in this case shouldn't it NOT be able to read it as its not accessible? in that case Line 18 (return x;) is NOT a valid access right? Im just trying to understand the terms. Also a Valid statement. Line 32 (A objA;) is it a valid statement or not? Like an integer is a valid statement right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

//------------------------------------------

class A
{
private:
int x;
protected:
int getX();
public:
void setX();
};

int A::getX()
{
return x;
}

void A::setX()
{
x=10;
}

//----------------------------------------------
class B
{
private:
int y;
protected:
A objA;
int getY();
public:
void setY();
};

void B::setY()
{

y=24;
int a = objA.getX();
}

 //----------------------------------------------

class C: public A
{
protected:
int z;
public:
int getZ();
void setZ();
};

int C::getZ()
{
return z;
}

void C::setZ()
{
z=65;
}
The problem is L42. A::getX() is protected and hence can't be accessed as B isn't derived from A.

Why not just paste the code into a c++ compiler and try it?
As @seeplus says, just try it:
1
2
3
4
5
6
int main()
{
   A a;
   a.setX();              // not much use if you can't set your own value
   cout << a.getX();      // fails
}


The question is (slightly) ambiguous. It's not accessible/valid to the hoi-polloi, but, since it is protected, you are allowed to access it if you are an inheriting class.

Line 18 on its own is OK / valid. It is the call on line 42 which will fail, because B is not a subclass of A.
Last edited on
anonomyss wrote:
What is Valid / Invalid access?

I don't think these are standardized terms, but what they probably ask in this case is whether the access specifiers (public, protected and private) allow you to access the variable.

anonomyss wrote:
If something is private (x) in this case shouldn't it NOT be able to read it as its not accessible?

Private members are accessible within the same class (and within all member functions that belong to that class).

anonomyss wrote:
Also a Valid statement. Line 32 (A objA;) is it a valid statement or not?

I think a "valid statement" is a statement that does not give you a compilation error.

That said, line 32 is technically not a statement, but maybe that's just me being overly pedantic. If someone asked this what they probably wonder is whether this line will compile or not.

anonomyss wrote:
Like an integer is a valid statement right?

A function contains a list of statements. Many statements end with a semicolon and are often put on their own lines. For example, the following code contains three statements, each on its own line:
1
2
3
int x = 9;
x += 7;
std::cout << x;
(assuming this code is written inside a function)

so x or 7, or even x += 7 (without the semicolon), are not statements. They are expressions (with types). Statements often contain expressions and sometimes also other statements. Any expression can be turned into a statement by putting a semicolon at the end.

If statements and while loops are examples of statements that don't end with semicolon (although they can contain a statement that has a semicolon at the end).
Last edited on
L18 is a valid access of x.
private means whether something can be accessed outside the class. x is a member of A. Therefore, L18 is a valid access within the class.

Last edited on
And 1 More question.

Class C has public inheritance with Class A.

So If I were to identify and write Class C's private/protected/public member variables resulting from the inheritance. What would it be? Would there actually be any? and if it were protected, would there also be any?
What do you think they are?
If it is public then public and protected data from A and all data from C?
Read https://en.cppreference.com/w/cpp/language/access
Does that make you change your answer?
So if its a public inheritance , It will be all Public / Protected / Private Data from A and only the Public data from C? If C has public inheritance with Class A.
A derived class does not have access to any private members in it's base class. A derived class with public inheritance only has access to protected/public members in it's base class.
Last edited on
It's quite logical if you think about it. The access specifier that you use when inheriting can only restrict, never allow more access to the inherited members than what the access specifiers for the members specify.
Last edited on
@anonomyss: Can you now answer the questions below?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class A {
  int ax;
protected:
  int ay;
public:
  int az;
};

class B : protected A {
  int bx;
protected:
  int by;
public:
  int bz;
};

class C : public B {
  void sample()
  {
    // Which of ax, ay, az, bx, by, bz we can access here?
  }
};

int main()
{
  A a;
  B b;
  C c;
  // Which of a.ax, a.ay, a.az,
  // b.ax, b.ay, b.az, b.bx, b.by, b.bz
  // c.ax, c.ay, c.az, c.bx, c.by, c.bz
  // we can access here?
}
Registered users can post here. Sign in or register to post.