✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
C++ structures behave differently than C structures. The first difference is that C++ structures enable data abstraction and encapsulation [to be covered in subsequent weeks] by allowing both data [called data members] and functions [called member functions] to be defined inside the structure block.
There is an additional minor difference to know about structure types in C++ in comparison to C. Suppose, we've defined the following structure:
struct Vector {
int x, y, z;
};
Recall that in C, the type defined above is struct Vector and a variable of such a type would be defined like this:
struct Vector vec;In C++, you can define variables of similar type with or without keyword struct:
struct Vector vec1; // variable of type struct Vector
Vector vec2; // ditto
In practice, this is a superficial difference that leads to a little less typing of C++ code in comparison to C. Now, compile the following code using a C compiler and then a C++ compiler.
#include <stdio.h>
struct Vector {
int x, y, z;
};
int main(void) {
Vector v = {10, 20, 30};
printf("(%d, %d, %d)\n", v.x, v.y, v.z);
return 0;
}
Which of the following best reflects the output of these compilations?