Given this:
struct { int x; } ix;
struct A { A() {}; int x; };
A ia;
Which of these is true?
a. ix is an object
b. ia is an object
c. both are objects
d. both are not objects.
Given this:
struct { int x; } ix;
struct A { A() {}; int x; };
A ia;
Which of these is true?
a. ix is an object
b. ia is an object
c. both are objects
d. both are not objects.
The word "object" is a rather ambiguous specification without some more context, but in general objects have identity, behavior, and state.
Neither ix
nor ia
have all three; ix
fails because it lacks identity or behavior, and ia
fails because it has no behavior. Both are essentially just blobs of data.
By my definition, I'd say an object has properties and methods. Both nouns and verbs.
You can kick a ball, you can invade a country, and you can eat, milk, or punch a cow. Those are therefore objects.
You might have a data structure that represents the properties of a ball (radius), country (population), or cow (daily milk output in liters), but that data structure doesn't represent an object in my mind until you tell it how to process pertinent behaviors.
I recognize this definition may not work in 100% of cases, but it's close enough for my needs.
Technically, an object is an instance of a class, but objects' true usefulness lies in their ability to encapsulate information and aid in the design of systems. They are an analysis tool.
Many of these answers have ignored the C++ tag. In C++, "an object is a region of storage. [Note: a function is not an object, regardless of whether or not it occupies storage in the same way that objects do.]" (The C++ Standard, 1.8/1).
If the homework question is about C++, then no other definition of object is applicable, not even "anything that is visible or tangible and is relatively stable in form" (dictionary.reference.com). It's not asking for your opinion about OOP principles, it's in effect asking whether ix and ia are variables.
Since it's homework I'll not tell you the answer, but do note that struct { int x; } ix;
is not the same thing as struct ix { int x; };
.
On the other hand, if the homework assignment is about OOP principles, then knock yourself out with whatever definition your lecturer has given you of "object". Since I don't know what that is, I can't tell you what answer he'll consider correct...
Given the C++
tag, the answer is pretty much "take your choice."
The C standard defines an object as meaning (in essence) anything that has an address, including all instances of native/primitive types (e.g. int). Since C++ depends so heavily on C, that definition still carries some weight in C++. By this definition, essentially every variable is an object, and so are a few other things (e.g. character string literals, dynamically allocated blocks of memory).
In Smalltalk (at rather the opposite extreme) the answer would be none of them is an object -- an object never has public data. Its behavior is defined entirely in terms of responses to messages.
An object is an instnace of a type (be it POD or class).
As such you are able to extract the address of an object. All objects take up at least one byte. The reason for this is that you don't have to add special code for handling zero sized objects because every object in memory has a destinct address (by making everything at least one byte the compiler will automatically have a unique address for each object).
int main()
{
struct { int x; } ix;
struct A { A() {}; int x; };
A ia;
ix.x = 5; // Assigned a value.
// Thus it has state and thus is an object.
ia.x = 6; // Assigned a value.
}
So they are both objects.
These questions are impossible to answer without extra clarification. The question is tagged C++, which means that the language is supposedly C++.
In this case, if the declarations are made in namespace scope, the ix
declaration is invalid. It is illegal to use an unnamed class type (which has no linkage) to declare an object with external linkage. The declaration of ix
would work in local scope
void foo() {
struct { int x; } ix; // OK, no linkage
}
It might also work if ix
was declared with internal linkage at namespace scope
static struct { int x; } ix; // OK? Internal linkage?
although I personally believe that this was intended to be ill-formed as well (Comeau somehow allows it).
But a namespace-scope declaration with external linkage is ill-formed
// In namespace scope
struct { int x; } ix; // ERROR
So, if the namespace scope is assumed and if the above declarations are meant to be taken as a single piece of code, there are no meaningful answers to these questions. The whole code is simply invalid. It is meaningless. It is not C++.
Otherwise, if ix
is declared with no linkage (local) or with internal linkage, then ix
is an object.
As for ia
, it is an object regardless of where it is declared, since the class type is named.
Note though that the notion of object in C++ has nothing to do with classes. Object in C++ is a region of storage (memory). A variable of int
type is an object in C++, for one example.
Added later: The bit about legality of ix
declaration is an interesting issue. Apparently C++98 allowed such declarations, which was proposed to be outlawed in DR#132. However, later the proposal was rejected (for a rather weird reason) and the things were left as is. Yet, Comeau Online refuses to accept a declaration of an object with external linkage with unnamed type (internal linkage is OK). It could quite possibly be a formal bug in Comeau compiler (not that I'd complain about it).
Added even later: Oh, I see that there's an even later DR#389, which finally outlaws such declarations, but the status of this DR is still CD1.
The real answer is "e. Whoever writes code like this should be coached to improve their legibility." Okay, that was a joke.
This question isn't so complex. But elsewhere, I've seen programming tests written purposely complex for the purpose of seeing if you can solve puzzles. It's completely pointless, because code that is that complex shouldn't and usually does not exist. If it's that hard to read, it's poorly written code.
Remember, code is not written for computers. Code is written for the next developer after you to read and understand.
And don't write code just so it works. That's not a high enough standard. The worst junk in the world will run, but it's a nightmare to fix or upgrade.
There are two commonly used definitions of "object" in C++.
One is official according to the C++ standard, and says that everything that has storage allocated for it is an object. A struct is an object, an int is an object, a bool is an object, a pointer is an object, a string literal is an object, and so on. By this definition, ix
, ia
and x
are all objects. But this probably isn't what your teacher meant. You have to be a bit of a language lawyer to use this definition, and it's not that widely known among "average" C++ users. It's also not a very relevant definition for someone just learning the language.
The definition you are probably expected to use is that of an "object" in the object-oriented sense. Here (at least in the C++ family of languages), an object is typically meant to be an instance of a class.
Which leaves the next obvious question: Is an instance of a struct also an object? Depends. In C++, a class and a struct are essentially the same, so semantically, yes, but technically, you're not using the class
keyword, so syntactically, probably not.
In short: It's a silly, and badly worded question, and only you know what your teacher means or wants to hear, because you're the one who attended the classes, not us. All we can do is guess at what he thinks defines a class.