int a[] { 0, 10, 20 }; int *p1 {a}; ++p1; //Statement 1 ++*p1; //Statement 2 int *const p2 {a}; ++p2; //Statement 3 ++*p2; //Statement 4 const int *p3 {a}; ++p3; //Statement 5 ++*p3; //Statement 6 const int *const p4 {a}; ++p4; //Statement 7 ++*p4; //Statement 8
cout << "\n";
int a[] { 0, 10, 20, 30, 40 }; const size_t n {size(a)}; //number of elements in the array //Output all the elements in the array. for (const int *p {a}; p <= a + n; ++p) { cout << *p << "\n"; }
j
in binary (base 2)?
Just tell me the value.
You don’t have to write any C++ code.
int i {5}; int j {i | 3};
bigObject
that contains two little objects
inside of it.
When we create the
bigObject
,
we are creating a total of three objects:
the
bigObject
itself, and the two little objects inside of it.
In what order are these three objects constructed?
#include <cstdlib> //for the macro EXIT_SUCCESS class littleClass { int dm; public: littleClass(int i); }; littleClass::littleClass(int i) : dm {i} { } class bigClass { littleClass dm1; littleClass dm2; public: bigClass(int i1, int i2); }; bigClass::bigClass(int i1, int i2) : dm1 {i1}, dm2 {i2} { } int main() { bigClass bigObject {10, 20}; return EXIT_SUCCESS; }
vector<int>
is capable of holding a series of int
s,
just like an array of int
s.
When would you have to use an object of class
vector<int>
insead of an array of int
s?
bloodPressure
.
A struct of this type will contain two
int
s
named
systolic
and
diastolic
.
Then create a variable named
normal
,
of type
bloodPressure
.
The variable
normal
should be born holding the values 110
and 70
,
which represent a normal blood pressure.
Then
create a pointer variable named p
that is born holding the address of the variable normal
.
Then use p
to
output (to cout
) the two int
s
in the variable that p
points to.
When you output these two int
s,
please output a diagonal slash "/"
between them,
because that’s how a blood pressure is conventionally printed
(110/70).
j
in binary (base 2)?
Just tell me the value.
You don’t have to write any C++ code.
int i {7}; int j {i << 2};
p
the wrong type of pointer?
size_t n {10}; const int *p {new int[n]}; //Some time later, delete[] p;
int a[] { 0, 10, 20, 30, 40, 50 }; int *p {a + 3}; cout << p[2] << "\n"; cout << *p << "\n"; cout << p[-1] << "\n";
int a[] { 0, 10, 20, 30, 40 }; const size_t n {size(a)}; //number of elements in the array for (const int *p {a}; p < a + n; ++p) { if (p[0] == p[1]) { cout << "Found 2 consecutive copies of the same value.\n"; } }
1101 1110 1010 1101 1011 1110 1110 1111
obj
that had a constructor that took one argument of type int
.
In what order would the 10 objects in the following C++ program
be constructed and destructed?
#include <cstdlib> //for the macro EXIT_SUCCESS void f(); //Function declarations void g(); int main() { obj b {20}; obj c {30}; obj d {40}; f(); return EXIT_SUCCESS; } void f() { obj e {50}; obj f {60}; obj g {60}; g(); } void g() { obj h {70}; obj a[] { 80, 90, 100 }; }