| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | #defines |
On (16 Nov 96) steven pasztor wrote to David Nugent...
DN> Obfuscate
sp> The "in word" of AUST_C_HERE... So far, I've yet to see
anywhere, on
sp> fidonet or otherwise, in which that word appears more than once a year!
sp> Yet in here, it's appeared enough for the rest of them...
Obfuscation can be done in any language. Politicians do it every
day in English. The easiest way to do it in a computer program is to
omit comments, the second easiest way is to use meaningless
variable names. For example, programs in Basic can become difficult
to read when one and two letter variable names are used (a
requirement in some limited Basics).
sp> Anywayz, onto matters of much more importance...
sp> Say, anyone good at explaining how to do that inheritance thing? (I'm
sp>
sp> How do I do that in C? These darn BC++ books are no help, and we don't do
sp> it in uni till sometime next year... And even then it'll probably be
sp> mid-semester by the time we get around to anything that complicated... :-)
Crash course in C++ inheritance:
/* header file */
class Base
{
public:
Base(int howbig); // constructor
int ask1(void); // functions
int ask2(void);
virtual int ask3(void); // virtual function
int dothings(void);
int a,b; // public variables - anyone can see/alter them
protected:
int c,d,e; // protected variables, derived classes can see them
private:
int f,x; // variables local to Base, derived classes can't see them
}
class Derived : public Base
{
public:
Derived( int howbig, int whatmode); // different constructor from above
int ask2(void); // completely overrides ask2() in Base
int ask3(void); // redefinition of virtual function - see below
protected:
int g; // new variable in Derived, doesn't exist in Base
int x; /* new here, has no relation to x in Base, because that x
is private. Confusing technique, avoid if possible. */
}
/* end header file */
/* class definitions */
Base::Base(int howbig) // constructor
{
a = howbig; // remember the parameter passed to the constructor
b = 0; // initialize a class variable
}
int Base::ask1(void) // typical member function
{
return d;
}
int Base::ask2(void) // another typical member function
{
return e;
}
int Base::ask3(void) // same as above
{
return e;
}
int Base::dothings(void)
{
int i = ask3(); // calls a virtual function - see below
int j = ask2();
return i;
}
Derived::Derived (int howbig, int whatmode) :
Base(howbig) // constructor showing inheritance from Base
{
g = whatmode; // remember the extra parameter passed to this constructor
}
int Derived::ask2(void) // overrides ask2() in Base - see below
{
return g;
}
int Derived::ask3(void) // overrides ask2() in Base - see below
{
return g;
}
/* example class implementation */
Base B1(20);
Derived D1(25,4);
/* end */
Derived inherits Base, and can access all of Base's variables and
functions except the private variables f & x which are entirely
local to Base, and the function ask2 (see below).
Derived's constructor passes howbig straight to Base's constructor,
and uses whatmode for itself.
Derived::ask1() calls Base::ask1(), and will return d if called
from either Base or Derived. Derived::ask2() overrides
Base::ask2(). If called from Derived, ask2 will return g. If called
from Base, it will return e.
ask3() qualifies as polymorphic. This is a bit trickier. Just like
ask2(), the version called from Base is different from the version
called from Derived. But (*BIG* but!) there is a function
dothings() in Base that calls ask3(). This function is not
redefined in Derived, so when called from Derived, the Base
version is called. When dothings() is called from Base, it calls
Base::ask2() and Base::ask3(), quite normally. When it is called
from Derived, however, the polymorphic function ask3() *changes*,
while the standard function ask2() doesn't: the actual functions
called are Base::ask2(), and Derived::ask3().
This is a pretty potent concept. For example, I have a program that
monitors a serial data stream, sending translated bits of it to
screen, disk, and printer. A base class handles all the stuff that
is common to all three outputs. Three derived classes provide the
output functions and the individual formatting and selection
functions. Most of these functions are called by functions in the
base class, so they are all polymorphic (virtual). Note that it is
*not* the functions in the derived classes calling the common
functions, it's the other way round.
An earlier version of this was written in C, and used function
pointers passed as parameters to functions to do the same thing.
Somewhat less elegant. Possible, though.
You might buy a commercial precompiled library to use in your
programs. The source code may not be available to you, but if the
classes in the library use virtual functions and their header files
are available, then you can get their classes to call *your*
functions by writing your own classes derived from theirs.
Borland's Turbo Vision uses this quite a lot.
Cheers
--- PPoint 1.88
* Origin: Silicon Heaven (3:711/934.16)SEEN-BY: 711/808 934 @PATH: 711/934 |
|
| 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™.