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.pngtestcode
#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;
}
网友评论