It is illegal to define a function within a structure in C++.
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
It is illegal to define a function within a structure in C++. State True or False
Pick ONE option
Show answer & explanation
In C++, it is completely legal to define functions (member functions/methods) within a structure. Structures in C++ are nearly identical to classes, differing only in default access level (public for structs, private for classes). Member functions are a core feature of both.
Step-by-step Derivation:
C++ structures support member functions just like classes. Example:
struct Point {
double x, y;
void display() { // Legal member function
cout << x << ", " << y;
}
};
This compiles and runs correctly in any standard C++ compiler (C++98 onwards). The statement is factually FALSE.