Reputation: 523
I'm confused about why classes are considered data types.
I understand that part of them is "data", and the other part is the methods. Why are they called data types?
Procedures in procedural programming languages, like C, hold one or more fundamental data types, sometimes. But they are not called data types.
Upvotes: 5
Views: 635
Reputation: 1
Confusion, misintegration, conflation of data and procedures, and definition by non-essentials is what the OOP approach is all about.
Google "what is wrong with object oriented programming" for some straight answers.
Upvotes: -2
Reputation: 69037
To put it roughly, you can think of a data type as a name plus an implementation of that type. The implementation defines what kind of operations you can do on entities of that type, while the name is used to ensure that you do not use a type when another one is expected.
Now, everything can be a type. You say that "procedures" are not type -- and it may usual be like that -- but you can define a type which corresponds to a certain kind of functions (or procedures), like this:
typedef void (*function_type)(void *);
contrast this to the simple declaration of a pointer to a function of that type:
void(*pf)(void *);
as you see, the trick is done by the keyword typedef
which defined the function_type
type. On entities of that type you can do very few operations: extracting the address, calling...
Again, to put it roughly, the trick is "giving a name" in order to be able to tell one entity from another.
For classes the reasoning is just the same, but there is no reason that they should not be considered data types since they define a name and allow you to associate a set of operations to that name so you can check the way entities of that type are used.
Upvotes: 1
Reputation: 361642
Classes in object oriented programming are not simply data type, they're abstract data type (ADT). So you should be asking :
What is abstract data type (ADT)?
ADT consists of data and operations that operate on the data. Data as well as operations both are parts of ADT.
Wikipedia says,
In computing, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics. An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.
In other words, different classes are different representations of different abstract data types which are composed of data (usually internal data called private
in OOP) as well as operations (usually called method or member-function).
Upvotes: 1
Reputation: 648
A procedure doesn't "hold" data types. It may define one temporarily for its own use while it's running, but it doesn't actually keep it. You can make a very poor approximation of it using static variables, but you can't make those public-they still apply and are visible only to their procedure. And global variables, of course, are not "held" by their procedures.
Classes, on the other hand, function much more like a data type. They do have methods, but the idea of methods is designed around the idea that they will be manipulating or returning class data in well-defined ways (well-defined if you wrote your class right, anyway). They can be instantiated as a data type, and passed to a function just like a primitive:
class Foo
{
...
};
void main()
{
Foo bar;
Foo *baz = new Foo;
delete baz;
}
void useFoo(Foo &blah)
{
blah.doSomething();
...
}
In short, they're considered data types because they are data types, they behave like data types, and they store specified types of data. The fact that they act on that data in specified ways doesn't change that (after all, the "+" operator has a specified effect on an integer or a double, too.)
Upvotes: 1
Reputation: 421150
Given the definition at Wikipedia:
[...] a data type (or datatype) is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; and the way values of that type can be stored.
I believe classes fit the definition quite well, while procedures in procedural programming like C don't fit at all.
Classes represent a set of possible values (objects) and defines the possible operations that can be done on the values of this type. It also makes it clear how to represent objects of the class in memory.
Procedures in C however does not identify a set of possible values and it wouldn't make sense to say that there are definitions stating what operations can be performed on procedures.
Perhaps your confusion stems from some text on functional programming where a procedure (or function) is treated as a first class value which has a specific type.
Upvotes: 7