195k views
5 votes
Which of the following is true about classes and structs?

a) By default, all members of a struct are public and all members of a
class are private.
b) A struct variable is passed by value only, and a class variable is passed
by reference only.
c) An assignment operator is allowed on class variables, but not on struct
variables.
d) All the above

1 Answer

4 votes

Final answer:

The true statement about classes and structs is that by default, all members of a struct are public and all members of a class are private. Statements regarding passing by value/reference and the assignment operator are incorrect for both structs and classes.

Step-by-step explanation:

The correct answer to the question of what is true about classes and structs is a) By default, all members of a struct are public and all members of a class are private. This means, in the context of programming, especially in languages like C++ and C#, when you define a struct, unless you specify otherwise, all the data members and member functions are accessible from outside the struct. On the other hand, for a class, unless specified otherwise, all members are private and can only be accessed through public member functions (getters and setters) or friends of the class.

b) A struct variable is passed by value only, and a class variable is passed by reference only is incorrect. Both structs and classes can be passed by value or by reference, depending on how you pass them in a function.

c) An assignment operator is allowed on class variables, but not on struct variables is also incorrect. Both structs and classes in C++ and similar languages can have assignment operators defined for them, and the behaviour is defined by how the programmer implements the operator overloading.

User Some Random IT Boy
by
8.4k points