| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: C++ |
From: John Beckett
"Geo" wrote in message
news::
> Ok but that doesn't explain what the difference between a data type like
> "int" and a class like "Date" is.
int i;
Date d;
i is a "variable" in that it contains data only. Operations
performed on i (like addition) are provided by the compiler and their
meaning can't be changed.
d is also a variable but it has user-defined functions, aka methods. A
self-contained thing with data and methods is known as an object because
you can (probably) manipulate it without knowledge of its inner workings.
It could be argued that there is not much point in insisting on calling i a
variable and d an object. Personally, I would regard d also as a variable.
However, the fact that a properly-designed class takes on a sort-of
personality and life of its own justifies in giving it the
"object" name.
Consider the function:
void func()
{
int i;
Date d;
// ... do stuff ....
}
The compiler simply reserves space on the stack for i. Nothing special
happens with the memory used for i at the beginning or end of the function
(i contains junk initially).
By contrast, the compiler reserves space on the stack for the data in d
(which includes some tables that a class needs), *and* the compiler calls
the constructor for d at the beginning of the function, *and* the compiler
calls the destructor for d at the end of the function. This is the
essential difference between a variable and an object.
John
--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)SEEN-BY: 633/267 270 5030/786 @PATH: 379/45 1 396/45 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.