Access over 20 million homework & study documents

Answersheet programming essentials in c chapter6 assessment

Content type
User Generated
Rating
Showing Page:
1/12
CPA Chapter 6 Assessment Answers 100%
1. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
private:
int v;
};
class Y : public X {
Y() : v(0) {}
}
int main() {
Y y;
cout << y.v;
return 0;
}
It prints -1
It prints 0
It prints 1
Compilation fails
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
protected:
int v;
};
class Y : protected X {
Y() : v(0) {}
}
int main() {
Y *y = new Y();
cout << y->v;
delete y;
return 0;
}
It prints -1

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/12
It prints 0
Compilation fails
It prints 1
3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
public:
int v;
void put(int x) { v = x; }
int get(void) { return v; }
};
class Y : public X {
public:
Y() { put(0); }
void write(int x) { put(x + 1); }
int read(void) { return get() 1; }
};
int main() {
Y *y = new Y();
y->write(1);
cout << y->read();
delete y;
return 0;
}
It prints -1
Compilation fails
It prints 1
It prints 0
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main() {
Z *z = new Z();
Y *y = new Y();
z = y;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/12
cout << (z == y);
return 0;
}
It prints 0
It prints 1
It prints -1
Compilation fails
5. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main() {
Z *z = new Z();
X *x = new X();
x = z;
cout << (x == z);
return 0;
}
It prints 1
Compilation fails
It prints -1
It prints 0
6. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class X {
public:
void shout() { cout << “X”; }
};
class Y : public X {
public:
void shout() { cout << “Y”; }
};
int main() {
X *x = new Y();
static_cast<Y *>(x) -> shout();
return 0;
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
4/12

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 12 pages?
Access Now
Unformatted Attachment Preview
CPA Chapter 6 Assessment Answers 100% 1. What happens when you attempt to compile and run the following code? #include using namespace std; class X { private: int v; }; class Y : public X { Y() : v(0) {} } int main() { Y y; cout write(1); cout read(); delete y; return 0; } • • • • It prints -1 Compilation fails It prints 1 It prints 0 4. What happens when you attempt to compile and run the following code? #include using namespace std; class X { }; class Y : public X { }; class Z : public X { }; int main() { Z *z = new Z(); Y *y = new Y(); z = y; cout ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Really useful study material!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4