C++|类成员访问权限|可访问表

2017-11-23  本文已影响0人  绍重先

Exercise 1: Given the following C++ code:

class A {
public :
    int x;
    A *objARef;
private :
    int y;
protected :
    int z;
};
class B : public A {
public :
    A objA;
};
class C {
public :
    A objA;
A *objARef;
B objB;
};

Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK).

table.png

testcode

#include<iostream>

using namespace std;

class A {
    public :
        int x;
        A *objARef;
        int getRef(){
            return objARef->x; 
            return objARef->y;
            return objARef->z;
        } 
    private :
        int y;
    protected :
        int z;
};
class B : public A {
    public :
        A objA;
        int getRef() {
            return objA.x;
            //return objA.y;
            //return objA.z;
            return z;//pass
        }
};
class C {
    public :
        A objA;
        A *objARef;
        B objB;
    
    int getobjA(){
        return objA.x;
        //return objA.y;
        //return objA.z;
    }
    
    int getARef(){
        return objARef->x;
        //return objARef->y;
        //return objARef->z;
    }

    int getobjB(){
        return objB.x;
        //return objB.y;
        //return objB.z;
    } 
};

int main() {
    B B1;
    //cout<<B1.z;
    cout<<B1.objARef->x;
    //cout<<B1.objARef->y;
    //cout<<B1.objARef->z;
}
上一篇 下一篇

猜你喜欢

热点阅读