project 5.2 from the Murach C# 2012 book Solution

1. Are the following statements correct? Why?
char* s = "hello";
s = “how are you?”;

2. Are the following statements correct? Why?
char s[] = "Greetings";
s = “Greetings";

3. Are the following statements valid? Why?

float f = 10.0;
const float *pf = &f;
f++;
(*pf)++;


4. Are the following statements valid? Why?

const int m =200;
const int * q = &m;
++(*q);
++q;


5. Is the following declaration valid? Why?
char str[8] = "COP 2931";


6. Given the following class inheritance structure, what is the order of the calls to the constructor if an object of class derived2 is declared?
Class base {}
class derived1: public base {}
Class derived2: private derived1 {}


7. In the following code segment, indicate whether the access in the two statements in boldface is valid or not and why.
class Setter{
private:
char *myName;
public:
friend void info(Setter, Name);
};
class Name: private Setter {
private:
char *myAddress;
};
void info(Setter s, Name n) {
cout << s.myName << endl;
cout << n.myAddress << endl;
};


8. In the following code segment, indicate whether the access in the statements in boldface is valid or not and why.
class Name {
protected:
char* myName;
};
class Contact: public Name {
private:
char* myAddress;
};
class employee: public Contact {
public:
void print(void) {
cout << myName << endl;
cout << myAddress << endl;
}
}
Powered by