I'm having some trouble keeping apart the terms class, object, variable and datatype.
can a class be considered as a datatype? can an object be considered as a variable?
Also, what's the technical difference?
I'm having some trouble keeping apart the terms class, object, variable and datatype.
can a class be considered as a datatype? can an object be considered as a variable?
Also, what's the technical difference?
Variables are objects with direct names.
i is a variable and an object.
*p is an object but not a variable.
Classes and types are pretty much identical, except types includes primitive types like int. Realistically, they're pretty interchangable- as well as variable/object. The reality of the C++ Standard is that very few rules apply differently to classes than to types, and variables than to objects.
There are two different uses of these terms:
Casual use:
C/C++ standard use (comes from the C standard, which isn't an Object-Oriented Language:
A class and data type are the same thing. Think of them as blueprints for a house. An object is a house constructed based on those blueprints. A variable is an address label that points to the house that was made from the blueprints. A pointer is the same as a variable, and it's just a way to locate a specific house that's been built, so it can be modified.
Hope this helps.
A data type is way of strongly indicating the way some data is used (in strongly-typed languages like c++). For example, the
int
datatype lets you know that you can perform actions traditionally associated with integer (whole) numbers. When something has thechar
data type, that lets you know that it can be used like a character (say, a letter of the alphabet). These two are examples of basic data types - built into the language.A class is (usually*) a way of assembling some data, and giving it a unique interface to access and modify that data. The same way that you can add or subtract integers, or divide floating point numbers (without truncation), the functions you put in your class determine how it is used by your program. Similarly, classes created by others provide an interface so that you can use objects of those classes just like you would use integers.
Variables are the individual instances of some data type. If you have some
int number = 0;
, that is a variable. This stands in contrast to "constants", i.e., unmodifiable symbols likeconst double pi = 3.14159;
.The word "objects" can be considered ambiguous - most of the time, when people use the word "objects", they mean "instances of some
class
orstruct
". Sometimes though, it is used to refer to classes. This can be confusing, but will become more clear as you become comfortable with the terms.In summary, classes can be datatypes (and usually are*). Objects can be variables (or constants, for that matter). You're on the right track! The more you read about or use these terms, the clearer the distinction will become.
*When I say "usually," what I mean is this - normally in the course of using c++, all of the code you will come across will use classes to represent data, in some way or another. However, on a rare occasion, someone gets into their head the idea of making a class that is pure static interface - no data. In their moment of brilliance, they decide to make the constructors private, disallowing instantiation (making what is called a "singleton"). I have never referred to these as types, or heard anyone else refer to them as types. I'm happy to be corrected on this, but for now, that's where I stand.
Yes, you can consider a class to be a datatype, you can also consider an instance of a class to be a variable.
But a class can be more than a datatype - it supports bundling methods along with data.
If you are trying to understand the difference between object-oriented design and design that is not object-oriented, then it is useful to distinguish between simple data and data that is part of a class. In C, data is inert. An individual variable or a structure is simply a receptacle for information. You can associate functions with that data, and you can adopt conventions for dealing with that data, but the association and conventions must be imposed by some agreement beyond the language. In an object-oriented language like C++, methods can be part of objects. Data access can be defined at the object, method, and field level. Inheritance is supported as a language feature.
Getting into the technical details:
Class is roughly synonymous with datatype. Variable is roughly synonymous with object. A class is an abstract representation of a thing. It is a description rather than the thing itself. As others have said, the term object can be ambiguous - referring to either a class or instances of that class. It is more precise to say that an instance of an object (or class) is a variable.
A class can be considered a group of scoped function and private variables. A object is an instance of a class, for example toyata can be a object instance of a class called Car. A variable is a identifier. It represents a identifier who's values and state can change in time A datatype represents the type of data.
For example, assume Car is a class. Then the statement:
Has a class, data-type, identifier(variable) and object.