 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Zero Guest
|
Posted: Wed Oct 18, 2006 9:10 am Post subject: !!!Simple question on heritage or just misunderstanding!!! |
|
|
Hello everyone,
here I have an example for heritage and I wonder
why the the object of type Down is able to change the private
attributes
from Above. Because I thought, when using heritage, the private
attributes
of Above are not useable. Can anybody remove my misunderstanding??
EXAMPLE:
#include <cstdlib>
#include <iostream>
using namespace std;
class Above
{
private:
int iTest1;
int iTest2;
public:
void setVars(int a, int b)
{
iTest1 = a;
};
};
class Down : public Above
{
};
int main(int argc, char *argv[])
{
Above a;
Down b;
b.setVars(4,5);
return EXIT_SUCCESS;
} |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Wed Oct 18, 2006 9:10 am Post subject: Re: !!!Simple question on heritage or just misunderstanding! |
|
|
Zero wrote:
| Quote: | Hello everyone,
here I have an example for heritage and I wonder
why the the object of type Down is able to change the private
attributes
from Above. Because I thought, when using heritage, the private
attributes
of Above are not useable. Can anybody remove my misunderstanding??
EXAMPLE:
#include <cstdlib
#include <iostream
using namespace std;
class Above
{
private:
int iTest1;
int iTest2;
public:
void setVars(int a, int b)
{
iTest1 = a;
};
};
class Down : public Above
{
};
int main(int argc, char *argv[])
{
Above a;
Down b;
b.setVars(4,5);
return EXIT_SUCCESS;
}
|
If you take your old car and give it a new paint job, new mags, brand
new suspension, add a sport exhaust, neon lights, a wicked sound system
and change the sprak plugs too, what are the chances that the ignition
keyslot will still accept your old car keys?
Now, the word heritage is what is bugging me and probably causes some
of your missunderstanding.
We say inheritance instead to describe a transposition, not something
you gained from some disgusting relative.
Heritage might best be replaced by transmission here, and i'm not
refering to the new transmission in the car either. Heritage is rather
composition as in a person has_a heritage but a person inherits
features from his parent's genes.
About your choice of Class names...
Down should not be deriving from Above in your class hierarchy. Down is
a Direction and Above is a relative position. These are unrelated.
Derivatives are not written to express differences, they express
commonality and specializations.
You could argue:
class Direction
{
};
class Up : public Direction
{
};
class Down : public Direction
{
};
etc... |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Oct 18, 2006 9:10 am Post subject: Re: !!!Simple question on heritage or just misunderstanding! |
|
|
* Zero:
| Quote: | Hello everyone,
here I have an example for heritage and I wonder
why the the object of type Down is able to change the private
attributes
from Above. Because I thought, when using heritage, the private
attributes
of Above are not useable. Can anybody remove my misunderstanding??
EXAMPLE:
#include <cstdlib
#include <iostream
using namespace std;
class Above
{
private:
int iTest1;
int iTest2;
public:
void setVars(int a, int b)
{
iTest1 = a;
};
};
class Down : public Above
{
};
int main(int argc, char *argv[])
{
Above a;
Down b;
b.setVars(4,5);
return EXIT_SUCCESS;
}
|
Consider that even though you can't control a dog's limbs directly (that
would entail accessing the dog's internal nervous system), you can tell
a dog to jump or lie down, using the the dog's public interface to the
world, and the dog will then, if it's well-behaved, /itself/ order its
limbs to do whatever you want -- and much better than you could have
controlled them directly if you had access.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|